Bug Summary

File:tools/clang/lib/Sema/SemaOpenMP.cpp
Warning:line 1021, column 38
Called C++ object pointer is null

Annotated Source Code

1//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
11/// clauses.
12///
13//===----------------------------------------------------------------------===//
14
15#include "TreeTransform.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTMutationListener.h"
18#include "clang/AST/CXXInheritance.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclOpenMP.h"
22#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
25#include "clang/AST/TypeOrdering.h"
26#include "clang/Basic/OpenMPKinds.h"
27#include "clang/Basic/TargetInfo.h"
28#include "clang/Lex/Preprocessor.h"
29#include "clang/Sema/Initialization.h"
30#include "clang/Sema/Lookup.h"
31#include "clang/Sema/Scope.h"
32#include "clang/Sema/ScopeInfo.h"
33#include "clang/Sema/SemaInternal.h"
34using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Stack of data-sharing attributes for variables
38//===----------------------------------------------------------------------===//
39
40namespace {
41/// \brief Default data sharing attributes, which can be applied to directive.
42enum DefaultDataSharingAttributes {
43 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
46};
47
48/// \brief Stack for tracking declarations used in OpenMP directives and
49/// clauses and their data-sharing attributes.
50class DSAStackTy final {
51public:
52 struct DSAVarData final {
53 OpenMPDirectiveKind DKind = OMPD_unknown;
54 OpenMPClauseKind CKind = OMPC_unknown;
55 Expr *RefExpr = nullptr;
56 DeclRefExpr *PrivateCopy = nullptr;
57 SourceLocation ImplicitDSALoc;
58 DSAVarData() {}
59 };
60 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
61 OperatorOffsetTy;
62
63private:
64 struct DSAInfo final {
65 OpenMPClauseKind Attributes = OMPC_unknown;
66 /// Pointer to a reference expression and a flag which shows that the
67 /// variable is marked as lastprivate(true) or not (false).
68 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69 DeclRefExpr *PrivateCopy = nullptr;
70 };
71 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
73 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
75 /// Struct that associates a component with the clause kind where they are
76 /// found.
77 struct MappedExprComponentTy {
78 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
79 OpenMPClauseKind Kind = OMPC_unknown;
80 };
81 typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy>
82 MappedExprComponentsTy;
83 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
84 CriticalsWithHintsTy;
85 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
86 DoacrossDependMapTy;
87
88 struct SharingMapTy final {
89 DeclSAMapTy SharingMap;
90 AlignedMapTy AlignedMap;
91 MappedExprComponentsTy MappedExprComponents;
92 LoopControlVariablesMapTy LCVMap;
93 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
94 SourceLocation DefaultAttrLoc;
95 OpenMPDirectiveKind Directive = OMPD_unknown;
96 DeclarationNameInfo DirectiveName;
97 Scope *CurScope = nullptr;
98 SourceLocation ConstructLoc;
99 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
100 /// get the data (loop counters etc.) about enclosing loop-based construct.
101 /// This data is required during codegen.
102 DoacrossDependMapTy DoacrossDepends;
103 /// \brief first argument (Expr *) contains optional argument of the
104 /// 'ordered' clause, the second one is true if the regions has 'ordered'
105 /// clause, false otherwise.
106 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
107 bool NowaitRegion = false;
108 bool CancelRegion = false;
109 unsigned AssociatedLoops = 1;
110 SourceLocation InnerTeamsRegionLoc;
111 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
112 Scope *CurScope, SourceLocation Loc)
113 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
114 ConstructLoc(Loc) {}
115 SharingMapTy() {}
116 };
117
118 typedef SmallVector<SharingMapTy, 4> StackTy;
119
120 /// \brief Stack of used declaration and their data-sharing attributes.
121 StackTy Stack;
122 /// \brief true, if check for DSA must be from parent directive, false, if
123 /// from current directive.
124 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
125 Sema &SemaRef;
126 bool ForceCapturing = false;
127 CriticalsWithHintsTy Criticals;
128
129 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
130
131 DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D);
132
133 /// \brief Checks if the variable is a local for OpenMP region.
134 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
135
136public:
137 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
138
139 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
140 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
141
142 bool isForceVarCapturing() const { return ForceCapturing; }
143 void setForceVarCapturing(bool V) { ForceCapturing = V; }
144
145 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
146 Scope *CurScope, SourceLocation Loc) {
147 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
148 Stack.back().DefaultAttrLoc = Loc;
149 }
150
151 void pop() {
152 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!")((Stack.size() > 1 && "Data-sharing attributes stack is empty!"
) ? static_cast<void> (0) : __assert_fail ("Stack.size() > 1 && \"Data-sharing attributes stack is empty!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 152, __PRETTY_FUNCTION__))
;
153 Stack.pop_back();
154 }
155
156 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
157 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
158 }
159 const std::pair<OMPCriticalDirective *, llvm::APSInt>
160 getCriticalWithHint(const DeclarationNameInfo &Name) const {
161 auto I = Criticals.find(Name.getAsString());
162 if (I != Criticals.end())
163 return I->second;
164 return std::make_pair(nullptr, llvm::APSInt());
165 }
166 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
167 /// add it and return NULL; otherwise return previous occurrence's expression
168 /// for diagnostics.
169 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
170
171 /// \brief Register specified variable as loop control variable.
172 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
173 /// \brief Check if the specified variable is a loop control variable for
174 /// current region.
175 /// \return The index of the loop control variable in the list of associated
176 /// for-loops (from outer to inner).
177 LCDeclInfo isLoopControlVariable(ValueDecl *D);
178 /// \brief Check if the specified variable is a loop control variable for
179 /// parent region.
180 /// \return The index of the loop control variable in the list of associated
181 /// for-loops (from outer to inner).
182 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
183 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
184 /// parent directive.
185 ValueDecl *getParentLoopControlVariable(unsigned I);
186
187 /// \brief Adds explicit data sharing attribute to the specified declaration.
188 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
189 DeclRefExpr *PrivateCopy = nullptr);
190
191 /// \brief Returns data sharing attributes from top of the stack for the
192 /// specified declaration.
193 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
194 /// \brief Returns data-sharing attributes for the specified declaration.
195 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
196 /// \brief Checks if the specified variables has data-sharing attributes which
197 /// match specified \a CPred predicate in any directive which matches \a DPred
198 /// predicate.
199 DSAVarData hasDSA(ValueDecl *D,
200 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
201 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
202 bool FromParent);
203 /// \brief Checks if the specified variables has data-sharing attributes which
204 /// match specified \a CPred predicate in any innermost directive which
205 /// matches \a DPred predicate.
206 DSAVarData
207 hasInnermostDSA(ValueDecl *D,
208 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
209 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
210 bool FromParent);
211 /// \brief Checks if the specified variables has explicit data-sharing
212 /// attributes which match specified \a CPred predicate at the specified
213 /// OpenMP region.
214 bool hasExplicitDSA(ValueDecl *D,
215 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
216 unsigned Level, bool NotLastprivate = false);
217
218 /// \brief Returns true if the directive at level \Level matches in the
219 /// specified \a DPred predicate.
220 bool hasExplicitDirective(
221 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
222 unsigned Level);
223
224 /// \brief Finds a directive which matches specified \a DPred predicate.
225 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
226 const DeclarationNameInfo &,
227 SourceLocation)> &DPred,
228 bool FromParent);
229
230 /// \brief Returns currently analyzed directive.
231 OpenMPDirectiveKind getCurrentDirective() const {
232 return Stack.back().Directive;
233 }
234 /// \brief Returns parent directive.
235 OpenMPDirectiveKind getParentDirective() const {
236 if (Stack.size() > 2)
237 return Stack[Stack.size() - 2].Directive;
238 return OMPD_unknown;
239 }
240
241 /// \brief Set default data sharing attribute to none.
242 void setDefaultDSANone(SourceLocation Loc) {
243 Stack.back().DefaultAttr = DSA_none;
244 Stack.back().DefaultAttrLoc = Loc;
245 }
246 /// \brief Set default data sharing attribute to shared.
247 void setDefaultDSAShared(SourceLocation Loc) {
248 Stack.back().DefaultAttr = DSA_shared;
249 Stack.back().DefaultAttrLoc = Loc;
250 }
251
252 DefaultDataSharingAttributes getDefaultDSA() const {
253 return Stack.back().DefaultAttr;
254 }
255 SourceLocation getDefaultDSALocation() const {
256 return Stack.back().DefaultAttrLoc;
257 }
258
259 /// \brief Checks if the specified variable is a threadprivate.
260 bool isThreadPrivate(VarDecl *D) {
261 DSAVarData DVar = getTopDSA(D, false);
262 return isOpenMPThreadPrivate(DVar.CKind);
263 }
264
265 /// \brief Marks current region as ordered (it has an 'ordered' clause).
266 void setOrderedRegion(bool IsOrdered, Expr *Param) {
267 Stack.back().OrderedRegion.setInt(IsOrdered);
268 Stack.back().OrderedRegion.setPointer(Param);
269 }
270 /// \brief Returns true, if parent region is ordered (has associated
271 /// 'ordered' clause), false - otherwise.
272 bool isParentOrderedRegion() const {
273 if (Stack.size() > 2)
274 return Stack[Stack.size() - 2].OrderedRegion.getInt();
275 return false;
276 }
277 /// \brief Returns optional parameter for the ordered region.
278 Expr *getParentOrderedRegionParam() const {
279 if (Stack.size() > 2)
280 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
281 return nullptr;
282 }
283 /// \brief Marks current region as nowait (it has a 'nowait' clause).
284 void setNowaitRegion(bool IsNowait = true) {
285 Stack.back().NowaitRegion = IsNowait;
286 }
287 /// \brief Returns true, if parent region is nowait (has associated
288 /// 'nowait' clause), false - otherwise.
289 bool isParentNowaitRegion() const {
290 if (Stack.size() > 2)
291 return Stack[Stack.size() - 2].NowaitRegion;
292 return false;
293 }
294 /// \brief Marks parent region as cancel region.
295 void setParentCancelRegion(bool Cancel = true) {
296 if (Stack.size() > 2)
297 Stack[Stack.size() - 2].CancelRegion =
298 Stack[Stack.size() - 2].CancelRegion || Cancel;
299 }
300 /// \brief Return true if current region has inner cancel construct.
301 bool isCancelRegion() const { return Stack.back().CancelRegion; }
302
303 /// \brief Set collapse value for the region.
304 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
305 /// \brief Return collapse value for region.
306 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
307
308 /// \brief Marks current target region as one with closely nested teams
309 /// region.
310 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
311 if (Stack.size() > 2)
312 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
313 }
314 /// \brief Returns true, if current region has closely nested teams region.
315 bool hasInnerTeamsRegion() const {
316 return getInnerTeamsRegionLoc().isValid();
317 }
318 /// \brief Returns location of the nested teams region (if any).
319 SourceLocation getInnerTeamsRegionLoc() const {
320 if (Stack.size() > 1)
321 return Stack.back().InnerTeamsRegionLoc;
322 return SourceLocation();
323 }
324
325 Scope *getCurScope() const { return Stack.back().CurScope; }
326 Scope *getCurScope() { return Stack.back().CurScope; }
327 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
328
329 /// Do the check specified in \a Check to all component lists and return true
330 /// if any issue is found.
331 bool checkMappableExprComponentListsForDecl(
332 ValueDecl *VD, bool CurrentRegionOnly,
333 const llvm::function_ref<
334 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
335 OpenMPClauseKind)> &Check) {
336 auto SI = Stack.rbegin();
337 auto SE = Stack.rend();
338
339 if (SI == SE)
340 return false;
341
342 if (CurrentRegionOnly) {
343 SE = std::next(SI);
344 } else {
345 ++SI;
346 }
347
348 for (; SI != SE; ++SI) {
349 auto MI = SI->MappedExprComponents.find(VD);
350 if (MI != SI->MappedExprComponents.end())
351 for (auto &L : MI->second.Components)
352 if (Check(L, MI->second.Kind))
353 return true;
354 }
355 return false;
356 }
357
358 /// Create a new mappable expression component list associated with a given
359 /// declaration and initialize it with the provided list of components.
360 void addMappableExpressionComponents(
361 ValueDecl *VD,
362 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
363 OpenMPClauseKind WhereFoundClauseKind) {
364 assert(Stack.size() > 1 &&((Stack.size() > 1 && "Not expecting to retrieve components from a empty stack!"
) ? static_cast<void> (0) : __assert_fail ("Stack.size() > 1 && \"Not expecting to retrieve components from a empty stack!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 365, __PRETTY_FUNCTION__))
365 "Not expecting to retrieve components from a empty stack!")((Stack.size() > 1 && "Not expecting to retrieve components from a empty stack!"
) ? static_cast<void> (0) : __assert_fail ("Stack.size() > 1 && \"Not expecting to retrieve components from a empty stack!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 365, __PRETTY_FUNCTION__))
;
366 auto &MEC = Stack.back().MappedExprComponents[VD];
367 // Create new entry and append the new components there.
368 MEC.Components.resize(MEC.Components.size() + 1);
369 MEC.Components.back().append(Components.begin(), Components.end());
370 MEC.Kind = WhereFoundClauseKind;
371 }
372
373 unsigned getNestingLevel() const {
374 assert(Stack.size() > 1)((Stack.size() > 1) ? static_cast<void> (0) : __assert_fail
("Stack.size() > 1", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 374, __PRETTY_FUNCTION__))
;
375 return Stack.size() - 2;
376 }
377 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
378 assert(Stack.size() > 2)((Stack.size() > 2) ? static_cast<void> (0) : __assert_fail
("Stack.size() > 2", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 378, __PRETTY_FUNCTION__))
;
379 assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive))((isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive
)) ? static_cast<void> (0) : __assert_fail ("isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive)"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 379, __PRETTY_FUNCTION__))
;
380 Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
381 }
382 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
383 getDoacrossDependClauses() const {
384 assert(Stack.size() > 1)((Stack.size() > 1) ? static_cast<void> (0) : __assert_fail
("Stack.size() > 1", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 384, __PRETTY_FUNCTION__))
;
385 if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
386 auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
387 return llvm::make_range(Ref.begin(), Ref.end());
388 }
389 return llvm::make_range(Stack[0].DoacrossDepends.end(),
390 Stack[0].DoacrossDepends.end());
391 }
392};
393bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
394 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
395 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
396}
397} // namespace
398
399static ValueDecl *getCanonicalDecl(ValueDecl *D) {
400 auto *VD = dyn_cast<VarDecl>(D);
401 auto *FD = dyn_cast<FieldDecl>(D);
402 if (VD != nullptr) {
403 VD = VD->getCanonicalDecl();
404 D = VD;
405 } else {
406 assert(FD)((FD) ? static_cast<void> (0) : __assert_fail ("FD", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 406, __PRETTY_FUNCTION__))
;
407 FD = FD->getCanonicalDecl();
408 D = FD;
409 }
410 return D;
411}
412
413DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter,
414 ValueDecl *D) {
415 D = getCanonicalDecl(D);
416 auto *VD = dyn_cast<VarDecl>(D);
417 auto *FD = dyn_cast<FieldDecl>(D);
418 DSAVarData DVar;
419 if (Iter == std::prev(Stack.rend())) {
420 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
421 // in a region but not in construct]
422 // File-scope or namespace-scope variables referenced in called routines
423 // in the region are shared unless they appear in a threadprivate
424 // directive.
425 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
426 DVar.CKind = OMPC_shared;
427
428 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
429 // in a region but not in construct]
430 // Variables with static storage duration that are declared in called
431 // routines in the region are shared.
432 if (VD && VD->hasGlobalStorage())
433 DVar.CKind = OMPC_shared;
434
435 // Non-static data members are shared by default.
436 if (FD)
437 DVar.CKind = OMPC_shared;
438
439 return DVar;
440 }
441
442 DVar.DKind = Iter->Directive;
443 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
444 // in a Construct, C/C++, predetermined, p.1]
445 // Variables with automatic storage duration that are declared in a scope
446 // inside the construct are private.
447 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
448 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
449 DVar.CKind = OMPC_private;
450 return DVar;
451 }
452
453 // Explicitly specified attributes and local variables with predetermined
454 // attributes.
455 if (Iter->SharingMap.count(D)) {
456 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
457 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
458 DVar.CKind = Iter->SharingMap[D].Attributes;
459 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
460 return DVar;
461 }
462
463 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
464 // in a Construct, C/C++, implicitly determined, p.1]
465 // In a parallel or task construct, the data-sharing attributes of these
466 // variables are determined by the default clause, if present.
467 switch (Iter->DefaultAttr) {
468 case DSA_shared:
469 DVar.CKind = OMPC_shared;
470 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
471 return DVar;
472 case DSA_none:
473 return DVar;
474 case DSA_unspecified:
475 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
476 // in a Construct, implicitly determined, p.2]
477 // In a parallel construct, if no default clause is present, these
478 // variables are shared.
479 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
480 if (isOpenMPParallelDirective(DVar.DKind) ||
481 isOpenMPTeamsDirective(DVar.DKind)) {
482 DVar.CKind = OMPC_shared;
483 return DVar;
484 }
485
486 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
487 // in a Construct, implicitly determined, p.4]
488 // In a task construct, if no default clause is present, a variable that in
489 // the enclosing context is determined to be shared by all implicit tasks
490 // bound to the current team is shared.
491 if (isOpenMPTaskingDirective(DVar.DKind)) {
492 DSAVarData DVarTemp;
493 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
494 I != EE; ++I) {
495 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
496 // Referenced in a Construct, implicitly determined, p.6]
497 // In a task construct, if no default clause is present, a variable
498 // whose data-sharing attribute is not determined by the rules above is
499 // firstprivate.
500 DVarTemp = getDSA(I, D);
501 if (DVarTemp.CKind != OMPC_shared) {
502 DVar.RefExpr = nullptr;
503 DVar.CKind = OMPC_firstprivate;
504 return DVar;
505 }
506 if (isParallelOrTaskRegion(I->Directive))
507 break;
508 }
509 DVar.CKind =
510 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
511 return DVar;
512 }
513 }
514 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
515 // in a Construct, implicitly determined, p.3]
516 // For constructs other than task, if no default clause is present, these
517 // variables inherit their data-sharing attributes from the enclosing
518 // context.
519 return getDSA(++Iter, D);
520}
521
522Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
523 assert(Stack.size() > 1 && "Data sharing attributes stack is empty")((Stack.size() > 1 && "Data sharing attributes stack is empty"
) ? static_cast<void> (0) : __assert_fail ("Stack.size() > 1 && \"Data sharing attributes stack is empty\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 523, __PRETTY_FUNCTION__))
;
524 D = getCanonicalDecl(D);
525 auto It = Stack.back().AlignedMap.find(D);
526 if (It == Stack.back().AlignedMap.end()) {
527 assert(NewDE && "Unexpected nullptr expr to be added into aligned map")((NewDE && "Unexpected nullptr expr to be added into aligned map"
) ? static_cast<void> (0) : __assert_fail ("NewDE && \"Unexpected nullptr expr to be added into aligned map\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 527, __PRETTY_FUNCTION__))
;
528 Stack.back().AlignedMap[D] = NewDE;
529 return nullptr;
530 } else {
531 assert(It->second && "Unexpected nullptr expr in the aligned map")((It->second && "Unexpected nullptr expr in the aligned map"
) ? static_cast<void> (0) : __assert_fail ("It->second && \"Unexpected nullptr expr in the aligned map\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 531, __PRETTY_FUNCTION__))
;
532 return It->second;
533 }
534 return nullptr;
535}
536
537void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
538 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty")((Stack.size() > 1 && "Data-sharing attributes stack is empty"
) ? static_cast<void> (0) : __assert_fail ("Stack.size() > 1 && \"Data-sharing attributes stack is empty\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 538, __PRETTY_FUNCTION__))
;
539 D = getCanonicalDecl(D);
540 Stack.back().LCVMap.insert(
541 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
542}
543
544DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
545 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty")((Stack.size() > 1 && "Data-sharing attributes stack is empty"
) ? static_cast<void> (0) : __assert_fail ("Stack.size() > 1 && \"Data-sharing attributes stack is empty\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 545, __PRETTY_FUNCTION__))
;
546 D = getCanonicalDecl(D);
547 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
548 : LCDeclInfo(0, nullptr);
549}
550
551DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
552 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty")((Stack.size() > 2 && "Data-sharing attributes stack is empty"
) ? static_cast<void> (0) : __assert_fail ("Stack.size() > 2 && \"Data-sharing attributes stack is empty\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 552, __PRETTY_FUNCTION__))
;
553 D = getCanonicalDecl(D);
554 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
555 ? Stack[Stack.size() - 2].LCVMap[D]
556 : LCDeclInfo(0, nullptr);
557}
558
559ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
560 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty")((Stack.size() > 2 && "Data-sharing attributes stack is empty"
) ? static_cast<void> (0) : __assert_fail ("Stack.size() > 2 && \"Data-sharing attributes stack is empty\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 560, __PRETTY_FUNCTION__))
;
561 if (Stack[Stack.size() - 2].LCVMap.size() < I)
562 return nullptr;
563 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
564 if (Pair.second.first == I)
565 return Pair.first;
566 }
567 return nullptr;
568}
569
570void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
571 DeclRefExpr *PrivateCopy) {
572 D = getCanonicalDecl(D);
573 if (A == OMPC_threadprivate) {
574 auto &Data = Stack[0].SharingMap[D];
575 Data.Attributes = A;
576 Data.RefExpr.setPointer(E);
577 Data.PrivateCopy = nullptr;
578 } else {
579 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty")((Stack.size() > 1 && "Data-sharing attributes stack is empty"
) ? static_cast<void> (0) : __assert_fail ("Stack.size() > 1 && \"Data-sharing attributes stack is empty\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 579, __PRETTY_FUNCTION__))
;
580 auto &Data = Stack.back().SharingMap[D];
581 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||((Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
(A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate
) || (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate
) || (isLoopControlVariable(D).first && A == OMPC_private
)) ? static_cast<void> (0) : __assert_fail ("Data.Attributes == OMPC_unknown || (A == Data.Attributes) || (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || (isLoopControlVariable(D).first && A == OMPC_private)"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 584, __PRETTY_FUNCTION__))
582 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||((Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
(A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate
) || (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate
) || (isLoopControlVariable(D).first && A == OMPC_private
)) ? static_cast<void> (0) : __assert_fail ("Data.Attributes == OMPC_unknown || (A == Data.Attributes) || (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || (isLoopControlVariable(D).first && A == OMPC_private)"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 584, __PRETTY_FUNCTION__))
583 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||((Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
(A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate
) || (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate
) || (isLoopControlVariable(D).first && A == OMPC_private
)) ? static_cast<void> (0) : __assert_fail ("Data.Attributes == OMPC_unknown || (A == Data.Attributes) || (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || (isLoopControlVariable(D).first && A == OMPC_private)"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 584, __PRETTY_FUNCTION__))
584 (isLoopControlVariable(D).first && A == OMPC_private))((Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
(A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate
) || (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate
) || (isLoopControlVariable(D).first && A == OMPC_private
)) ? static_cast<void> (0) : __assert_fail ("Data.Attributes == OMPC_unknown || (A == Data.Attributes) || (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || (isLoopControlVariable(D).first && A == OMPC_private)"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 584, __PRETTY_FUNCTION__))
;
585 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
586 Data.RefExpr.setInt(/*IntVal=*/true);
587 return;
588 }
589 const bool IsLastprivate =
590 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
591 Data.Attributes = A;
592 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
593 Data.PrivateCopy = PrivateCopy;
594 if (PrivateCopy) {
595 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
596 Data.Attributes = A;
597 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
598 Data.PrivateCopy = nullptr;
599 }
600 }
601}
602
603bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
604 D = D->getCanonicalDecl();
605 if (Stack.size() > 2) {
606 reverse_iterator I = Iter, E = std::prev(Stack.rend());
607 Scope *TopScope = nullptr;
608 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
609 ++I;
610 }
611 if (I == E)
612 return false;
613 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
614 Scope *CurScope = getCurScope();
615 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
616 CurScope = CurScope->getParent();
617 }
618 return CurScope != TopScope;
619 }
620 return false;
621}
622
623/// \brief Build a variable declaration for OpenMP loop iteration variable.
624static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
625 StringRef Name, const AttrVec *Attrs = nullptr) {
626 DeclContext *DC = SemaRef.CurContext;
627 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
628 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
629 VarDecl *Decl =
630 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
631 if (Attrs) {
632 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
633 I != E; ++I)
634 Decl->addAttr(*I);
635 }
636 Decl->setImplicit();
637 return Decl;
638}
639
640static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
641 SourceLocation Loc,
642 bool RefersToCapture = false) {
643 D->setReferenced();
644 D->markUsed(S.Context);
645 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
646 SourceLocation(), D, RefersToCapture, Loc, Ty,
647 VK_LValue);
648}
649
650DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
651 D = getCanonicalDecl(D);
652 DSAVarData DVar;
653
654 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
655 // in a Construct, C/C++, predetermined, p.1]
656 // Variables appearing in threadprivate directives are threadprivate.
657 auto *VD = dyn_cast<VarDecl>(D);
658 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
659 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
660 SemaRef.getLangOpts().OpenMPUseTLS &&
661 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
662 (VD && VD->getStorageClass() == SC_Register &&
663 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
664 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
665 D->getLocation()),
666 OMPC_threadprivate);
667 }
668 if (Stack[0].SharingMap.count(D)) {
669 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
670 DVar.CKind = OMPC_threadprivate;
671 return DVar;
672 }
673
674 if (Stack.size() == 1) {
675 // Not in OpenMP execution region and top scope was already checked.
676 return DVar;
677 }
678
679 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
680 // in a Construct, C/C++, predetermined, p.4]
681 // Static data members are shared.
682 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
683 // in a Construct, C/C++, predetermined, p.7]
684 // Variables with static storage duration that are declared in a scope
685 // inside the construct are shared.
686 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
687 if (VD && VD->isStaticDataMember()) {
688 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
689 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
690 return DVar;
691
692 DVar.CKind = OMPC_shared;
693 return DVar;
694 }
695
696 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
697 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
698 Type = SemaRef.getASTContext().getBaseElementType(Type);
699 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
700 // in a Construct, C/C++, predetermined, p.6]
701 // Variables with const qualified type having no mutable member are
702 // shared.
703 CXXRecordDecl *RD =
704 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
705 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
706 if (auto *CTD = CTSD->getSpecializedTemplate())
707 RD = CTD->getTemplatedDecl();
708 if (IsConstant &&
709 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
710 RD->hasMutableFields())) {
711 // Variables with const-qualified type having no mutable member may be
712 // listed in a firstprivate clause, even if they are static data members.
713 DSAVarData DVarTemp = hasDSA(
714 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
715 MatchesAlways, FromParent);
716 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
717 return DVar;
718
719 DVar.CKind = OMPC_shared;
720 return DVar;
721 }
722
723 // Explicitly specified attributes and local variables with predetermined
724 // attributes.
725 auto StartI = std::next(Stack.rbegin());
726 auto EndI = std::prev(Stack.rend());
727 if (FromParent && StartI != EndI) {
728 StartI = std::next(StartI);
729 }
730 auto I = std::prev(StartI);
731 if (I->SharingMap.count(D)) {
732 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
733 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
734 DVar.CKind = I->SharingMap[D].Attributes;
735 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
736 }
737
738 return DVar;
739}
740
741DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
742 bool FromParent) {
743 D = getCanonicalDecl(D);
744 auto StartI = Stack.rbegin();
745 auto EndI = std::prev(Stack.rend());
746 if (FromParent && StartI != EndI) {
747 StartI = std::next(StartI);
748 }
749 return getDSA(StartI, D);
750}
751
752DSAStackTy::DSAVarData
753DSAStackTy::hasDSA(ValueDecl *D,
754 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
755 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
756 bool FromParent) {
757 D = getCanonicalDecl(D);
758 auto StartI = std::next(Stack.rbegin());
759 auto EndI = Stack.rend();
760 if (FromParent && StartI != EndI) {
761 StartI = std::next(StartI);
762 }
763 for (auto I = StartI, EE = EndI; I != EE; ++I) {
764 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
765 continue;
766 DSAVarData DVar = getDSA(I, D);
767 if (CPred(DVar.CKind))
768 return DVar;
769 }
770 return DSAVarData();
771}
772
773DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
774 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
775 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
776 bool FromParent) {
777 D = getCanonicalDecl(D);
778 auto StartI = std::next(Stack.rbegin());
779 auto EndI = Stack.rend();
780 if (FromParent && StartI != EndI)
781 StartI = std::next(StartI);
782 if (StartI == EndI || !DPred(StartI->Directive))
783 return DSAVarData();
784 DSAVarData DVar = getDSA(StartI, D);
785 return CPred(DVar.CKind) ? DVar : DSAVarData();
786}
787
788bool DSAStackTy::hasExplicitDSA(
789 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
790 unsigned Level, bool NotLastprivate) {
791 if (CPred(ClauseKindMode))
792 return true;
793 D = getCanonicalDecl(D);
794 auto StartI = std::next(Stack.begin());
795 auto EndI = Stack.end();
796 if (std::distance(StartI, EndI) <= (int)Level)
797 return false;
798 std::advance(StartI, Level);
799 return (StartI->SharingMap.count(D) > 0) &&
800 StartI->SharingMap[D].RefExpr.getPointer() &&
801 CPred(StartI->SharingMap[D].Attributes) &&
802 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
803}
804
805bool DSAStackTy::hasExplicitDirective(
806 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
807 unsigned Level) {
808 auto StartI = std::next(Stack.begin());
809 auto EndI = Stack.end();
810 if (std::distance(StartI, EndI) <= (int)Level)
811 return false;
812 std::advance(StartI, Level);
813 return DPred(StartI->Directive);
814}
815
816bool DSAStackTy::hasDirective(
817 const llvm::function_ref<bool(OpenMPDirectiveKind,
818 const DeclarationNameInfo &, SourceLocation)>
819 &DPred,
820 bool FromParent) {
821 // We look only in the enclosing region.
822 if (Stack.size() < 2)
823 return false;
824 auto StartI = std::next(Stack.rbegin());
825 auto EndI = std::prev(Stack.rend());
826 if (FromParent && StartI != EndI) {
827 StartI = std::next(StartI);
828 }
829 for (auto I = StartI, EE = EndI; I != EE; ++I) {
830 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
831 return true;
832 }
833 return false;
834}
835
836void Sema::InitDataSharingAttributesStack() {
837 VarDataSharingAttributesStack = new DSAStackTy(*this);
838}
839
840#define DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
841
842bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
843 assert(LangOpts.OpenMP && "OpenMP is not allowed")((LangOpts.OpenMP && "OpenMP is not allowed") ? static_cast
<void> (0) : __assert_fail ("LangOpts.OpenMP && \"OpenMP is not allowed\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 843, __PRETTY_FUNCTION__))
;
844
845 auto &Ctx = getASTContext();
846 bool IsByRef = true;
847
848 // Find the directive that is associated with the provided scope.
849 auto Ty = D->getType();
850
851 if (DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
852 // This table summarizes how a given variable should be passed to the device
853 // given its type and the clauses where it appears. This table is based on
854 // the description in OpenMP 4.5 [2.10.4, target Construct] and
855 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
856 //
857 // =========================================================================
858 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
859 // | |(tofrom:scalar)| | pvt | | | |
860 // =========================================================================
861 // | scl | | | | - | | bycopy|
862 // | scl | | - | x | - | - | bycopy|
863 // | scl | | x | - | - | - | null |
864 // | scl | x | | | - | | byref |
865 // | scl | x | - | x | - | - | bycopy|
866 // | scl | x | x | - | - | - | null |
867 // | scl | | - | - | - | x | byref |
868 // | scl | x | - | - | - | x | byref |
869 //
870 // | agg | n.a. | | | - | | byref |
871 // | agg | n.a. | - | x | - | - | byref |
872 // | agg | n.a. | x | - | - | - | null |
873 // | agg | n.a. | - | - | - | x | byref |
874 // | agg | n.a. | - | - | - | x[] | byref |
875 //
876 // | ptr | n.a. | | | - | | bycopy|
877 // | ptr | n.a. | - | x | - | - | bycopy|
878 // | ptr | n.a. | x | - | - | - | null |
879 // | ptr | n.a. | - | - | - | x | byref |
880 // | ptr | n.a. | - | - | - | x[] | bycopy|
881 // | ptr | n.a. | - | - | x | | bycopy|
882 // | ptr | n.a. | - | - | x | x | bycopy|
883 // | ptr | n.a. | - | - | x | x[] | bycopy|
884 // =========================================================================
885 // Legend:
886 // scl - scalar
887 // ptr - pointer
888 // agg - aggregate
889 // x - applies
890 // - - invalid in this combination
891 // [] - mapped with an array section
892 // byref - should be mapped by reference
893 // byval - should be mapped by value
894 // null - initialize a local variable to null on the device
895 //
896 // Observations:
897 // - All scalar declarations that show up in a map clause have to be passed
898 // by reference, because they may have been mapped in the enclosing data
899 // environment.
900 // - If the scalar value does not fit the size of uintptr, it has to be
901 // passed by reference, regardless the result in the table above.
902 // - For pointers mapped by value that have either an implicit map or an
903 // array section, the runtime library may pass the NULL value to the
904 // device instead of the value passed to it by the compiler.
905
906 if (Ty->isReferenceType())
907 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
908
909 // Locate map clauses and see if the variable being captured is referred to
910 // in any of those clauses. Here we only care about variables, not fields,
911 // because fields are part of aggregates.
912 bool IsVariableUsedInMapClause = false;
913 bool IsVariableAssociatedWithSection = false;
914
915 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->checkMappableExprComponentListsForDecl(
916 D, /*CurrentRegionOnly=*/true,
917 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
918 MapExprComponents,
919 OpenMPClauseKind WhereFoundClauseKind) {
920 // Only the map clause information influences how a variable is
921 // captured. E.g. is_device_ptr does not require changing the default
922 // behavior.
923 if (WhereFoundClauseKind != OMPC_map)
924 return false;
925
926 auto EI = MapExprComponents.rbegin();
927 auto EE = MapExprComponents.rend();
928
929 assert(EI != EE && "Invalid map expression!")((EI != EE && "Invalid map expression!") ? static_cast
<void> (0) : __assert_fail ("EI != EE && \"Invalid map expression!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 929, __PRETTY_FUNCTION__))
;
930
931 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
932 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
933
934 ++EI;
935 if (EI == EE)
936 return false;
937
938 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
939 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
940 isa<MemberExpr>(EI->getAssociatedExpression())) {
941 IsVariableAssociatedWithSection = true;
942 // There is nothing more we need to know about this variable.
943 return true;
944 }
945
946 // Keep looking for more map info.
947 return false;
948 });
949
950 if (IsVariableUsedInMapClause) {
951 // If variable is identified in a map clause it is always captured by
952 // reference except if it is a pointer that is dereferenced somehow.
953 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
954 } else {
955 // By default, all the data that has a scalar type is mapped by copy.
956 IsByRef = !Ty->isScalarType();
957 }
958 }
959
960 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
961 IsByRef = !DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->hasExplicitDSA(
962 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
963 Level, /*NotLastprivate=*/true);
964 }
965
966 // When passing data by copy, we need to make sure it fits the uintptr size
967 // and alignment, because the runtime library only deals with uintptr types.
968 // If it does not fit the uintptr size, we need to pass the data by reference
969 // instead.
970 if (!IsByRef &&
971 (Ctx.getTypeSizeInChars(Ty) >
972 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
973 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
974 IsByRef = true;
975 }
976
977 return IsByRef;
978}
979
980unsigned Sema::getOpenMPNestingLevel() const {
981 assert(getLangOpts().OpenMP)((getLangOpts().OpenMP) ? static_cast<void> (0) : __assert_fail
("getLangOpts().OpenMP", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 981, __PRETTY_FUNCTION__))
;
982 return DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getNestingLevel();
983}
984
985VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
986 assert(LangOpts.OpenMP && "OpenMP is not allowed")((LangOpts.OpenMP && "OpenMP is not allowed") ? static_cast
<void> (0) : __assert_fail ("LangOpts.OpenMP && \"OpenMP is not allowed\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 986, __PRETTY_FUNCTION__))
;
987 D = getCanonicalDecl(D);
988
989 // If we are attempting to capture a global variable in a directive with
990 // 'target' we return true so that this global is also mapped to the device.
991 //
992 // FIXME: If the declaration is enclosed in a 'declare target' directive,
993 // then it should not be captured. Therefore, an extra check has to be
994 // inserted here once support for 'declare target' is added.
995 //
996 auto *VD = dyn_cast<VarDecl>(D);
997 if (VD && !VD->hasLocalStorage()) {
998 if (DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective() == OMPD_target &&
999 !DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isClauseParsingMode())
1000 return VD;
1001 if (DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->hasDirective(
1002 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1003 SourceLocation) -> bool {
1004 return isOpenMPTargetExecutionDirective(K);
1005 },
1006 false))
1007 return VD;
1008 }
1009
1010 if (DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective() != OMPD_unknown &&
1011 (!DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isClauseParsingMode() ||
1012 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getParentDirective() != OMPD_unknown)) {
1013 auto &&Info = DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isLoopControlVariable(D);
1014 if (Info.first ||
1015 (VD && VD->hasLocalStorage() &&
1016 isParallelOrTaskRegion(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective())) ||
1017 (VD && DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isForceVarCapturing()))
1018 return VD ? VD : Info.second;
1019 auto DVarPrivate = DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getTopDSA(D, DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isClauseParsingMode());
11
Within the expansion of the macro 'DSAStack':
a
Null pointer value stored to 'DVarPrivate.PrivateCopy'
1020 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
12
Assuming the condition is true
13
Taking true branch
1021 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
14
'?' condition is false
15
Called C++ object pointer is null
1022 DVarPrivate = DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->hasDSA(
1023 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1024 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isClauseParsingMode());
1025 if (DVarPrivate.CKind != OMPC_unknown)
1026 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1027 }
1028 return nullptr;
1029}
1030
1031bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
1032 assert(LangOpts.OpenMP && "OpenMP is not allowed")((LangOpts.OpenMP && "OpenMP is not allowed") ? static_cast
<void> (0) : __assert_fail ("LangOpts.OpenMP && \"OpenMP is not allowed\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 1032, __PRETTY_FUNCTION__))
;
1033 return DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->hasExplicitDSA(
1034 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
1035}
1036
1037bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
1038 assert(LangOpts.OpenMP && "OpenMP is not allowed")((LangOpts.OpenMP && "OpenMP is not allowed") ? static_cast
<void> (0) : __assert_fail ("LangOpts.OpenMP && \"OpenMP is not allowed\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 1038, __PRETTY_FUNCTION__))
;
1039 // Return true if the current level is no longer enclosed in a target region.
1040
1041 auto *VD = dyn_cast<VarDecl>(D);
1042 return VD && !VD->hasLocalStorage() &&
1043 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1044 Level);
1045}
1046
1047void Sema::DestroyDataSharingAttributesStack() { delete DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
; }
1048
1049void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1050 const DeclarationNameInfo &DirName,
1051 Scope *CurScope, SourceLocation Loc) {
1052 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->push(DKind, DirName, CurScope, Loc);
1053 PushExpressionEvaluationContext(
1054 ExpressionEvaluationContext::PotentiallyEvaluated);
1055}
1056
1057void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1058 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setClauseParsingMode(K);
1059}
1060
1061void Sema::EndOpenMPClause() {
1062 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setClauseParsingMode(/*K=*/OMPC_unknown);
1063}
1064
1065void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1066 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1067 // A variable of class type (or array thereof) that appears in a lastprivate
1068 // clause requires an accessible, unambiguous default constructor for the
1069 // class type, unless the list item is also specified in a firstprivate
1070 // clause.
1071 if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1072 for (auto *C : D->clauses()) {
1073 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1074 SmallVector<Expr *, 8> PrivateCopies;
1075 for (auto *DE : Clause->varlists()) {
1076 if (DE->isValueDependent() || DE->isTypeDependent()) {
1077 PrivateCopies.push_back(nullptr);
1078 continue;
1079 }
1080 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1081 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1082 QualType Type = VD->getType().getNonReferenceType();
1083 auto DVar = DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getTopDSA(VD, false);
1084 if (DVar.CKind == OMPC_lastprivate) {
1085 // Generate helper private variable and initialize it with the
1086 // default value. The address of the original variable is replaced
1087 // by the address of the new private variable in CodeGen. This new
1088 // variable is not added to IdResolver, so the code in the OpenMP
1089 // region uses original variable for proper diagnostics.
1090 auto *VDPrivate = buildVarDecl(
1091 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1092 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
1093 ActOnUninitializedDecl(VDPrivate);
1094 if (VDPrivate->isInvalidDecl())
1095 continue;
1096 PrivateCopies.push_back(buildDeclRefExpr(
1097 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1098 } else {
1099 // The variable is also a firstprivate, so initialization sequence
1100 // for private copy is generated already.
1101 PrivateCopies.push_back(nullptr);
1102 }
1103 }
1104 // Set initializers to private copies if no errors were found.
1105 if (PrivateCopies.size() == Clause->varlist_size())
1106 Clause->setPrivateCopies(PrivateCopies);
1107 }
1108 }
1109 }
1110
1111 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->pop();
1112 DiscardCleanupsInEvaluationContext();
1113 PopExpressionEvaluationContext();
1114}
1115
1116static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1117 Expr *NumIterations, Sema &SemaRef,
1118 Scope *S, DSAStackTy *Stack);
1119
1120namespace {
1121
1122class VarDeclFilterCCC : public CorrectionCandidateCallback {
1123private:
1124 Sema &SemaRef;
1125
1126public:
1127 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
1128 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1129 NamedDecl *ND = Candidate.getCorrectionDecl();
1130 if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
1131 return VD->hasGlobalStorage() &&
1132 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1133 SemaRef.getCurScope());
1134 }
1135 return false;
1136 }
1137};
1138
1139class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1140private:
1141 Sema &SemaRef;
1142
1143public:
1144 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1145 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1146 NamedDecl *ND = Candidate.getCorrectionDecl();
1147 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1148 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1149 SemaRef.getCurScope());
1150 }
1151 return false;
1152 }
1153};
1154
1155} // namespace
1156
1157ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1158 CXXScopeSpec &ScopeSpec,
1159 const DeclarationNameInfo &Id) {
1160 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1161 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1162
1163 if (Lookup.isAmbiguous())
1164 return ExprError();
1165
1166 VarDecl *VD;
1167 if (!Lookup.isSingleResult()) {
1168 if (TypoCorrection Corrected = CorrectTypo(
1169 Id, LookupOrdinaryName, CurScope, nullptr,
1170 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
1171 diagnoseTypo(Corrected,
1172 PDiag(Lookup.empty()
1173 ? diag::err_undeclared_var_use_suggest
1174 : diag::err_omp_expected_var_arg_suggest)
1175 << Id.getName());
1176 VD = Corrected.getCorrectionDeclAs<VarDecl>();
1177 } else {
1178 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1179 : diag::err_omp_expected_var_arg)
1180 << Id.getName();
1181 return ExprError();
1182 }
1183 } else {
1184 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1185 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1186 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1187 return ExprError();
1188 }
1189 }
1190 Lookup.suppressDiagnostics();
1191
1192 // OpenMP [2.9.2, Syntax, C/C++]
1193 // Variables must be file-scope, namespace-scope, or static block-scope.
1194 if (!VD->hasGlobalStorage()) {
1195 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
1196 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1197 bool IsDecl =
1198 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1199 Diag(VD->getLocation(),
1200 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1201 << VD;
1202 return ExprError();
1203 }
1204
1205 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1206 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
1207 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1208 // A threadprivate directive for file-scope variables must appear outside
1209 // any definition or declaration.
1210 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1211 !getCurLexicalContext()->isTranslationUnit()) {
1212 Diag(Id.getLoc(), diag::err_omp_var_scope)
1213 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1214 bool IsDecl =
1215 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1216 Diag(VD->getLocation(),
1217 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1218 << VD;
1219 return ExprError();
1220 }
1221 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1222 // A threadprivate directive for static class member variables must appear
1223 // in the class definition, in the same scope in which the member
1224 // variables are declared.
1225 if (CanonicalVD->isStaticDataMember() &&
1226 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1227 Diag(Id.getLoc(), diag::err_omp_var_scope)
1228 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1229 bool IsDecl =
1230 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1231 Diag(VD->getLocation(),
1232 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1233 << VD;
1234 return ExprError();
1235 }
1236 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1237 // A threadprivate directive for namespace-scope variables must appear
1238 // outside any definition or declaration other than the namespace
1239 // definition itself.
1240 if (CanonicalVD->getDeclContext()->isNamespace() &&
1241 (!getCurLexicalContext()->isFileContext() ||
1242 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1243 Diag(Id.getLoc(), diag::err_omp_var_scope)
1244 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1245 bool IsDecl =
1246 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1247 Diag(VD->getLocation(),
1248 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1249 << VD;
1250 return ExprError();
1251 }
1252 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1253 // A threadprivate directive for static block-scope variables must appear
1254 // in the scope of the variable and not in a nested scope.
1255 if (CanonicalVD->isStaticLocal() && CurScope &&
1256 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
1257 Diag(Id.getLoc(), diag::err_omp_var_scope)
1258 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1259 bool IsDecl =
1260 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1261 Diag(VD->getLocation(),
1262 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1263 << VD;
1264 return ExprError();
1265 }
1266
1267 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1268 // A threadprivate directive must lexically precede all references to any
1269 // of the variables in its list.
1270 if (VD->isUsed() && !DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isThreadPrivate(VD)) {
1271 Diag(Id.getLoc(), diag::err_omp_var_used)
1272 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1273 return ExprError();
1274 }
1275
1276 QualType ExprType = VD->getType().getNonReferenceType();
1277 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1278 SourceLocation(), VD,
1279 /*RefersToEnclosingVariableOrCapture=*/false,
1280 Id.getLoc(), ExprType, VK_LValue);
1281}
1282
1283Sema::DeclGroupPtrTy
1284Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1285 ArrayRef<Expr *> VarList) {
1286 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
1287 CurContext->addDecl(D);
1288 return DeclGroupPtrTy::make(DeclGroupRef(D));
1289 }
1290 return nullptr;
1291}
1292
1293namespace {
1294class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1295 Sema &SemaRef;
1296
1297public:
1298 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1299 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1300 if (VD->hasLocalStorage()) {
1301 SemaRef.Diag(E->getLocStart(),
1302 diag::err_omp_local_var_in_threadprivate_init)
1303 << E->getSourceRange();
1304 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1305 << VD << VD->getSourceRange();
1306 return true;
1307 }
1308 }
1309 return false;
1310 }
1311 bool VisitStmt(const Stmt *S) {
1312 for (auto Child : S->children()) {
1313 if (Child && Visit(Child))
1314 return true;
1315 }
1316 return false;
1317 }
1318 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
1319};
1320} // namespace
1321
1322OMPThreadPrivateDecl *
1323Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
1324 SmallVector<Expr *, 8> Vars;
1325 for (auto &RefExpr : VarList) {
1326 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
1327 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1328 SourceLocation ILoc = DE->getExprLoc();
1329
1330 // Mark variable as used.
1331 VD->setReferenced();
1332 VD->markUsed(Context);
1333
1334 QualType QType = VD->getType();
1335 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1336 // It will be analyzed later.
1337 Vars.push_back(DE);
1338 continue;
1339 }
1340
1341 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1342 // A threadprivate variable must not have an incomplete type.
1343 if (RequireCompleteType(ILoc, VD->getType(),
1344 diag::err_omp_threadprivate_incomplete_type)) {
1345 continue;
1346 }
1347
1348 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1349 // A threadprivate variable must not have a reference type.
1350 if (VD->getType()->isReferenceType()) {
1351 Diag(ILoc, diag::err_omp_ref_type_arg)
1352 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1353 bool IsDecl =
1354 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1355 Diag(VD->getLocation(),
1356 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1357 << VD;
1358 continue;
1359 }
1360
1361 // Check if this is a TLS variable. If TLS is not being supported, produce
1362 // the corresponding diagnostic.
1363 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1364 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1365 getLangOpts().OpenMPUseTLS &&
1366 getASTContext().getTargetInfo().isTLSSupported())) ||
1367 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1368 !VD->isLocalVarDecl())) {
1369 Diag(ILoc, diag::err_omp_var_thread_local)
1370 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
1371 bool IsDecl =
1372 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1373 Diag(VD->getLocation(),
1374 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1375 << VD;
1376 continue;
1377 }
1378
1379 // Check if initial value of threadprivate variable reference variable with
1380 // local storage (it is not supported by runtime).
1381 if (auto Init = VD->getAnyInitializer()) {
1382 LocalVarRefChecker Checker(*this);
1383 if (Checker.Visit(Init))
1384 continue;
1385 }
1386
1387 Vars.push_back(RefExpr);
1388 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->addDSA(VD, DE, OMPC_threadprivate);
1389 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1390 Context, SourceRange(Loc, Loc)));
1391 if (auto *ML = Context.getASTMutationListener())
1392 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
1393 }
1394 OMPThreadPrivateDecl *D = nullptr;
1395 if (!Vars.empty()) {
1396 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1397 Vars);
1398 D->setAccess(AS_public);
1399 }
1400 return D;
1401}
1402
1403static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1404 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
1405 bool IsLoopIterVar = false) {
1406 if (DVar.RefExpr) {
1407 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1408 << getOpenMPClauseName(DVar.CKind);
1409 return;
1410 }
1411 enum {
1412 PDSA_StaticMemberShared,
1413 PDSA_StaticLocalVarShared,
1414 PDSA_LoopIterVarPrivate,
1415 PDSA_LoopIterVarLinear,
1416 PDSA_LoopIterVarLastprivate,
1417 PDSA_ConstVarShared,
1418 PDSA_GlobalVarShared,
1419 PDSA_TaskVarFirstprivate,
1420 PDSA_LocalVarPrivate,
1421 PDSA_Implicit
1422 } Reason = PDSA_Implicit;
1423 bool ReportHint = false;
1424 auto ReportLoc = D->getLocation();
1425 auto *VD = dyn_cast<VarDecl>(D);
1426 if (IsLoopIterVar) {
1427 if (DVar.CKind == OMPC_private)
1428 Reason = PDSA_LoopIterVarPrivate;
1429 else if (DVar.CKind == OMPC_lastprivate)
1430 Reason = PDSA_LoopIterVarLastprivate;
1431 else
1432 Reason = PDSA_LoopIterVarLinear;
1433 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1434 DVar.CKind == OMPC_firstprivate) {
1435 Reason = PDSA_TaskVarFirstprivate;
1436 ReportLoc = DVar.ImplicitDSALoc;
1437 } else if (VD && VD->isStaticLocal())
1438 Reason = PDSA_StaticLocalVarShared;
1439 else if (VD && VD->isStaticDataMember())
1440 Reason = PDSA_StaticMemberShared;
1441 else if (VD && VD->isFileVarDecl())
1442 Reason = PDSA_GlobalVarShared;
1443 else if (D->getType().isConstant(SemaRef.getASTContext()))
1444 Reason = PDSA_ConstVarShared;
1445 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1446 ReportHint = true;
1447 Reason = PDSA_LocalVarPrivate;
1448 }
1449 if (Reason != PDSA_Implicit) {
1450 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1451 << Reason << ReportHint
1452 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1453 } else if (DVar.ImplicitDSALoc.isValid()) {
1454 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1455 << getOpenMPClauseName(DVar.CKind);
1456 }
1457}
1458
1459namespace {
1460class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1461 DSAStackTy *Stack;
1462 Sema &SemaRef;
1463 bool ErrorFound;
1464 CapturedStmt *CS;
1465 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1466 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
1467
1468public:
1469 void VisitDeclRefExpr(DeclRefExpr *E) {
1470 if (E->isTypeDependent() || E->isValueDependent() ||
1471 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1472 return;
1473 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1474 // Skip internally declared variables.
1475 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1476 return;
1477
1478 auto DVar = Stack->getTopDSA(VD, false);
1479 // Check if the variable has explicit DSA set and stop analysis if it so.
1480 if (DVar.RefExpr)
1481 return;
1482
1483 auto ELoc = E->getExprLoc();
1484 auto DKind = Stack->getCurrentDirective();
1485 // The default(none) clause requires that each variable that is referenced
1486 // in the construct, and does not have a predetermined data-sharing
1487 // attribute, must have its data-sharing attribute explicitly determined
1488 // by being listed in a data-sharing attribute clause.
1489 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1490 isParallelOrTaskRegion(DKind) &&
1491 VarsWithInheritedDSA.count(VD) == 0) {
1492 VarsWithInheritedDSA[VD] = E;
1493 return;
1494 }
1495
1496 // OpenMP [2.9.3.6, Restrictions, p.2]
1497 // A list item that appears in a reduction clause of the innermost
1498 // enclosing worksharing or parallel construct may not be accessed in an
1499 // explicit task.
1500 DVar = Stack->hasInnermostDSA(
1501 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1502 [](OpenMPDirectiveKind K) -> bool {
1503 return isOpenMPParallelDirective(K) ||
1504 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1505 },
1506 false);
1507 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1508 ErrorFound = true;
1509 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1510 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
1511 return;
1512 }
1513
1514 // Define implicit data-sharing attributes for task.
1515 DVar = Stack->getImplicitDSA(VD, false);
1516 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1517 !Stack->isLoopControlVariable(VD).first)
1518 ImplicitFirstprivate.push_back(E);
1519 }
1520 }
1521 void VisitMemberExpr(MemberExpr *E) {
1522 if (E->isTypeDependent() || E->isValueDependent() ||
1523 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1524 return;
1525 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1526 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1527 auto DVar = Stack->getTopDSA(FD, false);
1528 // Check if the variable has explicit DSA set and stop analysis if it
1529 // so.
1530 if (DVar.RefExpr)
1531 return;
1532
1533 auto ELoc = E->getExprLoc();
1534 auto DKind = Stack->getCurrentDirective();
1535 // OpenMP [2.9.3.6, Restrictions, p.2]
1536 // A list item that appears in a reduction clause of the innermost
1537 // enclosing worksharing or parallel construct may not be accessed in
1538 // an explicit task.
1539 DVar = Stack->hasInnermostDSA(
1540 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1541 [](OpenMPDirectiveKind K) -> bool {
1542 return isOpenMPParallelDirective(K) ||
1543 isOpenMPWorksharingDirective(K) ||
1544 isOpenMPTeamsDirective(K);
1545 },
1546 false);
1547 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1548 ErrorFound = true;
1549 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1550 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1551 return;
1552 }
1553
1554 // Define implicit data-sharing attributes for task.
1555 DVar = Stack->getImplicitDSA(FD, false);
1556 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1557 !Stack->isLoopControlVariable(FD).first)
1558 ImplicitFirstprivate.push_back(E);
1559 }
1560 } else
1561 Visit(E->getBase());
1562 }
1563 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
1564 for (auto *C : S->clauses()) {
1565 // Skip analysis of arguments of implicitly defined firstprivate clause
1566 // for task directives.
1567 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1568 for (auto *CC : C->children()) {
1569 if (CC)
1570 Visit(CC);
1571 }
1572 }
1573 }
1574 void VisitStmt(Stmt *S) {
1575 for (auto *C : S->children()) {
1576 if (C && !isa<OMPExecutableDirective>(C))
1577 Visit(C);
1578 }
1579 }
1580
1581 bool isErrorFound() { return ErrorFound; }
1582 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
1583 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
1584 return VarsWithInheritedDSA;
1585 }
1586
1587 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1588 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
1589};
1590} // namespace
1591
1592void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
1593 switch (DKind) {
1594 case OMPD_parallel:
1595 case OMPD_parallel_for:
1596 case OMPD_parallel_for_simd:
1597 case OMPD_parallel_sections:
1598 case OMPD_teams: {
1599 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1600 QualType KmpInt32PtrTy =
1601 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1602 Sema::CapturedParamNameType Params[] = {
1603 std::make_pair(".global_tid.", KmpInt32PtrTy),
1604 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1605 std::make_pair(StringRef(), QualType()) // __context with shared vars
1606 };
1607 ActOnCapturedRegionStart(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getConstructLoc(), CurScope, CR_OpenMP,
1608 Params);
1609 break;
1610 }
1611 case OMPD_target_teams:
1612 case OMPD_target_parallel: {
1613 Sema::CapturedParamNameType ParamsTarget[] = {
1614 std::make_pair(StringRef(), QualType()) // __context with shared vars
1615 };
1616 // Start a captured region for 'target' with no implicit parameters.
1617 ActOnCapturedRegionStart(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getConstructLoc(), CurScope, CR_OpenMP,
1618 ParamsTarget);
1619 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1620 QualType KmpInt32PtrTy =
1621 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1622 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
1623 std::make_pair(".global_tid.", KmpInt32PtrTy),
1624 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1625 std::make_pair(StringRef(), QualType()) // __context with shared vars
1626 };
1627 // Start a captured region for 'teams' or 'parallel'. Both regions have
1628 // the same implicit parameters.
1629 ActOnCapturedRegionStart(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getConstructLoc(), CurScope, CR_OpenMP,
1630 ParamsTeamsOrParallel);
1631 break;
1632 }
1633 case OMPD_simd:
1634 case OMPD_for:
1635 case OMPD_for_simd:
1636 case OMPD_sections:
1637 case OMPD_section:
1638 case OMPD_single:
1639 case OMPD_master:
1640 case OMPD_critical:
1641 case OMPD_taskgroup:
1642 case OMPD_distribute:
1643 case OMPD_ordered:
1644 case OMPD_atomic:
1645 case OMPD_target_data:
1646 case OMPD_target:
1647 case OMPD_target_parallel_for:
1648 case OMPD_target_parallel_for_simd:
1649 case OMPD_target_simd: {
1650 Sema::CapturedParamNameType Params[] = {
1651 std::make_pair(StringRef(), QualType()) // __context with shared vars
1652 };
1653 ActOnCapturedRegionStart(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getConstructLoc(), CurScope, CR_OpenMP,
1654 Params);
1655 break;
1656 }
1657 case OMPD_task: {
1658 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1659 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1660 FunctionProtoType::ExtProtoInfo EPI;
1661 EPI.Variadic = true;
1662 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1663 Sema::CapturedParamNameType Params[] = {
1664 std::make_pair(".global_tid.", KmpInt32Ty),
1665 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1666 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1667 std::make_pair(".copy_fn.",
1668 Context.getPointerType(CopyFnType).withConst()),
1669 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1670 std::make_pair(StringRef(), QualType()) // __context with shared vars
1671 };
1672 ActOnCapturedRegionStart(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getConstructLoc(), CurScope, CR_OpenMP,
1673 Params);
1674 // Mark this captured region as inlined, because we don't use outlined
1675 // function directly.
1676 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1677 AlwaysInlineAttr::CreateImplicit(
1678 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1679 break;
1680 }
1681 case OMPD_taskloop:
1682 case OMPD_taskloop_simd: {
1683 QualType KmpInt32Ty =
1684 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1685 QualType KmpUInt64Ty =
1686 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1687 QualType KmpInt64Ty =
1688 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1689 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1690 FunctionProtoType::ExtProtoInfo EPI;
1691 EPI.Variadic = true;
1692 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1693 Sema::CapturedParamNameType Params[] = {
1694 std::make_pair(".global_tid.", KmpInt32Ty),
1695 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1696 std::make_pair(".privates.",
1697 Context.VoidPtrTy.withConst().withRestrict()),
1698 std::make_pair(
1699 ".copy_fn.",
1700 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1701 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1702 std::make_pair(".lb.", KmpUInt64Ty),
1703 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1704 std::make_pair(".liter.", KmpInt32Ty),
1705 std::make_pair(StringRef(), QualType()) // __context with shared vars
1706 };
1707 ActOnCapturedRegionStart(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getConstructLoc(), CurScope, CR_OpenMP,
1708 Params);
1709 // Mark this captured region as inlined, because we don't use outlined
1710 // function directly.
1711 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1712 AlwaysInlineAttr::CreateImplicit(
1713 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1714 break;
1715 }
1716 case OMPD_distribute_parallel_for_simd:
1717 case OMPD_distribute_simd:
1718 case OMPD_distribute_parallel_for:
1719 case OMPD_teams_distribute:
1720 case OMPD_teams_distribute_simd:
1721 case OMPD_teams_distribute_parallel_for_simd:
1722 case OMPD_teams_distribute_parallel_for:
1723 case OMPD_target_teams_distribute:
1724 case OMPD_target_teams_distribute_parallel_for:
1725 case OMPD_target_teams_distribute_parallel_for_simd:
1726 case OMPD_target_teams_distribute_simd: {
1727 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1728 QualType KmpInt32PtrTy =
1729 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1730 Sema::CapturedParamNameType Params[] = {
1731 std::make_pair(".global_tid.", KmpInt32PtrTy),
1732 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1733 std::make_pair(".previous.lb.", Context.getSizeType()),
1734 std::make_pair(".previous.ub.", Context.getSizeType()),
1735 std::make_pair(StringRef(), QualType()) // __context with shared vars
1736 };
1737 ActOnCapturedRegionStart(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getConstructLoc(), CurScope, CR_OpenMP,
1738 Params);
1739 break;
1740 }
1741 case OMPD_threadprivate:
1742 case OMPD_taskyield:
1743 case OMPD_barrier:
1744 case OMPD_taskwait:
1745 case OMPD_cancellation_point:
1746 case OMPD_cancel:
1747 case OMPD_flush:
1748 case OMPD_target_enter_data:
1749 case OMPD_target_exit_data:
1750 case OMPD_declare_reduction:
1751 case OMPD_declare_simd:
1752 case OMPD_declare_target:
1753 case OMPD_end_declare_target:
1754 case OMPD_target_update:
1755 llvm_unreachable("OpenMP Directive is not allowed")::llvm::llvm_unreachable_internal("OpenMP Directive is not allowed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 1755)
;
1756 case OMPD_unknown:
1757 llvm_unreachable("Unknown OpenMP directive")::llvm::llvm_unreachable_internal("Unknown OpenMP directive",
"/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 1757)
;
1758 }
1759}
1760
1761int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
1762 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
1763 getOpenMPCaptureRegions(CaptureRegions, DKind);
1764 return CaptureRegions.size();
1765}
1766
1767static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
1768 Expr *CaptureExpr, bool WithInit,
1769 bool AsExpression) {
1770 assert(CaptureExpr)((CaptureExpr) ? static_cast<void> (0) : __assert_fail (
"CaptureExpr", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 1770, __PRETTY_FUNCTION__))
;
1771 ASTContext &C = S.getASTContext();
1772 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
1773 QualType Ty = Init->getType();
1774 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1775 if (S.getLangOpts().CPlusPlus)
1776 Ty = C.getLValueReferenceType(Ty);
1777 else {
1778 Ty = C.getPointerType(Ty);
1779 ExprResult Res =
1780 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1781 if (!Res.isUsable())
1782 return nullptr;
1783 Init = Res.get();
1784 }
1785 WithInit = true;
1786 }
1787 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
1788 CaptureExpr->getLocStart());
1789 if (!WithInit)
1790 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
1791 S.CurContext->addHiddenDecl(CED);
1792 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
1793 return CED;
1794}
1795
1796static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1797 bool WithInit) {
1798 OMPCapturedExprDecl *CD;
1799 if (auto *VD = S.IsOpenMPCapturedDecl(D))
10
Calling 'Sema::IsOpenMPCapturedDecl'
1800 CD = cast<OMPCapturedExprDecl>(VD);
1801 else
1802 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1803 /*AsExpression=*/false);
1804 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1805 CaptureExpr->getExprLoc());
1806}
1807
1808static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1809 if (!Ref) {
1810 auto *CD =
1811 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1812 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1813 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1814 CaptureExpr->getExprLoc());
1815 }
1816 ExprResult Res = Ref;
1817 if (!S.getLangOpts().CPlusPlus &&
1818 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1819 Ref->getType()->isPointerType())
1820 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1821 if (!Res.isUsable())
1822 return ExprError();
1823 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
1824}
1825
1826namespace {
1827// OpenMP directives parsed in this section are represented as a
1828// CapturedStatement with an associated statement. If a syntax error
1829// is detected during the parsing of the associated statement, the
1830// compiler must abort processing and close the CapturedStatement.
1831//
1832// Combined directives such as 'target parallel' have more than one
1833// nested CapturedStatements. This RAII ensures that we unwind out
1834// of all the nested CapturedStatements when an error is found.
1835class CaptureRegionUnwinderRAII {
1836private:
1837 Sema &S;
1838 bool &ErrorFound;
1839 OpenMPDirectiveKind DKind;
1840
1841public:
1842 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
1843 OpenMPDirectiveKind DKind)
1844 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
1845 ~CaptureRegionUnwinderRAII() {
1846 if (ErrorFound) {
1847 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
1848 while (--ThisCaptureLevel >= 0)
1849 S.ActOnCapturedRegionError();
1850 }
1851 }
1852};
1853} // namespace
1854
1855StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1856 ArrayRef<OMPClause *> Clauses) {
1857 bool ErrorFound = false;
1858 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
1859 *this, ErrorFound, DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective());
1860 if (!S.isUsable()) {
1861 ErrorFound = true;
1862 return StmtError();
1863 }
1864
1865 OMPOrderedClause *OC = nullptr;
1866 OMPScheduleClause *SC = nullptr;
1867 SmallVector<OMPLinearClause *, 4> LCs;
1868 SmallVector<OMPClauseWithPreInit *, 8> PICs;
1869 // This is required for proper codegen.
1870 for (auto *Clause : Clauses) {
1871 if (isOpenMPPrivate(Clause->getClauseKind()) ||
1872 Clause->getClauseKind() == OMPC_copyprivate ||
1873 (getLangOpts().OpenMPUseTLS &&
1874 getASTContext().getTargetInfo().isTLSSupported() &&
1875 Clause->getClauseKind() == OMPC_copyin)) {
1876 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
1877 // Mark all variables in private list clauses as used in inner region.
1878 for (auto *VarRef : Clause->children()) {
1879 if (auto *E = cast_or_null<Expr>(VarRef)) {
1880 MarkDeclarationsReferencedInExpr(E);
1881 }
1882 }
1883 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setForceVarCapturing(/*V=*/false);
1884 } else if (isParallelOrTaskRegion(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective())) {
1885 if (auto *C = OMPClauseWithPreInit::get(Clause))
1886 PICs.push_back(C);
1887 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1888 if (auto *E = C->getPostUpdateExpr())
1889 MarkDeclarationsReferencedInExpr(E);
1890 }
1891 }
1892 if (Clause->getClauseKind() == OMPC_schedule)
1893 SC = cast<OMPScheduleClause>(Clause);
1894 else if (Clause->getClauseKind() == OMPC_ordered)
1895 OC = cast<OMPOrderedClause>(Clause);
1896 else if (Clause->getClauseKind() == OMPC_linear)
1897 LCs.push_back(cast<OMPLinearClause>(Clause));
1898 }
1899 // OpenMP, 2.7.1 Loop Construct, Restrictions
1900 // The nonmonotonic modifier cannot be specified if an ordered clause is
1901 // specified.
1902 if (SC &&
1903 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1904 SC->getSecondScheduleModifier() ==
1905 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1906 OC) {
1907 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1908 ? SC->getFirstScheduleModifierLoc()
1909 : SC->getSecondScheduleModifierLoc(),
1910 diag::err_omp_schedule_nonmonotonic_ordered)
1911 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1912 ErrorFound = true;
1913 }
1914 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1915 for (auto *C : LCs) {
1916 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1917 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1918 }
1919 ErrorFound = true;
1920 }
1921 if (isOpenMPWorksharingDirective(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective()) &&
1922 isOpenMPSimdDirective(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective()) && OC &&
1923 OC->getNumForLoops()) {
1924 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1925 << getOpenMPDirectiveName(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective());
1926 ErrorFound = true;
1927 }
1928 if (ErrorFound) {
1929 return StmtError();
1930 }
1931 StmtResult SR = S;
1932 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
1933 getOpenMPCaptureRegions(CaptureRegions, DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective());
1934 for (auto ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
1935 // Mark all variables in private list clauses as used in inner region.
1936 // Required for proper codegen of combined directives.
1937 // TODO: add processing for other clauses.
1938 if (isParallelOrTaskRegion(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective())) {
1939 for (auto *C : PICs) {
1940 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
1941 // Find the particular capture region for the clause if the
1942 // directive is a combined one with multiple capture regions.
1943 // If the directive is not a combined one, the capture region
1944 // associated with the clause is OMPD_unknown and is generated
1945 // only once.
1946 if (CaptureRegion == ThisCaptureRegion ||
1947 CaptureRegion == OMPD_unknown) {
1948 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1949 for (auto *D : DS->decls())
1950 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1951 }
1952 }
1953 }
1954 }
1955 SR = ActOnCapturedRegionEnd(SR.get());
1956 }
1957 return SR;
1958}
1959
1960static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
1961 OpenMPDirectiveKind CancelRegion,
1962 SourceLocation StartLoc) {
1963 // CancelRegion is only needed for cancel and cancellation_point.
1964 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
1965 return false;
1966
1967 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
1968 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
1969 return false;
1970
1971 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
1972 << getOpenMPDirectiveName(CancelRegion);
1973 return true;
1974}
1975
1976static bool checkNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1977 OpenMPDirectiveKind CurrentRegion,
1978 const DeclarationNameInfo &CurrentName,
1979 OpenMPDirectiveKind CancelRegion,
1980 SourceLocation StartLoc) {
1981 if (Stack->getCurScope()) {
1982 auto ParentRegion = Stack->getParentDirective();
1983 auto OffendingRegion = ParentRegion;
1984 bool NestingProhibited = false;
1985 bool CloseNesting = true;
1986 bool OrphanSeen = false;
1987 enum {
1988 NoRecommend,
1989 ShouldBeInParallelRegion,
1990 ShouldBeInOrderedRegion,
1991 ShouldBeInTargetRegion,
1992 ShouldBeInTeamsRegion
1993 } Recommend = NoRecommend;
1994 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
1995 // OpenMP [2.16, Nesting of Regions]
1996 // OpenMP constructs may not be nested inside a simd region.
1997 // OpenMP [2.8.1,simd Construct, Restrictions]
1998 // An ordered construct with the simd clause is the only OpenMP
1999 // construct that can appear in the simd region.
2000 // Allowing a SIMD construct nested in another SIMD construct is an
2001 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2002 // message.
2003 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2004 ? diag::err_omp_prohibited_region_simd
2005 : diag::warn_omp_nesting_simd);
2006 return CurrentRegion != OMPD_simd;
2007 }
2008 if (ParentRegion == OMPD_atomic) {
2009 // OpenMP [2.16, Nesting of Regions]
2010 // OpenMP constructs may not be nested inside an atomic region.
2011 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2012 return true;
2013 }
2014 if (CurrentRegion == OMPD_section) {
2015 // OpenMP [2.7.2, sections Construct, Restrictions]
2016 // Orphaned section directives are prohibited. That is, the section
2017 // directives must appear within the sections construct and must not be
2018 // encountered elsewhere in the sections region.
2019 if (ParentRegion != OMPD_sections &&
2020 ParentRegion != OMPD_parallel_sections) {
2021 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2022 << (ParentRegion != OMPD_unknown)
2023 << getOpenMPDirectiveName(ParentRegion);
2024 return true;
2025 }
2026 return false;
2027 }
2028 // Allow some constructs (except teams) to be orphaned (they could be
2029 // used in functions, called from OpenMP regions with the required
2030 // preconditions).
2031 if (ParentRegion == OMPD_unknown &&
2032 !isOpenMPNestingTeamsDirective(CurrentRegion))
2033 return false;
2034 if (CurrentRegion == OMPD_cancellation_point ||
2035 CurrentRegion == OMPD_cancel) {
2036 // OpenMP [2.16, Nesting of Regions]
2037 // A cancellation point construct for which construct-type-clause is
2038 // taskgroup must be nested inside a task construct. A cancellation
2039 // point construct for which construct-type-clause is not taskgroup must
2040 // be closely nested inside an OpenMP construct that matches the type
2041 // specified in construct-type-clause.
2042 // A cancel construct for which construct-type-clause is taskgroup must be
2043 // nested inside a task construct. A cancel construct for which
2044 // construct-type-clause is not taskgroup must be closely nested inside an
2045 // OpenMP construct that matches the type specified in
2046 // construct-type-clause.
2047 NestingProhibited =
2048 !((CancelRegion == OMPD_parallel &&
2049 (ParentRegion == OMPD_parallel ||
2050 ParentRegion == OMPD_target_parallel)) ||
2051 (CancelRegion == OMPD_for &&
2052 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2053 ParentRegion == OMPD_target_parallel_for)) ||
2054 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2055 (CancelRegion == OMPD_sections &&
2056 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2057 ParentRegion == OMPD_parallel_sections)));
2058 } else if (CurrentRegion == OMPD_master) {
2059 // OpenMP [2.16, Nesting of Regions]
2060 // A master region may not be closely nested inside a worksharing,
2061 // atomic, or explicit task region.
2062 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2063 isOpenMPTaskingDirective(ParentRegion);
2064 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2065 // OpenMP [2.16, Nesting of Regions]
2066 // A critical region may not be nested (closely or otherwise) inside a
2067 // critical region with the same name. Note that this restriction is not
2068 // sufficient to prevent deadlock.
2069 SourceLocation PreviousCriticalLoc;
2070 bool DeadLock = Stack->hasDirective(
2071 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2072 const DeclarationNameInfo &DNI,
2073 SourceLocation Loc) -> bool {
2074 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2075 PreviousCriticalLoc = Loc;
2076 return true;
2077 } else
2078 return false;
2079 },
2080 false /* skip top directive */);
2081 if (DeadLock) {
2082 SemaRef.Diag(StartLoc,
2083 diag::err_omp_prohibited_region_critical_same_name)
2084 << CurrentName.getName();
2085 if (PreviousCriticalLoc.isValid())
2086 SemaRef.Diag(PreviousCriticalLoc,
2087 diag::note_omp_previous_critical_region);
2088 return true;
2089 }
2090 } else if (CurrentRegion == OMPD_barrier) {
2091 // OpenMP [2.16, Nesting of Regions]
2092 // A barrier region may not be closely nested inside a worksharing,
2093 // explicit task, critical, ordered, atomic, or master region.
2094 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2095 isOpenMPTaskingDirective(ParentRegion) ||
2096 ParentRegion == OMPD_master ||
2097 ParentRegion == OMPD_critical ||
2098 ParentRegion == OMPD_ordered;
2099 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
2100 !isOpenMPParallelDirective(CurrentRegion) &&
2101 !isOpenMPTeamsDirective(CurrentRegion)) {
2102 // OpenMP [2.16, Nesting of Regions]
2103 // A worksharing region may not be closely nested inside a worksharing,
2104 // explicit task, critical, ordered, atomic, or master region.
2105 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2106 isOpenMPTaskingDirective(ParentRegion) ||
2107 ParentRegion == OMPD_master ||
2108 ParentRegion == OMPD_critical ||
2109 ParentRegion == OMPD_ordered;
2110 Recommend = ShouldBeInParallelRegion;
2111 } else if (CurrentRegion == OMPD_ordered) {
2112 // OpenMP [2.16, Nesting of Regions]
2113 // An ordered region may not be closely nested inside a critical,
2114 // atomic, or explicit task region.
2115 // An ordered region must be closely nested inside a loop region (or
2116 // parallel loop region) with an ordered clause.
2117 // OpenMP [2.8.1,simd Construct, Restrictions]
2118 // An ordered construct with the simd clause is the only OpenMP construct
2119 // that can appear in the simd region.
2120 NestingProhibited = ParentRegion == OMPD_critical ||
2121 isOpenMPTaskingDirective(ParentRegion) ||
2122 !(isOpenMPSimdDirective(ParentRegion) ||
2123 Stack->isParentOrderedRegion());
2124 Recommend = ShouldBeInOrderedRegion;
2125 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
2126 // OpenMP [2.16, Nesting of Regions]
2127 // If specified, a teams construct must be contained within a target
2128 // construct.
2129 NestingProhibited = ParentRegion != OMPD_target;
2130 OrphanSeen = ParentRegion == OMPD_unknown;
2131 Recommend = ShouldBeInTargetRegion;
2132 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2133 }
2134 if (!NestingProhibited &&
2135 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2136 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2137 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
2138 // OpenMP [2.16, Nesting of Regions]
2139 // distribute, parallel, parallel sections, parallel workshare, and the
2140 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2141 // constructs that can be closely nested in the teams region.
2142 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2143 !isOpenMPDistributeDirective(CurrentRegion);
2144 Recommend = ShouldBeInParallelRegion;
2145 }
2146 if (!NestingProhibited &&
2147 isOpenMPNestingDistributeDirective(CurrentRegion)) {
2148 // OpenMP 4.5 [2.17 Nesting of Regions]
2149 // The region associated with the distribute construct must be strictly
2150 // nested inside a teams region
2151 NestingProhibited =
2152 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
2153 Recommend = ShouldBeInTeamsRegion;
2154 }
2155 if (!NestingProhibited &&
2156 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2157 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2158 // OpenMP 4.5 [2.17 Nesting of Regions]
2159 // If a target, target update, target data, target enter data, or
2160 // target exit data construct is encountered during execution of a
2161 // target region, the behavior is unspecified.
2162 NestingProhibited = Stack->hasDirective(
2163 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2164 SourceLocation) -> bool {
2165 if (isOpenMPTargetExecutionDirective(K)) {
2166 OffendingRegion = K;
2167 return true;
2168 } else
2169 return false;
2170 },
2171 false /* don't skip top directive */);
2172 CloseNesting = false;
2173 }
2174 if (NestingProhibited) {
2175 if (OrphanSeen) {
2176 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2177 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2178 } else {
2179 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2180 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2181 << Recommend << getOpenMPDirectiveName(CurrentRegion);
2182 }
2183 return true;
2184 }
2185 }
2186 return false;
2187}
2188
2189static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2190 ArrayRef<OMPClause *> Clauses,
2191 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2192 bool ErrorFound = false;
2193 unsigned NamedModifiersNumber = 0;
2194 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2195 OMPD_unknown + 1);
2196 SmallVector<SourceLocation, 4> NameModifierLoc;
2197 for (const auto *C : Clauses) {
2198 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2199 // At most one if clause without a directive-name-modifier can appear on
2200 // the directive.
2201 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2202 if (FoundNameModifiers[CurNM]) {
2203 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2204 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2205 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2206 ErrorFound = true;
2207 } else if (CurNM != OMPD_unknown) {
2208 NameModifierLoc.push_back(IC->getNameModifierLoc());
2209 ++NamedModifiersNumber;
2210 }
2211 FoundNameModifiers[CurNM] = IC;
2212 if (CurNM == OMPD_unknown)
2213 continue;
2214 // Check if the specified name modifier is allowed for the current
2215 // directive.
2216 // At most one if clause with the particular directive-name-modifier can
2217 // appear on the directive.
2218 bool MatchFound = false;
2219 for (auto NM : AllowedNameModifiers) {
2220 if (CurNM == NM) {
2221 MatchFound = true;
2222 break;
2223 }
2224 }
2225 if (!MatchFound) {
2226 S.Diag(IC->getNameModifierLoc(),
2227 diag::err_omp_wrong_if_directive_name_modifier)
2228 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2229 ErrorFound = true;
2230 }
2231 }
2232 }
2233 // If any if clause on the directive includes a directive-name-modifier then
2234 // all if clauses on the directive must include a directive-name-modifier.
2235 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2236 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2237 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2238 diag::err_omp_no_more_if_clause);
2239 } else {
2240 std::string Values;
2241 std::string Sep(", ");
2242 unsigned AllowedCnt = 0;
2243 unsigned TotalAllowedNum =
2244 AllowedNameModifiers.size() - NamedModifiersNumber;
2245 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2246 ++Cnt) {
2247 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2248 if (!FoundNameModifiers[NM]) {
2249 Values += "'";
2250 Values += getOpenMPDirectiveName(NM);
2251 Values += "'";
2252 if (AllowedCnt + 2 == TotalAllowedNum)
2253 Values += " or ";
2254 else if (AllowedCnt + 1 != TotalAllowedNum)
2255 Values += Sep;
2256 ++AllowedCnt;
2257 }
2258 }
2259 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2260 diag::err_omp_unnamed_if_clause)
2261 << (TotalAllowedNum > 1) << Values;
2262 }
2263 for (auto Loc : NameModifierLoc) {
2264 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2265 }
2266 ErrorFound = true;
2267 }
2268 return ErrorFound;
2269}
2270
2271StmtResult Sema::ActOnOpenMPExecutableDirective(
2272 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2273 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2274 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
2275 StmtResult Res = StmtError();
2276 // First check CancelRegion which is then used in checkNestingOfRegions.
2277 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
2278 checkNestingOfRegions(*this, DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
, Kind, DirName, CancelRegion,
2279 StartLoc))
2280 return StmtError();
2281
2282 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
2283 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
2284 bool ErrorFound = false;
2285 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
2286 if (AStmt) {
2287 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2287, __PRETTY_FUNCTION__))
;
2288
2289 // Check default data sharing attributes for referenced variables.
2290 DSAAttrChecker DSAChecker(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
, *this, cast<CapturedStmt>(AStmt));
2291 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
2292 Stmt *S = AStmt;
2293 while (--ThisCaptureLevel >= 0)
2294 S = cast<CapturedStmt>(S)->getCapturedStmt();
2295 DSAChecker.Visit(S);
2296 if (DSAChecker.isErrorFound())
2297 return StmtError();
2298 // Generate list of implicitly defined firstprivate variables.
2299 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
2300
2301 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2302 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2303 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2304 SourceLocation(), SourceLocation())) {
2305 ClausesWithImplicit.push_back(Implicit);
2306 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2307 DSAChecker.getImplicitFirstprivate().size();
2308 } else
2309 ErrorFound = true;
2310 }
2311 }
2312
2313 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
2314 switch (Kind) {
2315 case OMPD_parallel:
2316 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2317 EndLoc);
2318 AllowedNameModifiers.push_back(OMPD_parallel);
2319 break;
2320 case OMPD_simd:
2321 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2322 VarsWithInheritedDSA);
2323 break;
2324 case OMPD_for:
2325 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2326 VarsWithInheritedDSA);
2327 break;
2328 case OMPD_for_simd:
2329 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2330 EndLoc, VarsWithInheritedDSA);
2331 break;
2332 case OMPD_sections:
2333 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2334 EndLoc);
2335 break;
2336 case OMPD_section:
2337 assert(ClausesWithImplicit.empty() &&((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp section' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp section' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2338, __PRETTY_FUNCTION__))
2338 "No clauses are allowed for 'omp section' directive")((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp section' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp section' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2338, __PRETTY_FUNCTION__))
;
2339 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2340 break;
2341 case OMPD_single:
2342 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2343 EndLoc);
2344 break;
2345 case OMPD_master:
2346 assert(ClausesWithImplicit.empty() &&((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp master' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp master' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2347, __PRETTY_FUNCTION__))
2347 "No clauses are allowed for 'omp master' directive")((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp master' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp master' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2347, __PRETTY_FUNCTION__))
;
2348 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2349 break;
2350 case OMPD_critical:
2351 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2352 StartLoc, EndLoc);
2353 break;
2354 case OMPD_parallel_for:
2355 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2356 EndLoc, VarsWithInheritedDSA);
2357 AllowedNameModifiers.push_back(OMPD_parallel);
2358 break;
2359 case OMPD_parallel_for_simd:
2360 Res = ActOnOpenMPParallelForSimdDirective(
2361 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2362 AllowedNameModifiers.push_back(OMPD_parallel);
2363 break;
2364 case OMPD_parallel_sections:
2365 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2366 StartLoc, EndLoc);
2367 AllowedNameModifiers.push_back(OMPD_parallel);
2368 break;
2369 case OMPD_task:
2370 Res =
2371 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2372 AllowedNameModifiers.push_back(OMPD_task);
2373 break;
2374 case OMPD_taskyield:
2375 assert(ClausesWithImplicit.empty() &&((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp taskyield' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp taskyield' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2376, __PRETTY_FUNCTION__))
2376 "No clauses are allowed for 'omp taskyield' directive")((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp taskyield' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp taskyield' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2376, __PRETTY_FUNCTION__))
;
2377 assert(AStmt == nullptr &&((AStmt == nullptr && "No associated statement allowed for 'omp taskyield' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp taskyield' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2378, __PRETTY_FUNCTION__))
2378 "No associated statement allowed for 'omp taskyield' directive")((AStmt == nullptr && "No associated statement allowed for 'omp taskyield' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp taskyield' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2378, __PRETTY_FUNCTION__))
;
2379 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2380 break;
2381 case OMPD_barrier:
2382 assert(ClausesWithImplicit.empty() &&((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp barrier' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp barrier' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2383, __PRETTY_FUNCTION__))
2383 "No clauses are allowed for 'omp barrier' directive")((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp barrier' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp barrier' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2383, __PRETTY_FUNCTION__))
;
2384 assert(AStmt == nullptr &&((AStmt == nullptr && "No associated statement allowed for 'omp barrier' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp barrier' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2385, __PRETTY_FUNCTION__))
2385 "No associated statement allowed for 'omp barrier' directive")((AStmt == nullptr && "No associated statement allowed for 'omp barrier' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp barrier' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2385, __PRETTY_FUNCTION__))
;
2386 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2387 break;
2388 case OMPD_taskwait:
2389 assert(ClausesWithImplicit.empty() &&((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp taskwait' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp taskwait' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2390, __PRETTY_FUNCTION__))
2390 "No clauses are allowed for 'omp taskwait' directive")((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp taskwait' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp taskwait' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2390, __PRETTY_FUNCTION__))
;
2391 assert(AStmt == nullptr &&((AStmt == nullptr && "No associated statement allowed for 'omp taskwait' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp taskwait' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2392, __PRETTY_FUNCTION__))
2392 "No associated statement allowed for 'omp taskwait' directive")((AStmt == nullptr && "No associated statement allowed for 'omp taskwait' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp taskwait' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2392, __PRETTY_FUNCTION__))
;
2393 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2394 break;
2395 case OMPD_taskgroup:
2396 assert(ClausesWithImplicit.empty() &&((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp taskgroup' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp taskgroup' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2397, __PRETTY_FUNCTION__))
2397 "No clauses are allowed for 'omp taskgroup' directive")((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp taskgroup' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp taskgroup' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2397, __PRETTY_FUNCTION__))
;
2398 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2399 break;
2400 case OMPD_flush:
2401 assert(AStmt == nullptr &&((AStmt == nullptr && "No associated statement allowed for 'omp flush' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp flush' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2402, __PRETTY_FUNCTION__))
2402 "No associated statement allowed for 'omp flush' directive")((AStmt == nullptr && "No associated statement allowed for 'omp flush' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp flush' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2402, __PRETTY_FUNCTION__))
;
2403 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2404 break;
2405 case OMPD_ordered:
2406 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2407 EndLoc);
2408 break;
2409 case OMPD_atomic:
2410 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2411 EndLoc);
2412 break;
2413 case OMPD_teams:
2414 Res =
2415 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2416 break;
2417 case OMPD_target:
2418 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2419 EndLoc);
2420 AllowedNameModifiers.push_back(OMPD_target);
2421 break;
2422 case OMPD_target_parallel:
2423 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
2424 StartLoc, EndLoc);
2425 AllowedNameModifiers.push_back(OMPD_target);
2426 AllowedNameModifiers.push_back(OMPD_parallel);
2427 break;
2428 case OMPD_target_parallel_for:
2429 Res = ActOnOpenMPTargetParallelForDirective(
2430 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2431 AllowedNameModifiers.push_back(OMPD_target);
2432 AllowedNameModifiers.push_back(OMPD_parallel);
2433 break;
2434 case OMPD_cancellation_point:
2435 assert(ClausesWithImplicit.empty() &&((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp cancellation point' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp cancellation point' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2436, __PRETTY_FUNCTION__))
2436 "No clauses are allowed for 'omp cancellation point' directive")((ClausesWithImplicit.empty() && "No clauses are allowed for 'omp cancellation point' directive"
) ? static_cast<void> (0) : __assert_fail ("ClausesWithImplicit.empty() && \"No clauses are allowed for 'omp cancellation point' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2436, __PRETTY_FUNCTION__))
;
2437 assert(AStmt == nullptr && "No associated statement allowed for 'omp "((AStmt == nullptr && "No associated statement allowed for 'omp "
"cancellation point' directive") ? static_cast<void> (
0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp \" \"cancellation point' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2438, __PRETTY_FUNCTION__))
2438 "cancellation point' directive")((AStmt == nullptr && "No associated statement allowed for 'omp "
"cancellation point' directive") ? static_cast<void> (
0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp \" \"cancellation point' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2438, __PRETTY_FUNCTION__))
;
2439 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2440 break;
2441 case OMPD_cancel:
2442 assert(AStmt == nullptr &&((AStmt == nullptr && "No associated statement allowed for 'omp cancel' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp cancel' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2443, __PRETTY_FUNCTION__))
2443 "No associated statement allowed for 'omp cancel' directive")((AStmt == nullptr && "No associated statement allowed for 'omp cancel' directive"
) ? static_cast<void> (0) : __assert_fail ("AStmt == nullptr && \"No associated statement allowed for 'omp cancel' directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2443, __PRETTY_FUNCTION__))
;
2444 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2445 CancelRegion);
2446 AllowedNameModifiers.push_back(OMPD_cancel);
2447 break;
2448 case OMPD_target_data:
2449 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2450 EndLoc);
2451 AllowedNameModifiers.push_back(OMPD_target_data);
2452 break;
2453 case OMPD_target_enter_data:
2454 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2455 EndLoc);
2456 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2457 break;
2458 case OMPD_target_exit_data:
2459 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2460 EndLoc);
2461 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2462 break;
2463 case OMPD_taskloop:
2464 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2465 EndLoc, VarsWithInheritedDSA);
2466 AllowedNameModifiers.push_back(OMPD_taskloop);
2467 break;
2468 case OMPD_taskloop_simd:
2469 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2470 EndLoc, VarsWithInheritedDSA);
2471 AllowedNameModifiers.push_back(OMPD_taskloop);
2472 break;
2473 case OMPD_distribute:
2474 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2475 EndLoc, VarsWithInheritedDSA);
2476 break;
2477 case OMPD_target_update:
2478 assert(!AStmt && "Statement is not allowed for target update")((!AStmt && "Statement is not allowed for target update"
) ? static_cast<void> (0) : __assert_fail ("!AStmt && \"Statement is not allowed for target update\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2478, __PRETTY_FUNCTION__))
;
2479 Res =
2480 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
2481 AllowedNameModifiers.push_back(OMPD_target_update);
2482 break;
2483 case OMPD_distribute_parallel_for:
2484 Res = ActOnOpenMPDistributeParallelForDirective(
2485 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2486 AllowedNameModifiers.push_back(OMPD_parallel);
2487 break;
2488 case OMPD_distribute_parallel_for_simd:
2489 Res = ActOnOpenMPDistributeParallelForSimdDirective(
2490 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2491 AllowedNameModifiers.push_back(OMPD_parallel);
2492 break;
2493 case OMPD_distribute_simd:
2494 Res = ActOnOpenMPDistributeSimdDirective(
2495 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2496 break;
2497 case OMPD_target_parallel_for_simd:
2498 Res = ActOnOpenMPTargetParallelForSimdDirective(
2499 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2500 AllowedNameModifiers.push_back(OMPD_target);
2501 AllowedNameModifiers.push_back(OMPD_parallel);
2502 break;
2503 case OMPD_target_simd:
2504 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2505 EndLoc, VarsWithInheritedDSA);
2506 AllowedNameModifiers.push_back(OMPD_target);
2507 break;
2508 case OMPD_teams_distribute:
2509 Res = ActOnOpenMPTeamsDistributeDirective(
2510 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2511 break;
2512 case OMPD_teams_distribute_simd:
2513 Res = ActOnOpenMPTeamsDistributeSimdDirective(
2514 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2515 break;
2516 case OMPD_teams_distribute_parallel_for_simd:
2517 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
2518 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2519 AllowedNameModifiers.push_back(OMPD_parallel);
2520 break;
2521 case OMPD_teams_distribute_parallel_for:
2522 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
2523 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2524 AllowedNameModifiers.push_back(OMPD_parallel);
2525 break;
2526 case OMPD_target_teams:
2527 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
2528 EndLoc);
2529 AllowedNameModifiers.push_back(OMPD_target);
2530 break;
2531 case OMPD_target_teams_distribute:
2532 Res = ActOnOpenMPTargetTeamsDistributeDirective(
2533 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2534 AllowedNameModifiers.push_back(OMPD_target);
2535 break;
2536 case OMPD_target_teams_distribute_parallel_for:
2537 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
2538 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2539 AllowedNameModifiers.push_back(OMPD_target);
2540 AllowedNameModifiers.push_back(OMPD_parallel);
2541 break;
2542 case OMPD_target_teams_distribute_parallel_for_simd:
2543 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
2544 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2545 AllowedNameModifiers.push_back(OMPD_target);
2546 AllowedNameModifiers.push_back(OMPD_parallel);
2547 break;
2548 case OMPD_target_teams_distribute_simd:
2549 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
2550 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2551 AllowedNameModifiers.push_back(OMPD_target);
2552 break;
2553 case OMPD_declare_target:
2554 case OMPD_end_declare_target:
2555 case OMPD_threadprivate:
2556 case OMPD_declare_reduction:
2557 case OMPD_declare_simd:
2558 llvm_unreachable("OpenMP Directive is not allowed")::llvm::llvm_unreachable_internal("OpenMP Directive is not allowed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2558)
;
2559 case OMPD_unknown:
2560 llvm_unreachable("Unknown OpenMP directive")::llvm::llvm_unreachable_internal("Unknown OpenMP directive",
"/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2560)
;
2561 }
2562
2563 for (auto P : VarsWithInheritedDSA) {
2564 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2565 << P.first << P.second->getSourceRange();
2566 }
2567 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2568
2569 if (!AllowedNameModifiers.empty())
2570 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2571 ErrorFound;
2572
2573 if (ErrorFound)
2574 return StmtError();
2575 return Res;
2576}
2577
2578Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
2579 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
2580 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
2581 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
2582 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
2583 assert(Aligneds.size() == Alignments.size())((Aligneds.size() == Alignments.size()) ? static_cast<void
> (0) : __assert_fail ("Aligneds.size() == Alignments.size()"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2583, __PRETTY_FUNCTION__))
;
2584 assert(Linears.size() == LinModifiers.size())((Linears.size() == LinModifiers.size()) ? static_cast<void
> (0) : __assert_fail ("Linears.size() == LinModifiers.size()"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2584, __PRETTY_FUNCTION__))
;
2585 assert(Linears.size() == Steps.size())((Linears.size() == Steps.size()) ? static_cast<void> (
0) : __assert_fail ("Linears.size() == Steps.size()", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2585, __PRETTY_FUNCTION__))
;
2586 if (!DG || DG.get().isNull())
2587 return DeclGroupPtrTy();
2588
2589 if (!DG.get().isSingleDecl()) {
2590 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
2591 return DG;
2592 }
2593 auto *ADecl = DG.get().getSingleDecl();
2594 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
2595 ADecl = FTD->getTemplatedDecl();
2596
2597 auto *FD = dyn_cast<FunctionDecl>(ADecl);
2598 if (!FD) {
2599 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
2600 return DeclGroupPtrTy();
2601 }
2602
2603 // OpenMP [2.8.2, declare simd construct, Description]
2604 // The parameter of the simdlen clause must be a constant positive integer
2605 // expression.
2606 ExprResult SL;
2607 if (Simdlen)
2608 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
2609 // OpenMP [2.8.2, declare simd construct, Description]
2610 // The special this pointer can be used as if was one of the arguments to the
2611 // function in any of the linear, aligned, or uniform clauses.
2612 // The uniform clause declares one or more arguments to have an invariant
2613 // value for all concurrent invocations of the function in the execution of a
2614 // single SIMD loop.
2615 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
2616 Expr *UniformedLinearThis = nullptr;
2617 for (auto *E : Uniforms) {
2618 E = E->IgnoreParenImpCasts();
2619 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
2620 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
2621 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
2622 FD->getParamDecl(PVD->getFunctionScopeIndex())
2623 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
2624 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
2625 continue;
2626 }
2627 if (isa<CXXThisExpr>(E)) {
2628 UniformedLinearThis = E;
2629 continue;
2630 }
2631 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
2632 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
2633 }
2634 // OpenMP [2.8.2, declare simd construct, Description]
2635 // The aligned clause declares that the object to which each list item points
2636 // is aligned to the number of bytes expressed in the optional parameter of
2637 // the aligned clause.
2638 // The special this pointer can be used as if was one of the arguments to the
2639 // function in any of the linear, aligned, or uniform clauses.
2640 // The type of list items appearing in the aligned clause must be array,
2641 // pointer, reference to array, or reference to pointer.
2642 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
2643 Expr *AlignedThis = nullptr;
2644 for (auto *E : Aligneds) {
2645 E = E->IgnoreParenImpCasts();
2646 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
2647 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2648 auto *CanonPVD = PVD->getCanonicalDecl();
2649 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
2650 FD->getParamDecl(PVD->getFunctionScopeIndex())
2651 ->getCanonicalDecl() == CanonPVD) {
2652 // OpenMP [2.8.1, simd construct, Restrictions]
2653 // A list-item cannot appear in more than one aligned clause.
2654 if (AlignedArgs.count(CanonPVD) > 0) {
2655 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
2656 << 1 << E->getSourceRange();
2657 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
2658 diag::note_omp_explicit_dsa)
2659 << getOpenMPClauseName(OMPC_aligned);
2660 continue;
2661 }
2662 AlignedArgs[CanonPVD] = E;
2663 QualType QTy = PVD->getType()
2664 .getNonReferenceType()
2665 .getUnqualifiedType()
2666 .getCanonicalType();
2667 const Type *Ty = QTy.getTypePtrOrNull();
2668 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
2669 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
2670 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
2671 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
2672 }
2673 continue;
2674 }
2675 }
2676 if (isa<CXXThisExpr>(E)) {
2677 if (AlignedThis) {
2678 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
2679 << 2 << E->getSourceRange();
2680 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
2681 << getOpenMPClauseName(OMPC_aligned);
2682 }
2683 AlignedThis = E;
2684 continue;
2685 }
2686 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
2687 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
2688 }
2689 // The optional parameter of the aligned clause, alignment, must be a constant
2690 // positive integer expression. If no optional parameter is specified,
2691 // implementation-defined default alignments for SIMD instructions on the
2692 // target platforms are assumed.
2693 SmallVector<Expr *, 4> NewAligns;
2694 for (auto *E : Alignments) {
2695 ExprResult Align;
2696 if (E)
2697 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
2698 NewAligns.push_back(Align.get());
2699 }
2700 // OpenMP [2.8.2, declare simd construct, Description]
2701 // The linear clause declares one or more list items to be private to a SIMD
2702 // lane and to have a linear relationship with respect to the iteration space
2703 // of a loop.
2704 // The special this pointer can be used as if was one of the arguments to the
2705 // function in any of the linear, aligned, or uniform clauses.
2706 // When a linear-step expression is specified in a linear clause it must be
2707 // either a constant integer expression or an integer-typed parameter that is
2708 // specified in a uniform clause on the directive.
2709 llvm::DenseMap<Decl *, Expr *> LinearArgs;
2710 const bool IsUniformedThis = UniformedLinearThis != nullptr;
2711 auto MI = LinModifiers.begin();
2712 for (auto *E : Linears) {
2713 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
2714 ++MI;
2715 E = E->IgnoreParenImpCasts();
2716 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
2717 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2718 auto *CanonPVD = PVD->getCanonicalDecl();
2719 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
2720 FD->getParamDecl(PVD->getFunctionScopeIndex())
2721 ->getCanonicalDecl() == CanonPVD) {
2722 // OpenMP [2.15.3.7, linear Clause, Restrictions]
2723 // A list-item cannot appear in more than one linear clause.
2724 if (LinearArgs.count(CanonPVD) > 0) {
2725 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
2726 << getOpenMPClauseName(OMPC_linear)
2727 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
2728 Diag(LinearArgs[CanonPVD]->getExprLoc(),
2729 diag::note_omp_explicit_dsa)
2730 << getOpenMPClauseName(OMPC_linear);
2731 continue;
2732 }
2733 // Each argument can appear in at most one uniform or linear clause.
2734 if (UniformedArgs.count(CanonPVD) > 0) {
2735 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
2736 << getOpenMPClauseName(OMPC_linear)
2737 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
2738 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
2739 diag::note_omp_explicit_dsa)
2740 << getOpenMPClauseName(OMPC_uniform);
2741 continue;
2742 }
2743 LinearArgs[CanonPVD] = E;
2744 if (E->isValueDependent() || E->isTypeDependent() ||
2745 E->isInstantiationDependent() ||
2746 E->containsUnexpandedParameterPack())
2747 continue;
2748 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
2749 PVD->getOriginalType());
2750 continue;
2751 }
2752 }
2753 if (isa<CXXThisExpr>(E)) {
2754 if (UniformedLinearThis) {
2755 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
2756 << getOpenMPClauseName(OMPC_linear)
2757 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
2758 << E->getSourceRange();
2759 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
2760 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
2761 : OMPC_linear);
2762 continue;
2763 }
2764 UniformedLinearThis = E;
2765 if (E->isValueDependent() || E->isTypeDependent() ||
2766 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
2767 continue;
2768 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
2769 E->getType());
2770 continue;
2771 }
2772 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
2773 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
2774 }
2775 Expr *Step = nullptr;
2776 Expr *NewStep = nullptr;
2777 SmallVector<Expr *, 4> NewSteps;
2778 for (auto *E : Steps) {
2779 // Skip the same step expression, it was checked already.
2780 if (Step == E || !E) {
2781 NewSteps.push_back(E ? NewStep : nullptr);
2782 continue;
2783 }
2784 Step = E;
2785 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
2786 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2787 auto *CanonPVD = PVD->getCanonicalDecl();
2788 if (UniformedArgs.count(CanonPVD) == 0) {
2789 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
2790 << Step->getSourceRange();
2791 } else if (E->isValueDependent() || E->isTypeDependent() ||
2792 E->isInstantiationDependent() ||
2793 E->containsUnexpandedParameterPack() ||
2794 CanonPVD->getType()->hasIntegerRepresentation())
2795 NewSteps.push_back(Step);
2796 else {
2797 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
2798 << Step->getSourceRange();
2799 }
2800 continue;
2801 }
2802 NewStep = Step;
2803 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2804 !Step->isInstantiationDependent() &&
2805 !Step->containsUnexpandedParameterPack()) {
2806 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
2807 .get();
2808 if (NewStep)
2809 NewStep = VerifyIntegerConstantExpression(NewStep).get();
2810 }
2811 NewSteps.push_back(NewStep);
2812 }
2813 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
2814 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
2815 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
2816 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
2817 const_cast<Expr **>(Linears.data()), Linears.size(),
2818 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
2819 NewSteps.data(), NewSteps.size(), SR);
2820 ADecl->addAttr(NewAttr);
2821 return ConvertDeclToDeclGroup(ADecl);
2822}
2823
2824StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2825 Stmt *AStmt,
2826 SourceLocation StartLoc,
2827 SourceLocation EndLoc) {
2828 if (!AStmt)
2829 return StmtError();
2830
2831 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2832 // 1.2.2 OpenMP Language Terminology
2833 // Structured block - An executable statement with a single entry at the
2834 // top and a single exit at the bottom.
2835 // The point of exit cannot be a branch out of the structured block.
2836 // longjmp() and throw() must not violate the entry/exit criteria.
2837 CS->getCapturedDecl()->setNothrow();
2838
2839 getCurFunction()->setHasBranchProtectedScope();
2840
2841 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2842 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
2843}
2844
2845namespace {
2846/// \brief Helper class for checking canonical form of the OpenMP loops and
2847/// extracting iteration space of each loop in the loop nest, that will be used
2848/// for IR generation.
2849class OpenMPIterationSpaceChecker {
2850 /// \brief Reference to Sema.
2851 Sema &SemaRef;
2852 /// \brief A location for diagnostics (when there is no some better location).
2853 SourceLocation DefaultLoc;
2854 /// \brief A location for diagnostics (when increment is not compatible).
2855 SourceLocation ConditionLoc;
2856 /// \brief A source location for referring to loop init later.
2857 SourceRange InitSrcRange;
2858 /// \brief A source location for referring to condition later.
2859 SourceRange ConditionSrcRange;
2860 /// \brief A source location for referring to increment later.
2861 SourceRange IncrementSrcRange;
2862 /// \brief Loop variable.
2863 ValueDecl *LCDecl = nullptr;
2864 /// \brief Reference to loop variable.
2865 Expr *LCRef = nullptr;
2866 /// \brief Lower bound (initializer for the var).
2867 Expr *LB = nullptr;
2868 /// \brief Upper bound.
2869 Expr *UB = nullptr;
2870 /// \brief Loop step (increment).
2871 Expr *Step = nullptr;
2872 /// \brief This flag is true when condition is one of:
2873 /// Var < UB
2874 /// Var <= UB
2875 /// UB > Var
2876 /// UB >= Var
2877 bool TestIsLessOp = false;
2878 /// \brief This flag is true when condition is strict ( < or > ).
2879 bool TestIsStrictOp = false;
2880 /// \brief This flag is true when step is subtracted on each iteration.
2881 bool SubtractStep = false;
2882
2883public:
2884 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2885 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
2886 /// \brief Check init-expr for canonical loop form and save loop counter
2887 /// variable - #Var and its initialization value - #LB.
2888 bool CheckInit(Stmt *S, bool EmitDiags = true);
2889 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2890 /// for less/greater and for strict/non-strict comparison.
2891 bool CheckCond(Expr *S);
2892 /// \brief Check incr-expr for canonical loop form and return true if it
2893 /// does not conform, otherwise save loop step (#Step).
2894 bool CheckInc(Expr *S);
2895 /// \brief Return the loop counter variable.
2896 ValueDecl *GetLoopDecl() const { return LCDecl; }
2897 /// \brief Return the reference expression to loop counter variable.
2898 Expr *GetLoopDeclRefExpr() const { return LCRef; }
2899 /// \brief Source range of the loop init.
2900 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2901 /// \brief Source range of the loop condition.
2902 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2903 /// \brief Source range of the loop increment.
2904 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2905 /// \brief True if the step should be subtracted.
2906 bool ShouldSubtractStep() const { return SubtractStep; }
2907 /// \brief Build the expression to calculate the number of iterations.
2908 Expr *
2909 BuildNumIterations(Scope *S, const bool LimitedType,
2910 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
2911 /// \brief Build the precondition expression for the loops.
2912 Expr *BuildPreCond(Scope *S, Expr *Cond,
2913 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
2914 /// \brief Build reference expression to the counter be used for codegen.
2915 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
2916 DSAStackTy &DSA) const;
2917 /// \brief Build reference expression to the private counter be used for
2918 /// codegen.
2919 Expr *BuildPrivateCounterVar() const;
2920 /// \brief Build initialization of the counter be used for codegen.
2921 Expr *BuildCounterInit() const;
2922 /// \brief Build step of the counter be used for codegen.
2923 Expr *BuildCounterStep() const;
2924 /// \brief Return true if any expression is dependent.
2925 bool Dependent() const;
2926
2927private:
2928 /// \brief Check the right-hand side of an assignment in the increment
2929 /// expression.
2930 bool CheckIncRHS(Expr *RHS);
2931 /// \brief Helper to set loop counter variable and its initializer.
2932 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
2933 /// \brief Helper to set upper bound.
2934 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
2935 SourceLocation SL);
2936 /// \brief Helper to set loop increment.
2937 bool SetStep(Expr *NewStep, bool Subtract);
2938};
2939
2940bool OpenMPIterationSpaceChecker::Dependent() const {
2941 if (!LCDecl) {
2942 assert(!LB && !UB && !Step)((!LB && !UB && !Step) ? static_cast<void>
(0) : __assert_fail ("!LB && !UB && !Step", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2942, __PRETTY_FUNCTION__))
;
2943 return false;
2944 }
2945 return LCDecl->getType()->isDependentType() ||
2946 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
2947 (Step && Step->isValueDependent());
2948}
2949
2950static Expr *getExprAsWritten(Expr *E) {
2951 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2952 E = ExprTemp->getSubExpr();
2953
2954 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2955 E = MTE->GetTemporaryExpr();
2956
2957 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2958 E = Binder->getSubExpr();
2959
2960 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2961 E = ICE->getSubExprAsWritten();
2962 return E->IgnoreParens();
2963}
2964
2965bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
2966 Expr *NewLCRefExpr,
2967 Expr *NewLB) {
2968 // State consistency checking to ensure correct usage.
2969 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&((LCDecl == nullptr && LB == nullptr && LCRef
== nullptr && UB == nullptr && Step == nullptr
&& !TestIsLessOp && !TestIsStrictOp) ? static_cast
<void> (0) : __assert_fail ("LCDecl == nullptr && LB == nullptr && LCRef == nullptr && UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2970, __PRETTY_FUNCTION__))
2970 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp)((LCDecl == nullptr && LB == nullptr && LCRef
== nullptr && UB == nullptr && Step == nullptr
&& !TestIsLessOp && !TestIsStrictOp) ? static_cast
<void> (0) : __assert_fail ("LCDecl == nullptr && LB == nullptr && LCRef == nullptr && UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2970, __PRETTY_FUNCTION__))
;
2971 if (!NewLCDecl || !NewLB)
2972 return true;
2973 LCDecl = getCanonicalDecl(NewLCDecl);
2974 LCRef = NewLCRefExpr;
2975 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2976 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2977 if ((Ctor->isCopyOrMoveConstructor() ||
2978 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2979 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
2980 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
2981 LB = NewLB;
2982 return false;
2983}
2984
2985bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2986 SourceRange SR, SourceLocation SL) {
2987 // State consistency checking to ensure correct usage.
2988 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&((LCDecl != nullptr && LB != nullptr && UB ==
nullptr && Step == nullptr && !TestIsLessOp &&
!TestIsStrictOp) ? static_cast<void> (0) : __assert_fail
("LCDecl != nullptr && LB != nullptr && UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2989, __PRETTY_FUNCTION__))
2989 Step == nullptr && !TestIsLessOp && !TestIsStrictOp)((LCDecl != nullptr && LB != nullptr && UB ==
nullptr && Step == nullptr && !TestIsLessOp &&
!TestIsStrictOp) ? static_cast<void> (0) : __assert_fail
("LCDecl != nullptr && LB != nullptr && UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 2989, __PRETTY_FUNCTION__))
;
2990 if (!NewUB)
2991 return true;
2992 UB = NewUB;
2993 TestIsLessOp = LessOp;
2994 TestIsStrictOp = StrictOp;
2995 ConditionSrcRange = SR;
2996 ConditionLoc = SL;
2997 return false;
2998}
2999
3000bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3001 // State consistency checking to ensure correct usage.
3002 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr)((LCDecl != nullptr && LB != nullptr && Step ==
nullptr) ? static_cast<void> (0) : __assert_fail ("LCDecl != nullptr && LB != nullptr && Step == nullptr"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 3002, __PRETTY_FUNCTION__))
;
3003 if (!NewStep)
3004 return true;
3005 if (!NewStep->isValueDependent()) {
3006 // Check that the step is integer expression.
3007 SourceLocation StepLoc = NewStep->getLocStart();
3008 ExprResult Val =
3009 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3010 if (Val.isInvalid())
3011 return true;
3012 NewStep = Val.get();
3013
3014 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3015 // If test-expr is of form var relational-op b and relational-op is < or
3016 // <= then incr-expr must cause var to increase on each iteration of the
3017 // loop. If test-expr is of form var relational-op b and relational-op is
3018 // > or >= then incr-expr must cause var to decrease on each iteration of
3019 // the loop.
3020 // If test-expr is of form b relational-op var and relational-op is < or
3021 // <= then incr-expr must cause var to decrease on each iteration of the
3022 // loop. If test-expr is of form b relational-op var and relational-op is
3023 // > or >= then incr-expr must cause var to increase on each iteration of
3024 // the loop.
3025 llvm::APSInt Result;
3026 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3027 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3028 bool IsConstNeg =
3029 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
3030 bool IsConstPos =
3031 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
3032 bool IsConstZero = IsConstant && !Result.getBoolValue();
3033 if (UB && (IsConstZero ||
3034 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
3035 : (IsConstPos || (IsUnsigned && !Subtract))))) {
3036 SemaRef.Diag(NewStep->getExprLoc(),
3037 diag::err_omp_loop_incr_not_compatible)
3038 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
3039 SemaRef.Diag(ConditionLoc,
3040 diag::note_omp_loop_cond_requres_compatible_incr)
3041 << TestIsLessOp << ConditionSrcRange;
3042 return true;
3043 }
3044 if (TestIsLessOp == Subtract) {
3045 NewStep =
3046 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3047 .get();
3048 Subtract = !Subtract;
3049 }
3050 }
3051
3052 Step = NewStep;
3053 SubtractStep = Subtract;
3054 return false;
3055}
3056
3057bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
3058 // Check init-expr for canonical loop form and save loop counter
3059 // variable - #Var and its initialization value - #LB.
3060 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3061 // var = lb
3062 // integer-type var = lb
3063 // random-access-iterator-type var = lb
3064 // pointer-type var = lb
3065 //
3066 if (!S) {
3067 if (EmitDiags) {
3068 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3069 }
3070 return true;
3071 }
3072 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3073 if (!ExprTemp->cleanupsHaveSideEffects())
3074 S = ExprTemp->getSubExpr();
3075
3076 InitSrcRange = S->getSourceRange();
3077 if (Expr *E = dyn_cast<Expr>(S))
3078 S = E->IgnoreParens();
3079 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3080 if (BO->getOpcode() == BO_Assign) {
3081 auto *LHS = BO->getLHS()->IgnoreParens();
3082 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3083 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3084 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3085 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3086 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3087 }
3088 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3089 if (ME->isArrow() &&
3090 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3091 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3092 }
3093 }
3094 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
3095 if (DS->isSingleDecl()) {
3096 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
3097 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
3098 // Accept non-canonical init form here but emit ext. warning.
3099 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
3100 SemaRef.Diag(S->getLocStart(),
3101 diag::ext_omp_loop_not_canonical_init)
3102 << S->getSourceRange();
3103 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
3104 }
3105 }
3106 }
3107 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3108 if (CE->getOperator() == OO_Equal) {
3109 auto *LHS = CE->getArg(0);
3110 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3111 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3112 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3113 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3114 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3115 }
3116 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3117 if (ME->isArrow() &&
3118 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3119 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3120 }
3121 }
3122 }
3123
3124 if (Dependent() || SemaRef.CurContext->isDependentContext())
3125 return false;
3126 if (EmitDiags) {
3127 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3128 << S->getSourceRange();
3129 }
3130 return true;
3131}
3132
3133/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
3134/// variable (which may be the loop variable) if possible.
3135static const ValueDecl *GetInitLCDecl(Expr *E) {
3136 if (!E)
3137 return nullptr;
3138 E = getExprAsWritten(E);
3139 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3140 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3141 if ((Ctor->isCopyOrMoveConstructor() ||
3142 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3143 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3144 E = CE->getArg(0)->IgnoreParenImpCasts();
3145 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3146 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3147 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3148 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3149 return getCanonicalDecl(ME->getMemberDecl());
3150 return getCanonicalDecl(VD);
3151 }
3152 }
3153 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3154 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3155 return getCanonicalDecl(ME->getMemberDecl());
3156 return nullptr;
3157}
3158
3159bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3160 // Check test-expr for canonical form, save upper-bound UB, flags for
3161 // less/greater and for strict/non-strict comparison.
3162 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3163 // var relational-op b
3164 // b relational-op var
3165 //
3166 if (!S) {
3167 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
3168 return true;
3169 }
3170 S = getExprAsWritten(S);
3171 SourceLocation CondLoc = S->getLocStart();
3172 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3173 if (BO->isRelationalOp()) {
3174 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3175 return SetUB(BO->getRHS(),
3176 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3177 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3178 BO->getSourceRange(), BO->getOperatorLoc());
3179 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
3180 return SetUB(BO->getLHS(),
3181 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3182 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3183 BO->getSourceRange(), BO->getOperatorLoc());
3184 }
3185 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3186 if (CE->getNumArgs() == 2) {
3187 auto Op = CE->getOperator();
3188 switch (Op) {
3189 case OO_Greater:
3190 case OO_GreaterEqual:
3191 case OO_Less:
3192 case OO_LessEqual:
3193 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3194 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3195 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3196 CE->getOperatorLoc());
3197 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
3198 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3199 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3200 CE->getOperatorLoc());
3201 break;
3202 default:
3203 break;
3204 }
3205 }
3206 }
3207 if (Dependent() || SemaRef.CurContext->isDependentContext())
3208 return false;
3209 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3210 << S->getSourceRange() << LCDecl;
3211 return true;
3212}
3213
3214bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3215 // RHS of canonical loop form increment can be:
3216 // var + incr
3217 // incr + var
3218 // var - incr
3219 //
3220 RHS = RHS->IgnoreParenImpCasts();
3221 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
3222 if (BO->isAdditiveOp()) {
3223 bool IsAdd = BO->getOpcode() == BO_Add;
3224 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3225 return SetStep(BO->getRHS(), !IsAdd);
3226 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
3227 return SetStep(BO->getLHS(), false);
3228 }
3229 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3230 bool IsAdd = CE->getOperator() == OO_Plus;
3231 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3232 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3233 return SetStep(CE->getArg(1), !IsAdd);
3234 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
3235 return SetStep(CE->getArg(0), false);
3236 }
3237 }
3238 if (Dependent() || SemaRef.CurContext->isDependentContext())
3239 return false;
3240 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3241 << RHS->getSourceRange() << LCDecl;
3242 return true;
3243}
3244
3245bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3246 // Check incr-expr for canonical loop form and return true if it
3247 // does not conform.
3248 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3249 // ++var
3250 // var++
3251 // --var
3252 // var--
3253 // var += incr
3254 // var -= incr
3255 // var = var + incr
3256 // var = incr + var
3257 // var = var - incr
3258 //
3259 if (!S) {
3260 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
3261 return true;
3262 }
3263 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3264 if (!ExprTemp->cleanupsHaveSideEffects())
3265 S = ExprTemp->getSubExpr();
3266
3267 IncrementSrcRange = S->getSourceRange();
3268 S = S->IgnoreParens();
3269 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
3270 if (UO->isIncrementDecrementOp() &&
3271 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
3272 return SetStep(SemaRef
3273 .ActOnIntegerConstant(UO->getLocStart(),
3274 (UO->isDecrementOp() ? -1 : 1))
3275 .get(),
3276 false);
3277 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3278 switch (BO->getOpcode()) {
3279 case BO_AddAssign:
3280 case BO_SubAssign:
3281 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3282 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3283 break;
3284 case BO_Assign:
3285 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3286 return CheckIncRHS(BO->getRHS());
3287 break;
3288 default:
3289 break;
3290 }
3291 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3292 switch (CE->getOperator()) {
3293 case OO_PlusPlus:
3294 case OO_MinusMinus:
3295 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3296 return SetStep(SemaRef
3297 .ActOnIntegerConstant(
3298 CE->getLocStart(),
3299 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
3300 .get(),
3301 false);
3302 break;
3303 case OO_PlusEqual:
3304 case OO_MinusEqual:
3305 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3306 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3307 break;
3308 case OO_Equal:
3309 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3310 return CheckIncRHS(CE->getArg(1));
3311 break;
3312 default:
3313 break;
3314 }
3315 }
3316 if (Dependent() || SemaRef.CurContext->isDependentContext())
3317 return false;
3318 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3319 << S->getSourceRange() << LCDecl;
3320 return true;
3321}
3322
3323static ExprResult
3324tryBuildCapture(Sema &SemaRef, Expr *Capture,
3325 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3326 if (SemaRef.CurContext->isDependentContext())
3327 return ExprResult(Capture);
3328 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3329 return SemaRef.PerformImplicitConversion(
3330 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3331 /*AllowExplicit=*/true);
3332 auto I = Captures.find(Capture);
3333 if (I != Captures.end())
3334 return buildCapture(SemaRef, Capture, I->second);
3335 DeclRefExpr *Ref = nullptr;
3336 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3337 Captures[Capture] = Ref;
3338 return Res;
3339}
3340
3341/// \brief Build the expression to calculate the number of iterations.
3342Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3343 Scope *S, const bool LimitedType,
3344 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
3345 ExprResult Diff;
3346 auto VarType = LCDecl->getType().getNonReferenceType();
3347 if (VarType->isIntegerType() || VarType->isPointerType() ||
3348 SemaRef.getLangOpts().CPlusPlus) {
3349 // Upper - Lower
3350 auto *UBExpr = TestIsLessOp ? UB : LB;
3351 auto *LBExpr = TestIsLessOp ? LB : UB;
3352 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3353 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
3354 if (!Upper || !Lower)
3355 return nullptr;
3356
3357 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3358
3359 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
3360 // BuildBinOp already emitted error, this one is to point user to upper
3361 // and lower bound, and to tell what is passed to 'operator-'.
3362 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3363 << Upper->getSourceRange() << Lower->getSourceRange();
3364 return nullptr;
3365 }
3366 }
3367
3368 if (!Diff.isUsable())
3369 return nullptr;
3370
3371 // Upper - Lower [- 1]
3372 if (TestIsStrictOp)
3373 Diff = SemaRef.BuildBinOp(
3374 S, DefaultLoc, BO_Sub, Diff.get(),
3375 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3376 if (!Diff.isUsable())
3377 return nullptr;
3378
3379 // Upper - Lower [- 1] + Step
3380 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3381 if (!NewStep.isUsable())
3382 return nullptr;
3383 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
3384 if (!Diff.isUsable())
3385 return nullptr;
3386
3387 // Parentheses (for dumping/debugging purposes only).
3388 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3389 if (!Diff.isUsable())
3390 return nullptr;
3391
3392 // (Upper - Lower [- 1] + Step) / Step
3393 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
3394 if (!Diff.isUsable())
3395 return nullptr;
3396
3397 // OpenMP runtime requires 32-bit or 64-bit loop variables.
3398 QualType Type = Diff.get()->getType();
3399 auto &C = SemaRef.Context;
3400 bool UseVarType = VarType->hasIntegerRepresentation() &&
3401 C.getTypeSize(Type) > C.getTypeSize(VarType);
3402 if (!Type->isIntegerType() || UseVarType) {
3403 unsigned NewSize =
3404 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3405 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3406 : Type->hasSignedIntegerRepresentation();
3407 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3408 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3409 Diff = SemaRef.PerformImplicitConversion(
3410 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3411 if (!Diff.isUsable())
3412 return nullptr;
3413 }
3414 }
3415 if (LimitedType) {
3416 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3417 if (NewSize != C.getTypeSize(Type)) {
3418 if (NewSize < C.getTypeSize(Type)) {
3419 assert(NewSize == 64 && "incorrect loop var size")((NewSize == 64 && "incorrect loop var size") ? static_cast
<void> (0) : __assert_fail ("NewSize == 64 && \"incorrect loop var size\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 3419, __PRETTY_FUNCTION__))
;
3420 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3421 << InitSrcRange << ConditionSrcRange;
3422 }
3423 QualType NewType = C.getIntTypeForBitwidth(
3424 NewSize, Type->hasSignedIntegerRepresentation() ||
3425 C.getTypeSize(Type) < NewSize);
3426 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3427 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3428 Sema::AA_Converting, true);
3429 if (!Diff.isUsable())
3430 return nullptr;
3431 }
3432 }
3433 }
3434
3435 return Diff.get();
3436}
3437
3438Expr *OpenMPIterationSpaceChecker::BuildPreCond(
3439 Scope *S, Expr *Cond,
3440 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
3441 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3442 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3443 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
3444
3445 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
3446 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
3447 if (!NewLB.isUsable() || !NewUB.isUsable())
3448 return nullptr;
3449
3450 auto CondExpr = SemaRef.BuildBinOp(
3451 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3452 : (TestIsStrictOp ? BO_GT : BO_GE),
3453 NewLB.get(), NewUB.get());
3454 if (CondExpr.isUsable()) {
3455 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
3456 SemaRef.Context.BoolTy))
3457 CondExpr = SemaRef.PerformImplicitConversion(
3458 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3459 /*AllowExplicit=*/true);
3460 }
3461 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3462 // Otherwise use original loop conditon and evaluate it in runtime.
3463 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3464}
3465
3466/// \brief Build reference expression to the counter be used for codegen.
3467DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
3468 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
3469 auto *VD = dyn_cast<VarDecl>(LCDecl);
3470 if (!VD) {
3471 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
3472 auto *Ref = buildDeclRefExpr(
3473 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
3474 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
3475 // If the loop control decl is explicitly marked as private, do not mark it
3476 // as captured again.
3477 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
3478 Captures.insert(std::make_pair(LCRef, Ref));
3479 return Ref;
3480 }
3481 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
3482 DefaultLoc);
3483}
3484
3485Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3486 if (LCDecl && !LCDecl->isInvalidDecl()) {
3487 auto Type = LCDecl->getType().getNonReferenceType();
3488 auto *PrivateVar =
3489 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
3490 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
3491 if (PrivateVar->isInvalidDecl())
3492 return nullptr;
3493 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3494 }
3495 return nullptr;
3496}
3497
3498/// \brief Build initialization of the counter to be used for codegen.
3499Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3500
3501/// \brief Build step of the counter be used for codegen.
3502Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3503
3504/// \brief Iteration space of a single for loop.
3505struct LoopIterationSpace final {
3506 /// \brief Condition of the loop.
3507 Expr *PreCond = nullptr;
3508 /// \brief This expression calculates the number of iterations in the loop.
3509 /// It is always possible to calculate it before starting the loop.
3510 Expr *NumIterations = nullptr;
3511 /// \brief The loop counter variable.
3512 Expr *CounterVar = nullptr;
3513 /// \brief Private loop counter variable.
3514 Expr *PrivateCounterVar = nullptr;
3515 /// \brief This is initializer for the initial value of #CounterVar.
3516 Expr *CounterInit = nullptr;
3517 /// \brief This is step for the #CounterVar used to generate its update:
3518 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3519 Expr *CounterStep = nullptr;
3520 /// \brief Should step be subtracted?
3521 bool Subtract = false;
3522 /// \brief Source range of the loop init.
3523 SourceRange InitSrcRange;
3524 /// \brief Source range of the loop condition.
3525 SourceRange CondSrcRange;
3526 /// \brief Source range of the loop increment.
3527 SourceRange IncSrcRange;
3528};
3529
3530} // namespace
3531
3532void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3533 assert(getLangOpts().OpenMP && "OpenMP is not active.")((getLangOpts().OpenMP && "OpenMP is not active.") ? static_cast
<void> (0) : __assert_fail ("getLangOpts().OpenMP && \"OpenMP is not active.\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 3533, __PRETTY_FUNCTION__))
;
3534 assert(Init && "Expected loop in canonical form.")((Init && "Expected loop in canonical form.") ? static_cast
<void> (0) : __assert_fail ("Init && \"Expected loop in canonical form.\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 3534, __PRETTY_FUNCTION__))
;
3535 unsigned AssociatedLoops = DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getAssociatedLoops();
3536 if (AssociatedLoops > 0 &&
1
Assuming 'AssociatedLoops' is > 0
3
Taking true branch
3537 isOpenMPLoopDirective(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective())
) {
2
Assuming the condition is true
3538 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3539 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4
Taking true branch
3540 if (auto *D = ISC.GetLoopDecl()) {
5
Assuming 'D' is non-null
6
Taking true branch
3541 auto *VD = dyn_cast<VarDecl>(D);
3542 if (!VD) {
7
Taking true branch
3543 if (auto *Private = IsOpenMPCapturedDecl(D))
8
Taking false branch
3544 VD = Private;
3545 else {
3546 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
9
Calling 'buildCapture'
3547 /*WithInit=*/false);
3548 VD = cast<VarDecl>(Ref->getDecl());
3549 }
3550 }
3551 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->addLoopControlVariable(D, VD);
3552 }
3553 }
3554 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setAssociatedLoops(AssociatedLoops - 1);
3555 }
3556}
3557
3558/// \brief Called on a for stmt to check and extract its iteration space
3559/// for further processing (such as collapsing).
3560static bool CheckOpenMPIterationSpace(
3561 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3562 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
3563 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
3564 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
3565 LoopIterationSpace &ResultIterSpace,
3566 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3567 // OpenMP [2.6, Canonical Loop Form]
3568 // for (init-expr; test-expr; incr-expr) structured-block
3569 auto *For = dyn_cast_or_null<ForStmt>(S);
3570 if (!For) {
3571 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
3572 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3573 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3574 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3575 if (NestedLoopCount > 1) {
3576 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3577 SemaRef.Diag(DSA.getConstructLoc(),
3578 diag::note_omp_collapse_ordered_expr)
3579 << 2 << CollapseLoopCountExpr->getSourceRange()
3580 << OrderedLoopCountExpr->getSourceRange();
3581 else if (CollapseLoopCountExpr)
3582 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3583 diag::note_omp_collapse_ordered_expr)
3584 << 0 << CollapseLoopCountExpr->getSourceRange();
3585 else
3586 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3587 diag::note_omp_collapse_ordered_expr)
3588 << 1 << OrderedLoopCountExpr->getSourceRange();
3589 }
3590 return true;
3591 }
3592 assert(For->getBody())((For->getBody()) ? static_cast<void> (0) : __assert_fail
("For->getBody()", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 3592, __PRETTY_FUNCTION__))
;
3593
3594 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3595
3596 // Check init.
3597 auto Init = For->getInit();
3598 if (ISC.CheckInit(Init))
3599 return true;
3600
3601 bool HasErrors = false;
3602
3603 // Check loop variable's type.
3604 if (auto *LCDecl = ISC.GetLoopDecl()) {
3605 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
3606
3607 // OpenMP [2.6, Canonical Loop Form]
3608 // Var is one of the following:
3609 // A variable of signed or unsigned integer type.
3610 // For C++, a variable of a random access iterator type.
3611 // For C, a variable of a pointer type.
3612 auto VarType = LCDecl->getType().getNonReferenceType();
3613 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3614 !VarType->isPointerType() &&
3615 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3616 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3617 << SemaRef.getLangOpts().CPlusPlus;
3618 HasErrors = true;
3619 }
3620
3621 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
3622 // a Construct
3623 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3624 // parallel for construct is (are) private.
3625 // The loop iteration variable in the associated for-loop of a simd
3626 // construct with just one associated for-loop is linear with a
3627 // constant-linear-step that is the increment of the associated for-loop.
3628 // Exclude loop var from the list of variables with implicitly defined data
3629 // sharing attributes.
3630 VarsWithImplicitDSA.erase(LCDecl);
3631
3632 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3633 // in a Construct, C/C++].
3634 // The loop iteration variable in the associated for-loop of a simd
3635 // construct with just one associated for-loop may be listed in a linear
3636 // clause with a constant-linear-step that is the increment of the
3637 // associated for-loop.
3638 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3639 // parallel for construct may be listed in a private or lastprivate clause.
3640 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
3641 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3642 // declared in the loop and it is predetermined as a private.
3643 auto PredeterminedCKind =
3644 isOpenMPSimdDirective(DKind)
3645 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3646 : OMPC_private;
3647 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3648 DVar.CKind != PredeterminedCKind) ||
3649 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
3650 isOpenMPDistributeDirective(DKind)) &&
3651 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3652 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3653 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3654 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
3655 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3656 << getOpenMPClauseName(PredeterminedCKind);
3657 if (DVar.RefExpr == nullptr)
3658 DVar.CKind = PredeterminedCKind;
3659 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
3660 HasErrors = true;
3661 } else if (LoopDeclRefExpr != nullptr) {
3662 // Make the loop iteration variable private (for worksharing constructs),
3663 // linear (for simd directives with the only one associated loop) or
3664 // lastprivate (for simd directives with several collapsed or ordered
3665 // loops).
3666 if (DVar.CKind == OMPC_unknown)
3667 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
3668 [](OpenMPDirectiveKind) -> bool { return true; },
3669 /*FromParent=*/false);
3670 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
3671 }
3672
3673 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars")((isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"
) ? static_cast<void> (0) : __assert_fail ("isOpenMPLoopDirective(DKind) && \"DSA for non-loop vars\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 3673, __PRETTY_FUNCTION__))
;
3674
3675 // Check test-expr.
3676 HasErrors |= ISC.CheckCond(For->getCond());
3677
3678 // Check incr-expr.
3679 HasErrors |= ISC.CheckInc(For->getInc());
3680 }
3681
3682 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
3683 return HasErrors;
3684
3685 // Build the loop's iteration space representation.
3686 ResultIterSpace.PreCond =
3687 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
3688 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
3689 DSA.getCurScope(),
3690 (isOpenMPWorksharingDirective(DKind) ||
3691 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
3692 Captures);
3693 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
3694 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
3695 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3696 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3697 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3698 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3699 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3700 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3701
3702 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3703 ResultIterSpace.NumIterations == nullptr ||
3704 ResultIterSpace.CounterVar == nullptr ||
3705 ResultIterSpace.PrivateCounterVar == nullptr ||
3706 ResultIterSpace.CounterInit == nullptr ||
3707 ResultIterSpace.CounterStep == nullptr);
3708
3709 return HasErrors;
3710}
3711
3712/// \brief Build 'VarRef = Start.
3713static ExprResult
3714BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
3715 ExprResult Start,
3716 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3717 // Build 'VarRef = Start.
3718 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
3719 if (!NewStart.isUsable())
3720 return ExprError();
3721 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
3722 VarRef.get()->getType())) {
3723 NewStart = SemaRef.PerformImplicitConversion(
3724 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3725 /*AllowExplicit=*/true);
3726 if (!NewStart.isUsable())
3727 return ExprError();
3728 }
3729
3730 auto Init =
3731 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3732 return Init;
3733}
3734
3735/// \brief Build 'VarRef = Start + Iter * Step'.
3736static ExprResult
3737BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
3738 ExprResult VarRef, ExprResult Start, ExprResult Iter,
3739 ExprResult Step, bool Subtract,
3740 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
3741 // Add parentheses (for debugging purposes only).
3742 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3743 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3744 !Step.isUsable())
3745 return ExprError();
3746
3747 ExprResult NewStep = Step;
3748 if (Captures)
3749 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
3750 if (NewStep.isInvalid())
3751 return ExprError();
3752 ExprResult Update =
3753 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
3754 if (!Update.isUsable())
3755 return ExprError();
3756
3757 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
3758 // 'VarRef = Start (+|-) Iter * Step'.
3759 ExprResult NewStart = Start;
3760 if (Captures)
3761 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
3762 if (NewStart.isInvalid())
3763 return ExprError();
3764
3765 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
3766 ExprResult SavedUpdate = Update;
3767 ExprResult UpdateVal;
3768 if (VarRef.get()->getType()->isOverloadableType() ||
3769 NewStart.get()->getType()->isOverloadableType() ||
3770 Update.get()->getType()->isOverloadableType()) {
3771 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3772 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
3773 Update =
3774 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3775 if (Update.isUsable()) {
3776 UpdateVal =
3777 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
3778 VarRef.get(), SavedUpdate.get());
3779 if (UpdateVal.isUsable()) {
3780 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
3781 UpdateVal.get());
3782 }
3783 }
3784 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3785 }
3786
3787 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
3788 if (!Update.isUsable() || !UpdateVal.isUsable()) {
3789 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
3790 NewStart.get(), SavedUpdate.get());
3791 if (!Update.isUsable())
3792 return ExprError();
3793
3794 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
3795 VarRef.get()->getType())) {
3796 Update = SemaRef.PerformImplicitConversion(
3797 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3798 if (!Update.isUsable())
3799 return ExprError();
3800 }
3801
3802 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3803 }
3804 return Update;
3805}
3806
3807/// \brief Convert integer expression \a E to make it have at least \a Bits
3808/// bits.
3809static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
3810 if (E == nullptr)
3811 return ExprError();
3812 auto &C = SemaRef.Context;
3813 QualType OldType = E->getType();
3814 unsigned HasBits = C.getTypeSize(OldType);
3815 if (HasBits >= Bits)
3816 return ExprResult(E);
3817 // OK to convert to signed, because new type has more bits than old.
3818 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3819 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3820 true);
3821}
3822
3823/// \brief Check if the given expression \a E is a constant integer that fits
3824/// into \a Bits bits.
3825static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3826 if (E == nullptr)
3827 return false;
3828 llvm::APSInt Result;
3829 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3830 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3831 return false;
3832}
3833
3834/// Build preinits statement for the given declarations.
3835static Stmt *buildPreInits(ASTContext &Context,
3836 SmallVectorImpl<Decl *> &PreInits) {
3837 if (!PreInits.empty()) {
3838 return new (Context) DeclStmt(
3839 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
3840 SourceLocation(), SourceLocation());
3841 }
3842 return nullptr;
3843}
3844
3845/// Build preinits statement for the given declarations.
3846static Stmt *buildPreInits(ASTContext &Context,
3847 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3848 if (!Captures.empty()) {
3849 SmallVector<Decl *, 16> PreInits;
3850 for (auto &Pair : Captures)
3851 PreInits.push_back(Pair.second->getDecl());
3852 return buildPreInits(Context, PreInits);
3853 }
3854 return nullptr;
3855}
3856
3857/// Build postupdate expression for the given list of postupdates expressions.
3858static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
3859 Expr *PostUpdate = nullptr;
3860 if (!PostUpdates.empty()) {
3861 for (auto *E : PostUpdates) {
3862 Expr *ConvE = S.BuildCStyleCastExpr(
3863 E->getExprLoc(),
3864 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
3865 E->getExprLoc(), E)
3866 .get();
3867 PostUpdate = PostUpdate
3868 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
3869 PostUpdate, ConvE)
3870 .get()
3871 : ConvE;
3872 }
3873 }
3874 return PostUpdate;
3875}
3876
3877/// \brief Called on a for stmt to check itself and nested loops (if any).
3878/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3879/// number of collapsed loops otherwise.
3880static unsigned
3881CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3882 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3883 DSAStackTy &DSA,
3884 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
3885 OMPLoopDirective::HelperExprs &Built) {
3886 unsigned NestedLoopCount = 1;
3887 if (CollapseLoopCountExpr) {
3888 // Found 'collapse' clause - calculate collapse number.
3889 llvm::APSInt Result;
3890 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3891 NestedLoopCount = Result.getLimitedValue();
3892 }
3893 if (OrderedLoopCountExpr) {
3894 // Found 'ordered' clause - calculate collapse number.
3895 llvm::APSInt Result;
3896 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3897 if (Result.getLimitedValue() < NestedLoopCount) {
3898 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3899 diag::err_omp_wrong_ordered_loop_count)
3900 << OrderedLoopCountExpr->getSourceRange();
3901 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3902 diag::note_collapse_loop_count)
3903 << CollapseLoopCountExpr->getSourceRange();
3904 }
3905 NestedLoopCount = Result.getLimitedValue();
3906 }
3907 }
3908 // This is helper routine for loop directives (e.g., 'for', 'simd',
3909 // 'for simd', etc.).
3910 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
3911 SmallVector<LoopIterationSpace, 4> IterSpaces;
3912 IterSpaces.resize(NestedLoopCount);
3913 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
3914 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
3915 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
3916 NestedLoopCount, CollapseLoopCountExpr,
3917 OrderedLoopCountExpr, VarsWithImplicitDSA,
3918 IterSpaces[Cnt], Captures))
3919 return 0;
3920 // Move on to the next nested for loop, or to the loop body.
3921 // OpenMP [2.8.1, simd construct, Restrictions]
3922 // All loops associated with the construct must be perfectly nested; that
3923 // is, there must be no intervening code nor any OpenMP directive between
3924 // any two loops.
3925 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
3926 }
3927
3928 Built.clear(/* size */ NestedLoopCount);
3929
3930 if (SemaRef.CurContext->isDependentContext())
3931 return NestedLoopCount;
3932
3933 // An example of what is generated for the following code:
3934 //
3935 // #pragma omp simd collapse(2) ordered(2)
3936 // for (i = 0; i < NI; ++i)
3937 // for (k = 0; k < NK; ++k)
3938 // for (j = J0; j < NJ; j+=2) {
3939 // <loop body>
3940 // }
3941 //
3942 // We generate the code below.
3943 // Note: the loop body may be outlined in CodeGen.
3944 // Note: some counters may be C++ classes, operator- is used to find number of
3945 // iterations and operator+= to calculate counter value.
3946 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3947 // or i64 is currently supported).
3948 //
3949 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3950 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3951 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3952 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3953 // // similar updates for vars in clauses (e.g. 'linear')
3954 // <loop body (using local i and j)>
3955 // }
3956 // i = NI; // assign final values of counters
3957 // j = NJ;
3958 //
3959
3960 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3961 // the iteration counts of the collapsed for loops.
3962 // Precondition tests if there is at least one iteration (all conditions are
3963 // true).
3964 auto PreCond = ExprResult(IterSpaces[0].PreCond);
3965 auto N0 = IterSpaces[0].NumIterations;
3966 ExprResult LastIteration32 = WidenIterationCount(
3967 32 /* Bits */, SemaRef
3968 .PerformImplicitConversion(
3969 N0->IgnoreImpCasts(), N0->getType(),
3970 Sema::AA_Converting, /*AllowExplicit=*/true)
3971 .get(),
3972 SemaRef);
3973 ExprResult LastIteration64 = WidenIterationCount(
3974 64 /* Bits */, SemaRef
3975 .PerformImplicitConversion(
3976 N0->IgnoreImpCasts(), N0->getType(),
3977 Sema::AA_Converting, /*AllowExplicit=*/true)
3978 .get(),
3979 SemaRef);
3980
3981 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3982 return NestedLoopCount;
3983
3984 auto &C = SemaRef.Context;
3985 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3986
3987 Scope *CurScope = DSA.getCurScope();
3988 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
3989 if (PreCond.isUsable()) {
3990 PreCond =
3991 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
3992 PreCond.get(), IterSpaces[Cnt].PreCond);
3993 }
3994 auto N = IterSpaces[Cnt].NumIterations;
3995 SourceLocation Loc = N->getExprLoc();
3996 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3997 if (LastIteration32.isUsable())
3998 LastIteration32 = SemaRef.BuildBinOp(
3999 CurScope, Loc, BO_Mul, LastIteration32.get(),
4000 SemaRef
4001 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4002 Sema::AA_Converting,
4003 /*AllowExplicit=*/true)
4004 .get());
4005 if (LastIteration64.isUsable())
4006 LastIteration64 = SemaRef.BuildBinOp(
4007 CurScope, Loc, BO_Mul, LastIteration64.get(),
4008 SemaRef
4009 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4010 Sema::AA_Converting,
4011 /*AllowExplicit=*/true)
4012 .get());
4013 }
4014
4015 // Choose either the 32-bit or 64-bit version.
4016 ExprResult LastIteration = LastIteration64;
4017 if (LastIteration32.isUsable() &&
4018 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4019 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4020 FitsInto(
4021 32 /* Bits */,
4022 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4023 LastIteration64.get(), SemaRef)))
4024 LastIteration = LastIteration32;
4025 QualType VType = LastIteration.get()->getType();
4026 QualType RealVType = VType;
4027 QualType StrideVType = VType;
4028 if (isOpenMPTaskLoopDirective(DKind)) {
4029 VType =
4030 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4031 StrideVType =
4032 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4033 }
4034
4035 if (!LastIteration.isUsable())
4036 return 0;
4037
4038 // Save the number of iterations.
4039 ExprResult NumIterations = LastIteration;
4040 {
4041 LastIteration = SemaRef.BuildBinOp(
4042 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
4043 LastIteration.get(),
4044 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4045 if (!LastIteration.isUsable())
4046 return 0;
4047 }
4048
4049 // Calculate the last iteration number beforehand instead of doing this on
4050 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4051 llvm::APSInt Result;
4052 bool IsConstant =
4053 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4054 ExprResult CalcLastIteration;
4055 if (!IsConstant) {
4056 ExprResult SaveRef =
4057 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
4058 LastIteration = SaveRef;
4059
4060 // Prepare SaveRef + 1.
4061 NumIterations = SemaRef.BuildBinOp(
4062 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
4063 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4064 if (!NumIterations.isUsable())
4065 return 0;
4066 }
4067
4068 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4069
4070 // Build variables passed into runtime, necessary for worksharing directives.
4071 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
4072 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4073 isOpenMPDistributeDirective(DKind)) {
4074 // Lower bound variable, initialized with zero.
4075 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4076 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
4077 SemaRef.AddInitializerToDecl(LBDecl,
4078 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4079 /*DirectInit*/ false);
4080
4081 // Upper bound variable, initialized with last iteration number.
4082 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4083 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
4084 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4085 /*DirectInit*/ false);
4086
4087 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4088 // This will be used to implement clause 'lastprivate'.
4089 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
4090 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4091 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
4092 SemaRef.AddInitializerToDecl(ILDecl,
4093 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4094 /*DirectInit*/ false);
4095
4096 // Stride variable returned by runtime (we initialize it to 1 by default).
4097 VarDecl *STDecl =
4098 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4099 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
4100 SemaRef.AddInitializerToDecl(STDecl,
4101 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4102 /*DirectInit*/ false);
4103
4104 // Build expression: UB = min(UB, LastIteration)
4105 // It is necessary for CodeGen of directives with static scheduling.
4106 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4107 UB.get(), LastIteration.get());
4108 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4109 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4110 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4111 CondOp.get());
4112 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4113
4114 // If we have a combined directive that combines 'distribute', 'for' or
4115 // 'simd' we need to be able to access the bounds of the schedule of the
4116 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
4117 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
4118 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4119
4120 // Lower bound variable, initialized with zero.
4121 VarDecl *CombLBDecl =
4122 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
4123 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
4124 SemaRef.AddInitializerToDecl(
4125 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4126 /*DirectInit*/ false);
4127
4128 // Upper bound variable, initialized with last iteration number.
4129 VarDecl *CombUBDecl =
4130 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
4131 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
4132 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
4133 /*DirectInit*/ false);
4134
4135 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
4136 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
4137 ExprResult CombCondOp =
4138 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
4139 LastIteration.get(), CombUB.get());
4140 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
4141 CombCondOp.get());
4142 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
4143
4144 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
4145 // We expect to have at least 2 more parameters than the 'parallel'
4146 // directive does - the lower and upper bounds of the previous schedule.
4147 assert(CD->getNumParams() >= 4 &&((CD->getNumParams() >= 4 && "Unexpected number of parameters in loop combined directive"
) ? static_cast<void> (0) : __assert_fail ("CD->getNumParams() >= 4 && \"Unexpected number of parameters in loop combined directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4148, __PRETTY_FUNCTION__))
4148 "Unexpected number of parameters in loop combined directive")((CD->getNumParams() >= 4 && "Unexpected number of parameters in loop combined directive"
) ? static_cast<void> (0) : __assert_fail ("CD->getNumParams() >= 4 && \"Unexpected number of parameters in loop combined directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4148, __PRETTY_FUNCTION__))
;
4149
4150 // Set the proper type for the bounds given what we learned from the
4151 // enclosed loops.
4152 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
4153 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
4154
4155 // Previous lower and upper bounds are obtained from the region
4156 // parameters.
4157 PrevLB =
4158 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
4159 PrevUB =
4160 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
4161 }
4162 }
4163
4164 // Build the iteration variable and its initialization before loop.
4165 ExprResult IV;
4166 ExprResult Init, CombInit;
4167 {
4168 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4169 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
4170 Expr *RHS =
4171 (isOpenMPWorksharingDirective(DKind) ||
4172 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4173 ? LB.get()
4174 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4175 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4176 Init = SemaRef.ActOnFinishFullExpr(Init.get());
4177
4178 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4179 Expr *CombRHS =
4180 (isOpenMPWorksharingDirective(DKind) ||
4181 isOpenMPTaskLoopDirective(DKind) ||
4182 isOpenMPDistributeDirective(DKind))
4183 ? CombLB.get()
4184 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4185 CombInit =
4186 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
4187 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
4188 }
4189 }
4190
4191 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
4192 SourceLocation CondLoc;
4193 ExprResult Cond =
4194 (isOpenMPWorksharingDirective(DKind) ||
4195 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4196 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4197 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4198 NumIterations.get());
4199 ExprResult CombCond;
4200 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4201 CombCond =
4202 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
4203 }
4204 // Loop increment (IV = IV + 1)
4205 SourceLocation IncLoc;
4206 ExprResult Inc =
4207 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4208 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4209 if (!Inc.isUsable())
4210 return 0;
4211 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
4212 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4213 if (!Inc.isUsable())
4214 return 0;
4215
4216 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4217 // Used for directives with static scheduling.
4218 // In combined construct, add combined version that use CombLB and CombUB
4219 // base variables for the update
4220 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
4221 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4222 isOpenMPDistributeDirective(DKind)) {
4223 // LB + ST
4224 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4225 if (!NextLB.isUsable())
4226 return 0;
4227 // LB = LB + ST
4228 NextLB =
4229 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4230 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4231 if (!NextLB.isUsable())
4232 return 0;
4233 // UB + ST
4234 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4235 if (!NextUB.isUsable())
4236 return 0;
4237 // UB = UB + ST
4238 NextUB =
4239 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4240 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4241 if (!NextUB.isUsable())
4242 return 0;
4243 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4244 CombNextLB =
4245 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
4246 if (!NextLB.isUsable())
4247 return 0;
4248 // LB = LB + ST
4249 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
4250 CombNextLB.get());
4251 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
4252 if (!CombNextLB.isUsable())
4253 return 0;
4254 // UB + ST
4255 CombNextUB =
4256 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
4257 if (!CombNextUB.isUsable())
4258 return 0;
4259 // UB = UB + ST
4260 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
4261 CombNextUB.get());
4262 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
4263 if (!CombNextUB.isUsable())
4264 return 0;
4265 }
4266 }
4267
4268 // Create increment expression for distribute loop when combined in a same
4269 // directive with for as IV = IV + ST; ensure upper bound expression based
4270 // on PrevUB instead of NumIterations - used to implement 'for' when found
4271 // in combination with 'distribute', like in 'distribute parallel for'
4272 SourceLocation DistIncLoc;
4273 ExprResult DistCond, DistInc, PrevEUB;
4274 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4275 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
4276 assert(DistCond.isUsable() && "distribute cond expr was not built")((DistCond.isUsable() && "distribute cond expr was not built"
) ? static_cast<void> (0) : __assert_fail ("DistCond.isUsable() && \"distribute cond expr was not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4276, __PRETTY_FUNCTION__))
;
4277
4278 DistInc =
4279 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
4280 assert(DistInc.isUsable() && "distribute inc expr was not built")((DistInc.isUsable() && "distribute inc expr was not built"
) ? static_cast<void> (0) : __assert_fail ("DistInc.isUsable() && \"distribute inc expr was not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4280, __PRETTY_FUNCTION__))
;
4281 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
4282 DistInc.get());
4283 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
4284 assert(DistInc.isUsable() && "distribute inc expr was not built")((DistInc.isUsable() && "distribute inc expr was not built"
) ? static_cast<void> (0) : __assert_fail ("DistInc.isUsable() && \"distribute inc expr was not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4284, __PRETTY_FUNCTION__))
;
4285
4286 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
4287 // construct
4288 SourceLocation DistEUBLoc;
4289 ExprResult IsUBGreater =
4290 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
4291 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4292 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
4293 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
4294 CondOp.get());
4295 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
4296 }
4297
4298 // Build updates and final values of the loop counters.
4299 bool HasErrors = false;
4300 Built.Counters.resize(NestedLoopCount);
4301 Built.Inits.resize(NestedLoopCount);
4302 Built.Updates.resize(NestedLoopCount);
4303 Built.Finals.resize(NestedLoopCount);
4304 SmallVector<Expr *, 4> LoopMultipliers;
4305 {
4306 ExprResult Div;
4307 // Go from inner nested loop to outer.
4308 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4309 LoopIterationSpace &IS = IterSpaces[Cnt];
4310 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4311 // Build: Iter = (IV / Div) % IS.NumIters
4312 // where Div is product of previous iterations' IS.NumIters.
4313 ExprResult Iter;
4314 if (Div.isUsable()) {
4315 Iter =
4316 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4317 } else {
4318 Iter = IV;
4319 assert((Cnt == (int)NestedLoopCount - 1) &&(((Cnt == (int)NestedLoopCount - 1) && "unusable div expected on first iteration only"
) ? static_cast<void> (0) : __assert_fail ("(Cnt == (int)NestedLoopCount - 1) && \"unusable div expected on first iteration only\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4320, __PRETTY_FUNCTION__))
4320 "unusable div expected on first iteration only")(((Cnt == (int)NestedLoopCount - 1) && "unusable div expected on first iteration only"
) ? static_cast<void> (0) : __assert_fail ("(Cnt == (int)NestedLoopCount - 1) && \"unusable div expected on first iteration only\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4320, __PRETTY_FUNCTION__))
;
4321 }
4322
4323 if (Cnt != 0 && Iter.isUsable())
4324 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4325 IS.NumIterations);
4326 if (!Iter.isUsable()) {
4327 HasErrors = true;
4328 break;
4329 }
4330
4331 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4332 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4333 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4334 IS.CounterVar->getExprLoc(),
4335 /*RefersToCapture=*/true);
4336 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4337 IS.CounterInit, Captures);
4338 if (!Init.isUsable()) {
4339 HasErrors = true;
4340 break;
4341 }
4342 ExprResult Update = BuildCounterUpdate(
4343 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4344 IS.CounterStep, IS.Subtract, &Captures);
4345 if (!Update.isUsable()) {
4346 HasErrors = true;
4347 break;
4348 }
4349
4350 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4351 ExprResult Final = BuildCounterUpdate(
4352 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
4353 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
4354 if (!Final.isUsable()) {
4355 HasErrors = true;
4356 break;
4357 }
4358
4359 // Build Div for the next iteration: Div <- Div * IS.NumIters
4360 if (Cnt != 0) {
4361 if (Div.isUnset())
4362 Div = IS.NumIterations;
4363 else
4364 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4365 IS.NumIterations);
4366
4367 // Add parentheses (for debugging purposes only).
4368 if (Div.isUsable())
4369 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
4370 if (!Div.isUsable()) {
4371 HasErrors = true;
4372 break;
4373 }
4374 LoopMultipliers.push_back(Div.get());
4375 }
4376 if (!Update.isUsable() || !Final.isUsable()) {
4377 HasErrors = true;
4378 break;
4379 }
4380 // Save results
4381 Built.Counters[Cnt] = IS.CounterVar;
4382 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
4383 Built.Inits[Cnt] = Init.get();
4384 Built.Updates[Cnt] = Update.get();
4385 Built.Finals[Cnt] = Final.get();
4386 }
4387 }
4388
4389 if (HasErrors)
4390 return 0;
4391
4392 // Save results
4393 Built.IterationVarRef = IV.get();
4394 Built.LastIteration = LastIteration.get();
4395 Built.NumIterations = NumIterations.get();
4396 Built.CalcLastIteration =
4397 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
4398 Built.PreCond = PreCond.get();
4399 Built.PreInits = buildPreInits(C, Captures);
4400 Built.Cond = Cond.get();
4401 Built.Init = Init.get();
4402 Built.Inc = Inc.get();
4403 Built.LB = LB.get();
4404 Built.UB = UB.get();
4405 Built.IL = IL.get();
4406 Built.ST = ST.get();
4407 Built.EUB = EUB.get();
4408 Built.NLB = NextLB.get();
4409 Built.NUB = NextUB.get();
4410 Built.PrevLB = PrevLB.get();
4411 Built.PrevUB = PrevUB.get();
4412 Built.DistInc = DistInc.get();
4413 Built.PrevEUB = PrevEUB.get();
4414 Built.DistCombinedFields.LB = CombLB.get();
4415 Built.DistCombinedFields.UB = CombUB.get();
4416 Built.DistCombinedFields.EUB = CombEUB.get();
4417 Built.DistCombinedFields.Init = CombInit.get();
4418 Built.DistCombinedFields.Cond = CombCond.get();
4419 Built.DistCombinedFields.NLB = CombNextLB.get();
4420 Built.DistCombinedFields.NUB = CombNextUB.get();
4421
4422 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
4423 // Fill data for doacross depend clauses.
4424 for (auto Pair : DSA.getDoacrossDependClauses()) {
4425 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4426 Pair.first->setCounterValue(CounterVal);
4427 else {
4428 if (NestedLoopCount != Pair.second.size() ||
4429 NestedLoopCount != LoopMultipliers.size() + 1) {
4430 // Erroneous case - clause has some problems.
4431 Pair.first->setCounterValue(CounterVal);
4432 continue;
4433 }
4434 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink)((Pair.first->getDependencyKind() == OMPC_DEPEND_sink) ? static_cast
<void> (0) : __assert_fail ("Pair.first->getDependencyKind() == OMPC_DEPEND_sink"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4434, __PRETTY_FUNCTION__))
;
4435 auto I = Pair.second.rbegin();
4436 auto IS = IterSpaces.rbegin();
4437 auto ILM = LoopMultipliers.rbegin();
4438 Expr *UpCounterVal = CounterVal;
4439 Expr *Multiplier = nullptr;
4440 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4441 if (I->first) {
4442 assert(IS->CounterStep)((IS->CounterStep) ? static_cast<void> (0) : __assert_fail
("IS->CounterStep", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4442, __PRETTY_FUNCTION__))
;
4443 Expr *NormalizedOffset =
4444 SemaRef
4445 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
4446 I->first, IS->CounterStep)
4447 .get();
4448 if (Multiplier) {
4449 NormalizedOffset =
4450 SemaRef
4451 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
4452 NormalizedOffset, Multiplier)
4453 .get();
4454 }
4455 assert(I->second == OO_Plus || I->second == OO_Minus)((I->second == OO_Plus || I->second == OO_Minus) ? static_cast
<void> (0) : __assert_fail ("I->second == OO_Plus || I->second == OO_Minus"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4455, __PRETTY_FUNCTION__))
;
4456 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
4457 UpCounterVal = SemaRef
4458 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
4459 UpCounterVal, NormalizedOffset)
4460 .get();
4461 }
4462 Multiplier = *ILM;
4463 ++I;
4464 ++IS;
4465 ++ILM;
4466 }
4467 Pair.first->setCounterValue(UpCounterVal);
4468 }
4469 }
4470
4471 return NestedLoopCount;
4472}
4473
4474static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
4475 auto CollapseClauses =
4476 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4477 if (CollapseClauses.begin() != CollapseClauses.end())
4478 return (*CollapseClauses.begin())->getNumForLoops();
4479 return nullptr;
4480}
4481
4482static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
4483 auto OrderedClauses =
4484 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4485 if (OrderedClauses.begin() != OrderedClauses.end())
4486 return (*OrderedClauses.begin())->getNumForLoops();
4487 return nullptr;
4488}
4489
4490static bool checkSimdlenSafelenSpecified(Sema &S,
4491 const ArrayRef<OMPClause *> Clauses) {
4492 OMPSafelenClause *Safelen = nullptr;
4493 OMPSimdlenClause *Simdlen = nullptr;
4494
4495 for (auto *Clause : Clauses) {
4496 if (Clause->getClauseKind() == OMPC_safelen)
4497 Safelen = cast<OMPSafelenClause>(Clause);
4498 else if (Clause->getClauseKind() == OMPC_simdlen)
4499 Simdlen = cast<OMPSimdlenClause>(Clause);
4500 if (Safelen && Simdlen)
4501 break;
4502 }
4503
4504 if (Simdlen && Safelen) {
4505 llvm::APSInt SimdlenRes, SafelenRes;
4506 auto SimdlenLength = Simdlen->getSimdlen();
4507 auto SafelenLength = Safelen->getSafelen();
4508 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
4509 SimdlenLength->isInstantiationDependent() ||
4510 SimdlenLength->containsUnexpandedParameterPack())
4511 return false;
4512 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
4513 SafelenLength->isInstantiationDependent() ||
4514 SafelenLength->containsUnexpandedParameterPack())
4515 return false;
4516 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
4517 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
4518 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
4519 // If both simdlen and safelen clauses are specified, the value of the
4520 // simdlen parameter must be less than or equal to the value of the safelen
4521 // parameter.
4522 if (SimdlenRes > SafelenRes) {
4523 S.Diag(SimdlenLength->getExprLoc(),
4524 diag::err_omp_wrong_simdlen_safelen_values)
4525 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
4526 return true;
4527 }
4528 }
4529 return false;
4530}
4531
4532StmtResult Sema::ActOnOpenMPSimdDirective(
4533 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4534 SourceLocation EndLoc,
4535 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4536 if (!AStmt)
4537 return StmtError();
4538
4539 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4539, __PRETTY_FUNCTION__))
;
4540 OMPLoopDirective::HelperExprs B;
4541 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4542 // define the nested loops number.
4543 unsigned NestedLoopCount = CheckOpenMPLoop(
4544 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4545 AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
, VarsWithImplicitDSA, B);
4546 if (NestedLoopCount == 0)
4547 return StmtError();
4548
4549 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp simd loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4550, __PRETTY_FUNCTION__))
4550 "omp simd loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp simd loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4550, __PRETTY_FUNCTION__))
;
4551
4552 if (!CurContext->isDependentContext()) {
4553 // Finalize the clauses that need pre-built expressions for CodeGen.
4554 for (auto C : Clauses) {
4555 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4556 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4557 B.NumIterations, *this, CurScope,
4558 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
4559 return StmtError();
4560 }
4561 }
4562
4563 if (checkSimdlenSafelenSpecified(*this, Clauses))
4564 return StmtError();
4565
4566 getCurFunction()->setHasBranchProtectedScope();
4567 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4568 Clauses, AStmt, B);
4569}
4570
4571StmtResult Sema::ActOnOpenMPForDirective(
4572 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4573 SourceLocation EndLoc,
4574 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4575 if (!AStmt)
4576 return StmtError();
4577
4578 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4578, __PRETTY_FUNCTION__))
;
4579 OMPLoopDirective::HelperExprs B;
4580 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4581 // define the nested loops number.
4582 unsigned NestedLoopCount = CheckOpenMPLoop(
4583 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4584 AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
, VarsWithImplicitDSA, B);
4585 if (NestedLoopCount == 0)
4586 return StmtError();
4587
4588 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4589, __PRETTY_FUNCTION__))
4589 "omp for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4589, __PRETTY_FUNCTION__))
;
4590
4591 if (!CurContext->isDependentContext()) {
4592 // Finalize the clauses that need pre-built expressions for CodeGen.
4593 for (auto C : Clauses) {
4594 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4595 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4596 B.NumIterations, *this, CurScope,
4597 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
4598 return StmtError();
4599 }
4600 }
4601
4602 getCurFunction()->setHasBranchProtectedScope();
4603 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4604 Clauses, AStmt, B, DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
4605}
4606
4607StmtResult Sema::ActOnOpenMPForSimdDirective(
4608 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4609 SourceLocation EndLoc,
4610 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4611 if (!AStmt)
4612 return StmtError();
4613
4614 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4614, __PRETTY_FUNCTION__))
;
4615 OMPLoopDirective::HelperExprs B;
4616 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4617 // define the nested loops number.
4618 unsigned NestedLoopCount =
4619 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4620 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
4621 VarsWithImplicitDSA, B);
4622 if (NestedLoopCount == 0)
4623 return StmtError();
4624
4625 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for simd loop exprs were not built") ? static_cast<void
> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4626, __PRETTY_FUNCTION__))
4626 "omp for simd loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for simd loop exprs were not built") ? static_cast<void
> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4626, __PRETTY_FUNCTION__))
;
4627
4628 if (!CurContext->isDependentContext()) {
4629 // Finalize the clauses that need pre-built expressions for CodeGen.
4630 for (auto C : Clauses) {
4631 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4632 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4633 B.NumIterations, *this, CurScope,
4634 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
4635 return StmtError();
4636 }
4637 }
4638
4639 if (checkSimdlenSafelenSpecified(*this, Clauses))
4640 return StmtError();
4641
4642 getCurFunction()->setHasBranchProtectedScope();
4643 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4644 Clauses, AStmt, B);
4645}
4646
4647StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4648 Stmt *AStmt,
4649 SourceLocation StartLoc,
4650 SourceLocation EndLoc) {
4651 if (!AStmt)
4652 return StmtError();
4653
4654 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4654, __PRETTY_FUNCTION__))
;
4655 auto BaseStmt = AStmt;
4656 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4657 BaseStmt = CS->getCapturedStmt();
4658 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4659 auto S = C->children();
4660 if (S.begin() == S.end())
4661 return StmtError();
4662 // All associated statements must be '#pragma omp section' except for
4663 // the first one.
4664 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
4665 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4666 if (SectionStmt)
4667 Diag(SectionStmt->getLocStart(),
4668 diag::err_omp_sections_substmt_not_section);
4669 return StmtError();
4670 }
4671 cast<OMPSectionDirective>(SectionStmt)
4672 ->setHasCancel(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
4673 }
4674 } else {
4675 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4676 return StmtError();
4677 }
4678
4679 getCurFunction()->setHasBranchProtectedScope();
4680
4681 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4682 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
4683}
4684
4685StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4686 SourceLocation StartLoc,
4687 SourceLocation EndLoc) {
4688 if (!AStmt)
4689 return StmtError();
4690
4691 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4691, __PRETTY_FUNCTION__))
;
4692
4693 getCurFunction()->setHasBranchProtectedScope();
4694 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setParentCancelRegion(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
4695
4696 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4697 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
4698}
4699
4700StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4701 Stmt *AStmt,
4702 SourceLocation StartLoc,
4703 SourceLocation EndLoc) {
4704 if (!AStmt)
4705 return StmtError();
4706
4707 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4707, __PRETTY_FUNCTION__))
;
4708
4709 getCurFunction()->setHasBranchProtectedScope();
4710
4711 // OpenMP [2.7.3, single Construct, Restrictions]
4712 // The copyprivate clause must not be used with the nowait clause.
4713 OMPClause *Nowait = nullptr;
4714 OMPClause *Copyprivate = nullptr;
4715 for (auto *Clause : Clauses) {
4716 if (Clause->getClauseKind() == OMPC_nowait)
4717 Nowait = Clause;
4718 else if (Clause->getClauseKind() == OMPC_copyprivate)
4719 Copyprivate = Clause;
4720 if (Copyprivate && Nowait) {
4721 Diag(Copyprivate->getLocStart(),
4722 diag::err_omp_single_copyprivate_with_nowait);
4723 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4724 return StmtError();
4725 }
4726 }
4727
4728 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4729}
4730
4731StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4732 SourceLocation StartLoc,
4733 SourceLocation EndLoc) {
4734 if (!AStmt)
4735 return StmtError();
4736
4737 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4737, __PRETTY_FUNCTION__))
;
4738
4739 getCurFunction()->setHasBranchProtectedScope();
4740
4741 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4742}
4743
4744StmtResult Sema::ActOnOpenMPCriticalDirective(
4745 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4746 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
4747 if (!AStmt)
4748 return StmtError();
4749
4750 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4750, __PRETTY_FUNCTION__))
;
4751
4752 bool ErrorFound = false;
4753 llvm::APSInt Hint;
4754 SourceLocation HintLoc;
4755 bool DependentHint = false;
4756 for (auto *C : Clauses) {
4757 if (C->getClauseKind() == OMPC_hint) {
4758 if (!DirName.getName()) {
4759 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4760 ErrorFound = true;
4761 }
4762 Expr *E = cast<OMPHintClause>(C)->getHint();
4763 if (E->isTypeDependent() || E->isValueDependent() ||
4764 E->isInstantiationDependent())
4765 DependentHint = true;
4766 else {
4767 Hint = E->EvaluateKnownConstInt(Context);
4768 HintLoc = C->getLocStart();
4769 }
4770 }
4771 }
4772 if (ErrorFound)
4773 return StmtError();
4774 auto Pair = DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCriticalWithHint(DirName);
4775 if (Pair.first && DirName.getName() && !DependentHint) {
4776 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4777 Diag(StartLoc, diag::err_omp_critical_with_hint);
4778 if (HintLoc.isValid()) {
4779 Diag(HintLoc, diag::note_omp_critical_hint_here)
4780 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4781 } else
4782 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4783 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4784 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4785 << 1
4786 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4787 /*Radix=*/10, /*Signed=*/false);
4788 } else
4789 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4790 }
4791 }
4792
4793 getCurFunction()->setHasBranchProtectedScope();
4794
4795 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4796 Clauses, AStmt);
4797 if (!Pair.first && DirName.getName() && !DependentHint)
4798 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->addCriticalWithHint(Dir, Hint);
4799 return Dir;
4800}
4801
4802StmtResult Sema::ActOnOpenMPParallelForDirective(
4803 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4804 SourceLocation EndLoc,
4805 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4806 if (!AStmt)
4807 return StmtError();
4808
4809 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4810 // 1.2.2 OpenMP Language Terminology
4811 // Structured block - An executable statement with a single entry at the
4812 // top and a single exit at the bottom.
4813 // The point of exit cannot be a branch out of the structured block.
4814 // longjmp() and throw() must not violate the entry/exit criteria.
4815 CS->getCapturedDecl()->setNothrow();
4816
4817 OMPLoopDirective::HelperExprs B;
4818 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4819 // define the nested loops number.
4820 unsigned NestedLoopCount =
4821 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4822 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
4823 VarsWithImplicitDSA, B);
4824 if (NestedLoopCount == 0)
4825 return StmtError();
4826
4827 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp parallel for loop exprs were not built") ? static_cast<
void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp parallel for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4828, __PRETTY_FUNCTION__))
4828 "omp parallel for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp parallel for loop exprs were not built") ? static_cast<
void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp parallel for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4828, __PRETTY_FUNCTION__))
;
4829
4830 if (!CurContext->isDependentContext()) {
4831 // Finalize the clauses that need pre-built expressions for CodeGen.
4832 for (auto C : Clauses) {
4833 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4834 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4835 B.NumIterations, *this, CurScope,
4836 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
4837 return StmtError();
4838 }
4839 }
4840
4841 getCurFunction()->setHasBranchProtectedScope();
4842 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
4843 NestedLoopCount, Clauses, AStmt, B,
4844 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
4845}
4846
4847StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4848 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4849 SourceLocation EndLoc,
4850 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4851 if (!AStmt)
4852 return StmtError();
4853
4854 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4855 // 1.2.2 OpenMP Language Terminology
4856 // Structured block - An executable statement with a single entry at the
4857 // top and a single exit at the bottom.
4858 // The point of exit cannot be a branch out of the structured block.
4859 // longjmp() and throw() must not violate the entry/exit criteria.
4860 CS->getCapturedDecl()->setNothrow();
4861
4862 OMPLoopDirective::HelperExprs B;
4863 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4864 // define the nested loops number.
4865 unsigned NestedLoopCount =
4866 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4867 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
4868 VarsWithImplicitDSA, B);
4869 if (NestedLoopCount == 0)
4870 return StmtError();
4871
4872 if (!CurContext->isDependentContext()) {
4873 // Finalize the clauses that need pre-built expressions for CodeGen.
4874 for (auto C : Clauses) {
4875 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4876 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4877 B.NumIterations, *this, CurScope,
4878 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
4879 return StmtError();
4880 }
4881 }
4882
4883 if (checkSimdlenSafelenSpecified(*this, Clauses))
4884 return StmtError();
4885
4886 getCurFunction()->setHasBranchProtectedScope();
4887 return OMPParallelForSimdDirective::Create(
4888 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
4889}
4890
4891StmtResult
4892Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4893 Stmt *AStmt, SourceLocation StartLoc,
4894 SourceLocation EndLoc) {
4895 if (!AStmt)
4896 return StmtError();
4897
4898 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4898, __PRETTY_FUNCTION__))
;
4899 auto BaseStmt = AStmt;
4900 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4901 BaseStmt = CS->getCapturedStmt();
4902 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4903 auto S = C->children();
4904 if (S.begin() == S.end())
4905 return StmtError();
4906 // All associated statements must be '#pragma omp section' except for
4907 // the first one.
4908 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
4909 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4910 if (SectionStmt)
4911 Diag(SectionStmt->getLocStart(),
4912 diag::err_omp_parallel_sections_substmt_not_section);
4913 return StmtError();
4914 }
4915 cast<OMPSectionDirective>(SectionStmt)
4916 ->setHasCancel(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
4917 }
4918 } else {
4919 Diag(AStmt->getLocStart(),
4920 diag::err_omp_parallel_sections_not_compound_stmt);
4921 return StmtError();
4922 }
4923
4924 getCurFunction()->setHasBranchProtectedScope();
4925
4926 return OMPParallelSectionsDirective::Create(
4927 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
4928}
4929
4930StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4931 Stmt *AStmt, SourceLocation StartLoc,
4932 SourceLocation EndLoc) {
4933 if (!AStmt)
4934 return StmtError();
4935
4936 auto *CS = cast<CapturedStmt>(AStmt);
4937 // 1.2.2 OpenMP Language Terminology
4938 // Structured block - An executable statement with a single entry at the
4939 // top and a single exit at the bottom.
4940 // The point of exit cannot be a branch out of the structured block.
4941 // longjmp() and throw() must not violate the entry/exit criteria.
4942 CS->getCapturedDecl()->setNothrow();
4943
4944 getCurFunction()->setHasBranchProtectedScope();
4945
4946 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4947 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
4948}
4949
4950StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4951 SourceLocation EndLoc) {
4952 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4953}
4954
4955StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4956 SourceLocation EndLoc) {
4957 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4958}
4959
4960StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4961 SourceLocation EndLoc) {
4962 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4963}
4964
4965StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4966 SourceLocation StartLoc,
4967 SourceLocation EndLoc) {
4968 if (!AStmt)
4969 return StmtError();
4970
4971 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4971, __PRETTY_FUNCTION__))
;
4972
4973 getCurFunction()->setHasBranchProtectedScope();
4974
4975 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4976}
4977
4978StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4979 SourceLocation StartLoc,
4980 SourceLocation EndLoc) {
4981 assert(Clauses.size() <= 1 && "Extra clauses in flush directive")((Clauses.size() <= 1 && "Extra clauses in flush directive"
) ? static_cast<void> (0) : __assert_fail ("Clauses.size() <= 1 && \"Extra clauses in flush directive\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 4981, __PRETTY_FUNCTION__))
;
4982 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4983}
4984
4985StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4986 Stmt *AStmt,
4987 SourceLocation StartLoc,
4988 SourceLocation EndLoc) {
4989 OMPClause *DependFound = nullptr;
4990 OMPClause *DependSourceClause = nullptr;
4991 OMPClause *DependSinkClause = nullptr;
4992 bool ErrorFound = false;
4993 OMPThreadsClause *TC = nullptr;
4994 OMPSIMDClause *SC = nullptr;
4995 for (auto *C : Clauses) {
4996 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4997 DependFound = C;
4998 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4999 if (DependSourceClause) {
5000 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5001 << getOpenMPDirectiveName(OMPD_ordered)
5002 << getOpenMPClauseName(OMPC_depend) << 2;
5003 ErrorFound = true;
5004 } else
5005 DependSourceClause = C;
5006 if (DependSinkClause) {
5007 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5008 << 0;
5009 ErrorFound = true;
5010 }
5011 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5012 if (DependSourceClause) {
5013 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5014 << 1;
5015 ErrorFound = true;
5016 }
5017 DependSinkClause = C;
5018 }
5019 } else if (C->getClauseKind() == OMPC_threads)
5020 TC = cast<OMPThreadsClause>(C);
5021 else if (C->getClauseKind() == OMPC_simd)
5022 SC = cast<OMPSIMDClause>(C);
5023 }
5024 if (!ErrorFound && !SC &&
5025 isOpenMPSimdDirective(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getParentDirective())) {
5026 // OpenMP [2.8.1,simd Construct, Restrictions]
5027 // An ordered construct with the simd clause is the only OpenMP construct
5028 // that can appear in the simd region.
5029 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
5030 ErrorFound = true;
5031 } else if (DependFound && (TC || SC)) {
5032 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5033 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5034 ErrorFound = true;
5035 } else if (DependFound && !DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getParentOrderedRegionParam()) {
5036 Diag(DependFound->getLocStart(),
5037 diag::err_omp_ordered_directive_without_param);
5038 ErrorFound = true;
5039 } else if (TC || Clauses.empty()) {
5040 if (auto *Param = DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getParentOrderedRegionParam()) {
5041 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5042 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5043 << (TC != nullptr);
5044 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5045 ErrorFound = true;
5046 }
5047 }
5048 if ((!AStmt && !DependFound) || ErrorFound)
5049 return StmtError();
5050
5051 if (AStmt) {
5052 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 5052, __PRETTY_FUNCTION__))
;
5053
5054 getCurFunction()->setHasBranchProtectedScope();
5055 }
5056
5057 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5058}
5059
5060namespace {
5061/// \brief Helper class for checking expression in 'omp atomic [update]'
5062/// construct.
5063class OpenMPAtomicUpdateChecker {
5064 /// \brief Error results for atomic update expressions.
5065 enum ExprAnalysisErrorCode {
5066 /// \brief A statement is not an expression statement.
5067 NotAnExpression,
5068 /// \brief Expression is not builtin binary or unary operation.
5069 NotABinaryOrUnaryExpression,
5070 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5071 NotAnUnaryIncDecExpression,
5072 /// \brief An expression is not of scalar type.
5073 NotAScalarType,
5074 /// \brief A binary operation is not an assignment operation.
5075 NotAnAssignmentOp,
5076 /// \brief RHS part of the binary operation is not a binary expression.
5077 NotABinaryExpression,
5078 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5079 /// expression.
5080 NotABinaryOperator,
5081 /// \brief RHS binary operation does not have reference to the updated LHS
5082 /// part.
5083 NotAnUpdateExpression,
5084 /// \brief No errors is found.
5085 NoError
5086 };
5087 /// \brief Reference to Sema.
5088 Sema &SemaRef;
5089 /// \brief A location for note diagnostics (when error is found).
5090 SourceLocation NoteLoc;
5091 /// \brief 'x' lvalue part of the source atomic expression.
5092 Expr *X;
5093 /// \brief 'expr' rvalue part of the source atomic expression.
5094 Expr *E;
5095 /// \brief Helper expression of the form
5096 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5097 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5098 Expr *UpdateExpr;
5099 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5100 /// important for non-associative operations.
5101 bool IsXLHSInRHSPart;
5102 BinaryOperatorKind Op;
5103 SourceLocation OpLoc;
5104 /// \brief true if the source expression is a postfix unary operation, false
5105 /// if it is a prefix unary operation.
5106 bool IsPostfixUpdate;
5107
5108public:
5109 OpenMPAtomicUpdateChecker(Sema &SemaRef)
5110 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
5111 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
5112 /// \brief Check specified statement that it is suitable for 'atomic update'
5113 /// constructs and extract 'x', 'expr' and Operation from the original
5114 /// expression. If DiagId and NoteId == 0, then only check is performed
5115 /// without error notification.
5116 /// \param DiagId Diagnostic which should be emitted if error is found.
5117 /// \param NoteId Diagnostic note for the main error message.
5118 /// \return true if statement is not an update expression, false otherwise.
5119 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
5120 /// \brief Return the 'x' lvalue part of the source atomic expression.
5121 Expr *getX() const { return X; }
5122 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5123 Expr *getExpr() const { return E; }
5124 /// \brief Return the update expression used in calculation of the updated
5125 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5126 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5127 Expr *getUpdateExpr() const { return UpdateExpr; }
5128 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5129 /// false otherwise.
5130 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5131
5132 /// \brief true if the source expression is a postfix unary operation, false
5133 /// if it is a prefix unary operation.
5134 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5135
5136private:
5137 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5138 unsigned NoteId = 0);
5139};
5140} // namespace
5141
5142bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5143 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5144 ExprAnalysisErrorCode ErrorFound = NoError;
5145 SourceLocation ErrorLoc, NoteLoc;
5146 SourceRange ErrorRange, NoteRange;
5147 // Allowed constructs are:
5148 // x = x binop expr;
5149 // x = expr binop x;
5150 if (AtomicBinOp->getOpcode() == BO_Assign) {
5151 X = AtomicBinOp->getLHS();
5152 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5153 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5154 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5155 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5156 AtomicInnerBinOp->isBitwiseOp()) {
5157 Op = AtomicInnerBinOp->getOpcode();
5158 OpLoc = AtomicInnerBinOp->getOperatorLoc();
5159 auto *LHS = AtomicInnerBinOp->getLHS();
5160 auto *RHS = AtomicInnerBinOp->getRHS();
5161 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5162 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5163 /*Canonical=*/true);
5164 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5165 /*Canonical=*/true);
5166 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5167 /*Canonical=*/true);
5168 if (XId == LHSId) {
5169 E = RHS;
5170 IsXLHSInRHSPart = true;
5171 } else if (XId == RHSId) {
5172 E = LHS;
5173 IsXLHSInRHSPart = false;
5174 } else {
5175 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5176 ErrorRange = AtomicInnerBinOp->getSourceRange();
5177 NoteLoc = X->getExprLoc();
5178 NoteRange = X->getSourceRange();
5179 ErrorFound = NotAnUpdateExpression;
5180 }
5181 } else {
5182 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5183 ErrorRange = AtomicInnerBinOp->getSourceRange();
5184 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5185 NoteRange = SourceRange(NoteLoc, NoteLoc);
5186 ErrorFound = NotABinaryOperator;
5187 }
5188 } else {
5189 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5190 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5191 ErrorFound = NotABinaryExpression;
5192 }
5193 } else {
5194 ErrorLoc = AtomicBinOp->getExprLoc();
5195 ErrorRange = AtomicBinOp->getSourceRange();
5196 NoteLoc = AtomicBinOp->getOperatorLoc();
5197 NoteRange = SourceRange(NoteLoc, NoteLoc);
5198 ErrorFound = NotAnAssignmentOp;
5199 }
5200 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5201 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5202 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5203 return true;
5204 } else if (SemaRef.CurContext->isDependentContext())
5205 E = X = UpdateExpr = nullptr;
5206 return ErrorFound != NoError;
5207}
5208
5209bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5210 unsigned NoteId) {
5211 ExprAnalysisErrorCode ErrorFound = NoError;
5212 SourceLocation ErrorLoc, NoteLoc;
5213 SourceRange ErrorRange, NoteRange;
5214 // Allowed constructs are:
5215 // x++;
5216 // x--;
5217 // ++x;
5218 // --x;
5219 // x binop= expr;
5220 // x = x binop expr;
5221 // x = expr binop x;
5222 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5223 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5224 if (AtomicBody->getType()->isScalarType() ||
5225 AtomicBody->isInstantiationDependent()) {
5226 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5227 AtomicBody->IgnoreParenImpCasts())) {
5228 // Check for Compound Assignment Operation
5229 Op = BinaryOperator::getOpForCompoundAssignment(
5230 AtomicCompAssignOp->getOpcode());
5231 OpLoc = AtomicCompAssignOp->getOperatorLoc();
5232 E = AtomicCompAssignOp->getRHS();
5233 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
5234 IsXLHSInRHSPart = true;
5235 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5236 AtomicBody->IgnoreParenImpCasts())) {
5237 // Check for Binary Operation
5238 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5239 return true;
5240 } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
5241 AtomicBody->IgnoreParenImpCasts())) {
5242 // Check for Unary Operation
5243 if (AtomicUnaryOp->isIncrementDecrementOp()) {
5244 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
5245 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5246 OpLoc = AtomicUnaryOp->getOperatorLoc();
5247 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
5248 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5249 IsXLHSInRHSPart = true;
5250 } else {
5251 ErrorFound = NotAnUnaryIncDecExpression;
5252 ErrorLoc = AtomicUnaryOp->getExprLoc();
5253 ErrorRange = AtomicUnaryOp->getSourceRange();
5254 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5255 NoteRange = SourceRange(NoteLoc, NoteLoc);
5256 }
5257 } else if (!AtomicBody->isInstantiationDependent()) {
5258 ErrorFound = NotABinaryOrUnaryExpression;
5259 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5260 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5261 }
5262 } else {
5263 ErrorFound = NotAScalarType;
5264 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5265 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5266 }
5267 } else {
5268 ErrorFound = NotAnExpression;
5269 NoteLoc = ErrorLoc = S->getLocStart();
5270 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5271 }
5272 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5273 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5274 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5275 return true;
5276 } else if (SemaRef.CurContext->isDependentContext())
5277 E = X = UpdateExpr = nullptr;
5278 if (ErrorFound == NoError && E && X) {
5279 // Build an update expression of form 'OpaqueValueExpr(x) binop
5280 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5281 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5282 auto *OVEX = new (SemaRef.getASTContext())
5283 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5284 auto *OVEExpr = new (SemaRef.getASTContext())
5285 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5286 auto Update =
5287 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5288 IsXLHSInRHSPart ? OVEExpr : OVEX);
5289 if (Update.isInvalid())
5290 return true;
5291 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5292 Sema::AA_Casting);
5293 if (Update.isInvalid())
5294 return true;
5295 UpdateExpr = Update.get();
5296 }
5297 return ErrorFound != NoError;
5298}
5299
5300StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5301 Stmt *AStmt,
5302 SourceLocation StartLoc,
5303 SourceLocation EndLoc) {
5304 if (!AStmt)
5305 return StmtError();
5306
5307 auto *CS = cast<CapturedStmt>(AStmt);
5308 // 1.2.2 OpenMP Language Terminology
5309 // Structured block - An executable statement with a single entry at the
5310 // top and a single exit at the bottom.
5311 // The point of exit cannot be a branch out of the structured block.
5312 // longjmp() and throw() must not violate the entry/exit criteria.
5313 OpenMPClauseKind AtomicKind = OMPC_unknown;
5314 SourceLocation AtomicKindLoc;
5315 for (auto *C : Clauses) {
5316 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
5317 C->getClauseKind() == OMPC_update ||
5318 C->getClauseKind() == OMPC_capture) {
5319 if (AtomicKind != OMPC_unknown) {
5320 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5321 << SourceRange(C->getLocStart(), C->getLocEnd());
5322 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5323 << getOpenMPClauseName(AtomicKind);
5324 } else {
5325 AtomicKind = C->getClauseKind();
5326 AtomicKindLoc = C->getLocStart();
5327 }
5328 }
5329 }
5330
5331 auto Body = CS->getCapturedStmt();
5332 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5333 Body = EWC->getSubExpr();
5334
5335 Expr *X = nullptr;
5336 Expr *V = nullptr;
5337 Expr *E = nullptr;
5338 Expr *UE = nullptr;
5339 bool IsXLHSInRHSPart = false;
5340 bool IsPostfixUpdate = false;
5341 // OpenMP [2.12.6, atomic Construct]
5342 // In the next expressions:
5343 // * x and v (as applicable) are both l-value expressions with scalar type.
5344 // * During the execution of an atomic region, multiple syntactic
5345 // occurrences of x must designate the same storage location.
5346 // * Neither of v and expr (as applicable) may access the storage location
5347 // designated by x.
5348 // * Neither of x and expr (as applicable) may access the storage location
5349 // designated by v.
5350 // * expr is an expression with scalar type.
5351 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5352 // * binop, binop=, ++, and -- are not overloaded operators.
5353 // * The expression x binop expr must be numerically equivalent to x binop
5354 // (expr). This requirement is satisfied if the operators in expr have
5355 // precedence greater than binop, or by using parentheses around expr or
5356 // subexpressions of expr.
5357 // * The expression expr binop x must be numerically equivalent to (expr)
5358 // binop x. This requirement is satisfied if the operators in expr have
5359 // precedence equal to or greater than binop, or by using parentheses around
5360 // expr or subexpressions of expr.
5361 // * For forms that allow multiple occurrences of x, the number of times
5362 // that x is evaluated is unspecified.
5363 if (AtomicKind == OMPC_read) {
5364 enum {
5365 NotAnExpression,
5366 NotAnAssignmentOp,
5367 NotAScalarType,
5368 NotAnLValue,
5369 NoError
5370 } ErrorFound = NoError;
5371 SourceLocation ErrorLoc, NoteLoc;
5372 SourceRange ErrorRange, NoteRange;
5373 // If clause is read:
5374 // v = x;
5375 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5376 auto *AtomicBinOp =
5377 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5378 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5379 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5380 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5381 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5382 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5383 if (!X->isLValue() || !V->isLValue()) {
5384 auto NotLValueExpr = X->isLValue() ? V : X;
5385 ErrorFound = NotAnLValue;
5386 ErrorLoc = AtomicBinOp->getExprLoc();
5387 ErrorRange = AtomicBinOp->getSourceRange();
5388 NoteLoc = NotLValueExpr->getExprLoc();
5389 NoteRange = NotLValueExpr->getSourceRange();
5390 }
5391 } else if (!X->isInstantiationDependent() ||
5392 !V->isInstantiationDependent()) {
5393 auto NotScalarExpr =
5394 (X->isInstantiationDependent() || X->getType()->isScalarType())
5395 ? V
5396 : X;
5397 ErrorFound = NotAScalarType;
5398 ErrorLoc = AtomicBinOp->getExprLoc();
5399 ErrorRange = AtomicBinOp->getSourceRange();
5400 NoteLoc = NotScalarExpr->getExprLoc();
5401 NoteRange = NotScalarExpr->getSourceRange();
5402 }
5403 } else if (!AtomicBody->isInstantiationDependent()) {
5404 ErrorFound = NotAnAssignmentOp;
5405 ErrorLoc = AtomicBody->getExprLoc();
5406 ErrorRange = AtomicBody->getSourceRange();
5407 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5408 : AtomicBody->getExprLoc();
5409 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5410 : AtomicBody->getSourceRange();
5411 }
5412 } else {
5413 ErrorFound = NotAnExpression;
5414 NoteLoc = ErrorLoc = Body->getLocStart();
5415 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5416 }
5417 if (ErrorFound != NoError) {
5418 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5419 << ErrorRange;
5420 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5421 << NoteRange;
5422 return StmtError();
5423 } else if (CurContext->isDependentContext())
5424 V = X = nullptr;
5425 } else if (AtomicKind == OMPC_write) {
5426 enum {
5427 NotAnExpression,
5428 NotAnAssignmentOp,
5429 NotAScalarType,
5430 NotAnLValue,
5431 NoError
5432 } ErrorFound = NoError;
5433 SourceLocation ErrorLoc, NoteLoc;
5434 SourceRange ErrorRange, NoteRange;
5435 // If clause is write:
5436 // x = expr;
5437 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5438 auto *AtomicBinOp =
5439 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5440 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5441 X = AtomicBinOp->getLHS();
5442 E = AtomicBinOp->getRHS();
5443 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5444 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5445 if (!X->isLValue()) {
5446 ErrorFound = NotAnLValue;
5447 ErrorLoc = AtomicBinOp->getExprLoc();
5448 ErrorRange = AtomicBinOp->getSourceRange();
5449 NoteLoc = X->getExprLoc();
5450 NoteRange = X->getSourceRange();
5451 }
5452 } else if (!X->isInstantiationDependent() ||
5453 !E->isInstantiationDependent()) {
5454 auto NotScalarExpr =
5455 (X->isInstantiationDependent() || X->getType()->isScalarType())
5456 ? E
5457 : X;
5458 ErrorFound = NotAScalarType;
5459 ErrorLoc = AtomicBinOp->getExprLoc();
5460 ErrorRange = AtomicBinOp->getSourceRange();
5461 NoteLoc = NotScalarExpr->getExprLoc();
5462 NoteRange = NotScalarExpr->getSourceRange();
5463 }
5464 } else if (!AtomicBody->isInstantiationDependent()) {
5465 ErrorFound = NotAnAssignmentOp;
5466 ErrorLoc = AtomicBody->getExprLoc();
5467 ErrorRange = AtomicBody->getSourceRange();
5468 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5469 : AtomicBody->getExprLoc();
5470 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5471 : AtomicBody->getSourceRange();
5472 }
5473 } else {
5474 ErrorFound = NotAnExpression;
5475 NoteLoc = ErrorLoc = Body->getLocStart();
5476 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5477 }
5478 if (ErrorFound != NoError) {
5479 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5480 << ErrorRange;
5481 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5482 << NoteRange;
5483 return StmtError();
5484 } else if (CurContext->isDependentContext())
5485 E = X = nullptr;
5486 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
5487 // If clause is update:
5488 // x++;
5489 // x--;
5490 // ++x;
5491 // --x;
5492 // x binop= expr;
5493 // x = x binop expr;
5494 // x = expr binop x;
5495 OpenMPAtomicUpdateChecker Checker(*this);
5496 if (Checker.checkStatement(
5497 Body, (AtomicKind == OMPC_update)
5498 ? diag::err_omp_atomic_update_not_expression_statement
5499 : diag::err_omp_atomic_not_expression_statement,
5500 diag::note_omp_atomic_update))
5501 return StmtError();
5502 if (!CurContext->isDependentContext()) {
5503 E = Checker.getExpr();
5504 X = Checker.getX();
5505 UE = Checker.getUpdateExpr();
5506 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5507 }
5508 } else if (AtomicKind == OMPC_capture) {
5509 enum {
5510 NotAnAssignmentOp,
5511 NotACompoundStatement,
5512 NotTwoSubstatements,
5513 NotASpecificExpression,
5514 NoError
5515 } ErrorFound = NoError;
5516 SourceLocation ErrorLoc, NoteLoc;
5517 SourceRange ErrorRange, NoteRange;
5518 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5519 // If clause is a capture:
5520 // v = x++;
5521 // v = x--;
5522 // v = ++x;
5523 // v = --x;
5524 // v = x binop= expr;
5525 // v = x = x binop expr;
5526 // v = x = expr binop x;
5527 auto *AtomicBinOp =
5528 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5529 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5530 V = AtomicBinOp->getLHS();
5531 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5532 OpenMPAtomicUpdateChecker Checker(*this);
5533 if (Checker.checkStatement(
5534 Body, diag::err_omp_atomic_capture_not_expression_statement,
5535 diag::note_omp_atomic_update))
5536 return StmtError();
5537 E = Checker.getExpr();
5538 X = Checker.getX();
5539 UE = Checker.getUpdateExpr();
5540 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5541 IsPostfixUpdate = Checker.isPostfixUpdate();
5542 } else if (!AtomicBody->isInstantiationDependent()) {
5543 ErrorLoc = AtomicBody->getExprLoc();
5544 ErrorRange = AtomicBody->getSourceRange();
5545 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5546 : AtomicBody->getExprLoc();
5547 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5548 : AtomicBody->getSourceRange();
5549 ErrorFound = NotAnAssignmentOp;
5550 }
5551 if (ErrorFound != NoError) {
5552 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5553 << ErrorRange;
5554 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5555 return StmtError();
5556 } else if (CurContext->isDependentContext()) {
5557 UE = V = E = X = nullptr;
5558 }
5559 } else {
5560 // If clause is a capture:
5561 // { v = x; x = expr; }
5562 // { v = x; x++; }
5563 // { v = x; x--; }
5564 // { v = x; ++x; }
5565 // { v = x; --x; }
5566 // { v = x; x binop= expr; }
5567 // { v = x; x = x binop expr; }
5568 // { v = x; x = expr binop x; }
5569 // { x++; v = x; }
5570 // { x--; v = x; }
5571 // { ++x; v = x; }
5572 // { --x; v = x; }
5573 // { x binop= expr; v = x; }
5574 // { x = x binop expr; v = x; }
5575 // { x = expr binop x; v = x; }
5576 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5577 // Check that this is { expr1; expr2; }
5578 if (CS->size() == 2) {
5579 auto *First = CS->body_front();
5580 auto *Second = CS->body_back();
5581 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5582 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5583 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5584 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5585 // Need to find what subexpression is 'v' and what is 'x'.
5586 OpenMPAtomicUpdateChecker Checker(*this);
5587 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5588 BinaryOperator *BinOp = nullptr;
5589 if (IsUpdateExprFound) {
5590 BinOp = dyn_cast<BinaryOperator>(First);
5591 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5592 }
5593 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5594 // { v = x; x++; }
5595 // { v = x; x--; }
5596 // { v = x; ++x; }
5597 // { v = x; --x; }
5598 // { v = x; x binop= expr; }
5599 // { v = x; x = x binop expr; }
5600 // { v = x; x = expr binop x; }
5601 // Check that the first expression has form v = x.
5602 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5603 llvm::FoldingSetNodeID XId, PossibleXId;
5604 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5605 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5606 IsUpdateExprFound = XId == PossibleXId;
5607 if (IsUpdateExprFound) {
5608 V = BinOp->getLHS();
5609 X = Checker.getX();
5610 E = Checker.getExpr();
5611 UE = Checker.getUpdateExpr();
5612 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5613 IsPostfixUpdate = true;
5614 }
5615 }
5616 if (!IsUpdateExprFound) {
5617 IsUpdateExprFound = !Checker.checkStatement(First);
5618 BinOp = nullptr;
5619 if (IsUpdateExprFound) {
5620 BinOp = dyn_cast<BinaryOperator>(Second);
5621 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5622 }
5623 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5624 // { x++; v = x; }
5625 // { x--; v = x; }
5626 // { ++x; v = x; }
5627 // { --x; v = x; }
5628 // { x binop= expr; v = x; }
5629 // { x = x binop expr; v = x; }
5630 // { x = expr binop x; v = x; }
5631 // Check that the second expression has form v = x.
5632 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5633 llvm::FoldingSetNodeID XId, PossibleXId;
5634 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5635 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5636 IsUpdateExprFound = XId == PossibleXId;
5637 if (IsUpdateExprFound) {
5638 V = BinOp->getLHS();
5639 X = Checker.getX();
5640 E = Checker.getExpr();
5641 UE = Checker.getUpdateExpr();
5642 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5643 IsPostfixUpdate = false;
5644 }
5645 }
5646 }
5647 if (!IsUpdateExprFound) {
5648 // { v = x; x = expr; }
5649 auto *FirstExpr = dyn_cast<Expr>(First);
5650 auto *SecondExpr = dyn_cast<Expr>(Second);
5651 if (!FirstExpr || !SecondExpr ||
5652 !(FirstExpr->isInstantiationDependent() ||
5653 SecondExpr->isInstantiationDependent())) {
5654 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5655 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
5656 ErrorFound = NotAnAssignmentOp;
5657 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5658 : First->getLocStart();
5659 NoteRange = ErrorRange = FirstBinOp
5660 ? FirstBinOp->getSourceRange()
5661 : SourceRange(ErrorLoc, ErrorLoc);
5662 } else {
5663 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5664 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5665 ErrorFound = NotAnAssignmentOp;
5666 NoteLoc = ErrorLoc = SecondBinOp
5667 ? SecondBinOp->getOperatorLoc()
5668 : Second->getLocStart();
5669 NoteRange = ErrorRange =
5670 SecondBinOp ? SecondBinOp->getSourceRange()
5671 : SourceRange(ErrorLoc, ErrorLoc);
5672 } else {
5673 auto *PossibleXRHSInFirst =
5674 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5675 auto *PossibleXLHSInSecond =
5676 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5677 llvm::FoldingSetNodeID X1Id, X2Id;
5678 PossibleXRHSInFirst->Profile(X1Id, Context,
5679 /*Canonical=*/true);
5680 PossibleXLHSInSecond->Profile(X2Id, Context,
5681 /*Canonical=*/true);
5682 IsUpdateExprFound = X1Id == X2Id;
5683 if (IsUpdateExprFound) {
5684 V = FirstBinOp->getLHS();
5685 X = SecondBinOp->getLHS();
5686 E = SecondBinOp->getRHS();
5687 UE = nullptr;
5688 IsXLHSInRHSPart = false;
5689 IsPostfixUpdate = true;
5690 } else {
5691 ErrorFound = NotASpecificExpression;
5692 ErrorLoc = FirstBinOp->getExprLoc();
5693 ErrorRange = FirstBinOp->getSourceRange();
5694 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5695 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5696 }
5697 }
5698 }
5699 }
5700 }
5701 } else {
5702 NoteLoc = ErrorLoc = Body->getLocStart();
5703 NoteRange = ErrorRange =
5704 SourceRange(Body->getLocStart(), Body->getLocStart());
5705 ErrorFound = NotTwoSubstatements;
5706 }
5707 } else {
5708 NoteLoc = ErrorLoc = Body->getLocStart();
5709 NoteRange = ErrorRange =
5710 SourceRange(Body->getLocStart(), Body->getLocStart());
5711 ErrorFound = NotACompoundStatement;
5712 }
5713 if (ErrorFound != NoError) {
5714 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5715 << ErrorRange;
5716 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5717 return StmtError();
5718 } else if (CurContext->isDependentContext()) {
5719 UE = V = E = X = nullptr;
5720 }
5721 }
5722 }
5723
5724 getCurFunction()->setHasBranchProtectedScope();
5725
5726 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5727 X, V, E, UE, IsXLHSInRHSPart,
5728 IsPostfixUpdate);
5729}
5730
5731StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5732 Stmt *AStmt,
5733 SourceLocation StartLoc,
5734 SourceLocation EndLoc) {
5735 if (!AStmt)
5736 return StmtError();
5737
5738 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5739 // 1.2.2 OpenMP Language Terminology
5740 // Structured block - An executable statement with a single entry at the
5741 // top and a single exit at the bottom.
5742 // The point of exit cannot be a branch out of the structured block.
5743 // longjmp() and throw() must not violate the entry/exit criteria.
5744 CS->getCapturedDecl()->setNothrow();
5745
5746 // OpenMP [2.16, Nesting of Regions]
5747 // If specified, a teams construct must be contained within a target
5748 // construct. That target construct must contain no statements or directives
5749 // outside of the teams construct.
5750 if (DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->hasInnerTeamsRegion()) {
5751 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5752 bool OMPTeamsFound = true;
5753 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5754 auto I = CS->body_begin();
5755 while (I != CS->body_end()) {
5756 auto *OED = dyn_cast<OMPExecutableDirective>(*I);
5757 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5758 OMPTeamsFound = false;
5759 break;
5760 }
5761 ++I;
5762 }
5763 assert(I != CS->body_end() && "Not found statement")((I != CS->body_end() && "Not found statement") ? static_cast
<void> (0) : __assert_fail ("I != CS->body_end() && \"Not found statement\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 5763, __PRETTY_FUNCTION__))
;
5764 S = *I;
5765 } else {
5766 auto *OED = dyn_cast<OMPExecutableDirective>(S);
5767 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
5768 }
5769 if (!OMPTeamsFound) {
5770 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5771 Diag(DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getInnerTeamsRegionLoc(),
5772 diag::note_omp_nested_teams_construct_here);
5773 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5774 << isa<OMPExecutableDirective>(S);
5775 return StmtError();
5776 }
5777 }
5778
5779 getCurFunction()->setHasBranchProtectedScope();
5780
5781 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5782}
5783
5784StmtResult
5785Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5786 Stmt *AStmt, SourceLocation StartLoc,
5787 SourceLocation EndLoc) {
5788 if (!AStmt)
5789 return StmtError();
5790
5791 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5792 // 1.2.2 OpenMP Language Terminology
5793 // Structured block - An executable statement with a single entry at the
5794 // top and a single exit at the bottom.
5795 // The point of exit cannot be a branch out of the structured block.
5796 // longjmp() and throw() must not violate the entry/exit criteria.
5797 CS->getCapturedDecl()->setNothrow();
5798
5799 getCurFunction()->setHasBranchProtectedScope();
5800
5801 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5802 AStmt);
5803}
5804
5805StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5806 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5807 SourceLocation EndLoc,
5808 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5809 if (!AStmt)
5810 return StmtError();
5811
5812 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5813 // 1.2.2 OpenMP Language Terminology
5814 // Structured block - An executable statement with a single entry at the
5815 // top and a single exit at the bottom.
5816 // The point of exit cannot be a branch out of the structured block.
5817 // longjmp() and throw() must not violate the entry/exit criteria.
5818 CS->getCapturedDecl()->setNothrow();
5819
5820 OMPLoopDirective::HelperExprs B;
5821 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5822 // define the nested loops number.
5823 unsigned NestedLoopCount =
5824 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5825 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
5826 VarsWithImplicitDSA, B);
5827 if (NestedLoopCount == 0)
5828 return StmtError();
5829
5830 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target parallel for loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target parallel for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 5831, __PRETTY_FUNCTION__))
5831 "omp target parallel for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target parallel for loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target parallel for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 5831, __PRETTY_FUNCTION__))
;
5832
5833 if (!CurContext->isDependentContext()) {
5834 // Finalize the clauses that need pre-built expressions for CodeGen.
5835 for (auto C : Clauses) {
5836 if (auto *LC = dyn_cast<OMPLinearClause>(C))
5837 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5838 B.NumIterations, *this, CurScope,
5839 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
5840 return StmtError();
5841 }
5842 }
5843
5844 getCurFunction()->setHasBranchProtectedScope();
5845 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5846 NestedLoopCount, Clauses, AStmt,
5847 B, DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isCancelRegion());
5848}
5849
5850/// \brief Check for existence of a map clause in the list of clauses.
5851static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5852 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5853 I != E; ++I) {
5854 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5855 return true;
5856 }
5857 }
5858
5859 return false;
5860}
5861
5862StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5863 Stmt *AStmt,
5864 SourceLocation StartLoc,
5865 SourceLocation EndLoc) {
5866 if (!AStmt)
5867 return StmtError();
5868
5869 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 5869, __PRETTY_FUNCTION__))
;
5870
5871 // OpenMP [2.10.1, Restrictions, p. 97]
5872 // At least one map clause must appear on the directive.
5873 if (!HasMapClause(Clauses)) {
5874 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5875 << getOpenMPDirectiveName(OMPD_target_data);
5876 return StmtError();
5877 }
5878
5879 getCurFunction()->setHasBranchProtectedScope();
5880
5881 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5882 AStmt);
5883}
5884
5885StmtResult
5886Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5887 SourceLocation StartLoc,
5888 SourceLocation EndLoc) {
5889 // OpenMP [2.10.2, Restrictions, p. 99]
5890 // At least one map clause must appear on the directive.
5891 if (!HasMapClause(Clauses)) {
5892 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5893 << getOpenMPDirectiveName(OMPD_target_enter_data);
5894 return StmtError();
5895 }
5896
5897 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5898 Clauses);
5899}
5900
5901StmtResult
5902Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5903 SourceLocation StartLoc,
5904 SourceLocation EndLoc) {
5905 // OpenMP [2.10.3, Restrictions, p. 102]
5906 // At least one map clause must appear on the directive.
5907 if (!HasMapClause(Clauses)) {
5908 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5909 << getOpenMPDirectiveName(OMPD_target_exit_data);
5910 return StmtError();
5911 }
5912
5913 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5914}
5915
5916StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
5917 SourceLocation StartLoc,
5918 SourceLocation EndLoc) {
5919 bool seenMotionClause = false;
5920 for (auto *C : Clauses) {
5921 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
5922 seenMotionClause = true;
5923 }
5924 if (!seenMotionClause) {
5925 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
5926 return StmtError();
5927 }
5928 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
5929}
5930
5931StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5932 Stmt *AStmt, SourceLocation StartLoc,
5933 SourceLocation EndLoc) {
5934 if (!AStmt)
5935 return StmtError();
5936
5937 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5938 // 1.2.2 OpenMP Language Terminology
5939 // Structured block - An executable statement with a single entry at the
5940 // top and a single exit at the bottom.
5941 // The point of exit cannot be a branch out of the structured block.
5942 // longjmp() and throw() must not violate the entry/exit criteria.
5943 CS->getCapturedDecl()->setNothrow();
5944
5945 getCurFunction()->setHasBranchProtectedScope();
5946
5947 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5948}
5949
5950StmtResult
5951Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5952 SourceLocation EndLoc,
5953 OpenMPDirectiveKind CancelRegion) {
5954 if (DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isParentNowaitRegion()) {
5955 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5956 return StmtError();
5957 }
5958 if (DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isParentOrderedRegion()) {
5959 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5960 return StmtError();
5961 }
5962 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5963 CancelRegion);
5964}
5965
5966StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5967 SourceLocation StartLoc,
5968 SourceLocation EndLoc,
5969 OpenMPDirectiveKind CancelRegion) {
5970 if (DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isParentNowaitRegion()) {
5971 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5972 return StmtError();
5973 }
5974 if (DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->isParentOrderedRegion()) {
5975 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5976 return StmtError();
5977 }
5978 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setParentCancelRegion(/*Cancel=*/true);
5979 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5980 CancelRegion);
5981}
5982
5983static bool checkGrainsizeNumTasksClauses(Sema &S,
5984 ArrayRef<OMPClause *> Clauses) {
5985 OMPClause *PrevClause = nullptr;
5986 bool ErrorFound = false;
5987 for (auto *C : Clauses) {
5988 if (C->getClauseKind() == OMPC_grainsize ||
5989 C->getClauseKind() == OMPC_num_tasks) {
5990 if (!PrevClause)
5991 PrevClause = C;
5992 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5993 S.Diag(C->getLocStart(),
5994 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5995 << getOpenMPClauseName(C->getClauseKind())
5996 << getOpenMPClauseName(PrevClause->getClauseKind());
5997 S.Diag(PrevClause->getLocStart(),
5998 diag::note_omp_previous_grainsize_num_tasks)
5999 << getOpenMPClauseName(PrevClause->getClauseKind());
6000 ErrorFound = true;
6001 }
6002 }
6003 }
6004 return ErrorFound;
6005}
6006
6007StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6008 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6009 SourceLocation EndLoc,
6010 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6011 if (!AStmt)
6012 return StmtError();
6013
6014 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6014, __PRETTY_FUNCTION__))
;
6015 OMPLoopDirective::HelperExprs B;
6016 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6017 // define the nested loops number.
6018 unsigned NestedLoopCount =
6019 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
6020 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6021 VarsWithImplicitDSA, B);
6022 if (NestedLoopCount == 0)
6023 return StmtError();
6024
6025 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6026, __PRETTY_FUNCTION__))
6026 "omp for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6026, __PRETTY_FUNCTION__))
;
6027
6028 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6029 // The grainsize clause and num_tasks clause are mutually exclusive and may
6030 // not appear on the same taskloop directive.
6031 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6032 return StmtError();
6033
6034 getCurFunction()->setHasBranchProtectedScope();
6035 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6036 NestedLoopCount, Clauses, AStmt, B);
6037}
6038
6039StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6040 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6041 SourceLocation EndLoc,
6042 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6043 if (!AStmt)
6044 return StmtError();
6045
6046 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6046, __PRETTY_FUNCTION__))
;
6047 OMPLoopDirective::HelperExprs B;
6048 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6049 // define the nested loops number.
6050 unsigned NestedLoopCount =
6051 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6052 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6053 VarsWithImplicitDSA, B);
6054 if (NestedLoopCount == 0)
6055 return StmtError();
6056
6057 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6058, __PRETTY_FUNCTION__))
6058 "omp for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6058, __PRETTY_FUNCTION__))
;
6059
6060 if (!CurContext->isDependentContext()) {
6061 // Finalize the clauses that need pre-built expressions for CodeGen.
6062 for (auto C : Clauses) {
6063 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6064 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6065 B.NumIterations, *this, CurScope,
6066 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
6067 return StmtError();
6068 }
6069 }
6070
6071 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6072 // The grainsize clause and num_tasks clause are mutually exclusive and may
6073 // not appear on the same taskloop directive.
6074 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6075 return StmtError();
6076
6077 getCurFunction()->setHasBranchProtectedScope();
6078 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6079 NestedLoopCount, Clauses, AStmt, B);
6080}
6081
6082StmtResult Sema::ActOnOpenMPDistributeDirective(
6083 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6084 SourceLocation EndLoc,
6085 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6086 if (!AStmt)
6087 return StmtError();
6088
6089 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected")((isa<CapturedStmt>(AStmt) && "Captured statement expected"
) ? static_cast<void> (0) : __assert_fail ("isa<CapturedStmt>(AStmt) && \"Captured statement expected\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6089, __PRETTY_FUNCTION__))
;
6090 OMPLoopDirective::HelperExprs B;
6091 // In presence of clause 'collapse' with number of loops, it will
6092 // define the nested loops number.
6093 unsigned NestedLoopCount =
6094 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6095 nullptr /*ordered not a clause on distribute*/, AStmt,
6096 *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
, VarsWithImplicitDSA, B);
6097 if (NestedLoopCount == 0)
6098 return StmtError();
6099
6100 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6101, __PRETTY_FUNCTION__))
6101 "omp for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6101, __PRETTY_FUNCTION__))
;
6102
6103 getCurFunction()->setHasBranchProtectedScope();
6104 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6105 NestedLoopCount, Clauses, AStmt, B);
6106}
6107
6108StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
6109 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6110 SourceLocation EndLoc,
6111 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6112 if (!AStmt)
6113 return StmtError();
6114
6115 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6116 // 1.2.2 OpenMP Language Terminology
6117 // Structured block - An executable statement with a single entry at the
6118 // top and a single exit at the bottom.
6119 // The point of exit cannot be a branch out of the structured block.
6120 // longjmp() and throw() must not violate the entry/exit criteria.
6121 CS->getCapturedDecl()->setNothrow();
6122
6123 OMPLoopDirective::HelperExprs B;
6124 // In presence of clause 'collapse' with number of loops, it will
6125 // define the nested loops number.
6126 unsigned NestedLoopCount = CheckOpenMPLoop(
6127 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
6128 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6129 VarsWithImplicitDSA, B);
6130 if (NestedLoopCount == 0)
6131 return StmtError();
6132
6133 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6134, __PRETTY_FUNCTION__))
6134 "omp for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6134, __PRETTY_FUNCTION__))
;
6135
6136 getCurFunction()->setHasBranchProtectedScope();
6137 return OMPDistributeParallelForDirective::Create(
6138 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6139}
6140
6141StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
6142 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6143 SourceLocation EndLoc,
6144 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6145 if (!AStmt)
6146 return StmtError();
6147
6148 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6149 // 1.2.2 OpenMP Language Terminology
6150 // Structured block - An executable statement with a single entry at the
6151 // top and a single exit at the bottom.
6152 // The point of exit cannot be a branch out of the structured block.
6153 // longjmp() and throw() must not violate the entry/exit criteria.
6154 CS->getCapturedDecl()->setNothrow();
6155
6156 OMPLoopDirective::HelperExprs B;
6157 // In presence of clause 'collapse' with number of loops, it will
6158 // define the nested loops number.
6159 unsigned NestedLoopCount = CheckOpenMPLoop(
6160 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
6161 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6162 VarsWithImplicitDSA, B);
6163 if (NestedLoopCount == 0)
6164 return StmtError();
6165
6166 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6167, __PRETTY_FUNCTION__))
6167 "omp for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6167, __PRETTY_FUNCTION__))
;
6168
6169 if (checkSimdlenSafelenSpecified(*this, Clauses))
6170 return StmtError();
6171
6172 getCurFunction()->setHasBranchProtectedScope();
6173 return OMPDistributeParallelForSimdDirective::Create(
6174 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6175}
6176
6177StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
6178 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6179 SourceLocation EndLoc,
6180 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6181 if (!AStmt)
6182 return StmtError();
6183
6184 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6185 // 1.2.2 OpenMP Language Terminology
6186 // Structured block - An executable statement with a single entry at the
6187 // top and a single exit at the bottom.
6188 // The point of exit cannot be a branch out of the structured block.
6189 // longjmp() and throw() must not violate the entry/exit criteria.
6190 CS->getCapturedDecl()->setNothrow();
6191
6192 OMPLoopDirective::HelperExprs B;
6193 // In presence of clause 'collapse' with number of loops, it will
6194 // define the nested loops number.
6195 unsigned NestedLoopCount =
6196 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
6197 nullptr /*ordered not a clause on distribute*/, AStmt,
6198 *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
, VarsWithImplicitDSA, B);
6199 if (NestedLoopCount == 0)
6200 return StmtError();
6201
6202 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6203, __PRETTY_FUNCTION__))
6203 "omp for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6203, __PRETTY_FUNCTION__))
;
6204
6205 if (checkSimdlenSafelenSpecified(*this, Clauses))
6206 return StmtError();
6207
6208 getCurFunction()->setHasBranchProtectedScope();
6209 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
6210 NestedLoopCount, Clauses, AStmt, B);
6211}
6212
6213StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
6214 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6215 SourceLocation EndLoc,
6216 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6217 if (!AStmt)
6218 return StmtError();
6219
6220 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6221 // 1.2.2 OpenMP Language Terminology
6222 // Structured block - An executable statement with a single entry at the
6223 // top and a single exit at the bottom.
6224 // The point of exit cannot be a branch out of the structured block.
6225 // longjmp() and throw() must not violate the entry/exit criteria.
6226 CS->getCapturedDecl()->setNothrow();
6227
6228 OMPLoopDirective::HelperExprs B;
6229 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6230 // define the nested loops number.
6231 unsigned NestedLoopCount = CheckOpenMPLoop(
6232 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
6233 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6234 VarsWithImplicitDSA, B);
6235 if (NestedLoopCount == 0)
6236 return StmtError();
6237
6238 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target parallel for simd loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target parallel for simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6239, __PRETTY_FUNCTION__))
6239 "omp target parallel for simd loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target parallel for simd loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target parallel for simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6239, __PRETTY_FUNCTION__))
;
6240
6241 if (!CurContext->isDependentContext()) {
6242 // Finalize the clauses that need pre-built expressions for CodeGen.
6243 for (auto C : Clauses) {
6244 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6245 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6246 B.NumIterations, *this, CurScope,
6247 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
6248 return StmtError();
6249 }
6250 }
6251 if (checkSimdlenSafelenSpecified(*this, Clauses))
6252 return StmtError();
6253
6254 getCurFunction()->setHasBranchProtectedScope();
6255 return OMPTargetParallelForSimdDirective::Create(
6256 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6257}
6258
6259StmtResult Sema::ActOnOpenMPTargetSimdDirective(
6260 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6261 SourceLocation EndLoc,
6262 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6263 if (!AStmt)
6264 return StmtError();
6265
6266 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6267 // 1.2.2 OpenMP Language Terminology
6268 // Structured block - An executable statement with a single entry at the
6269 // top and a single exit at the bottom.
6270 // The point of exit cannot be a branch out of the structured block.
6271 // longjmp() and throw() must not violate the entry/exit criteria.
6272 CS->getCapturedDecl()->setNothrow();
6273
6274 OMPLoopDirective::HelperExprs B;
6275 // In presence of clause 'collapse' with number of loops, it will define the
6276 // nested loops number.
6277 unsigned NestedLoopCount =
6278 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
6279 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6280 VarsWithImplicitDSA, B);
6281 if (NestedLoopCount == 0)
6282 return StmtError();
6283
6284 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target simd loop exprs were not built") ? static_cast<
void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6285, __PRETTY_FUNCTION__))
6285 "omp target simd loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target simd loop exprs were not built") ? static_cast<
void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6285, __PRETTY_FUNCTION__))
;
6286
6287 if (!CurContext->isDependentContext()) {
6288 // Finalize the clauses that need pre-built expressions for CodeGen.
6289 for (auto C : Clauses) {
6290 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6291 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6292 B.NumIterations, *this, CurScope,
6293 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
6294 return StmtError();
6295 }
6296 }
6297
6298 if (checkSimdlenSafelenSpecified(*this, Clauses))
6299 return StmtError();
6300
6301 getCurFunction()->setHasBranchProtectedScope();
6302 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
6303 NestedLoopCount, Clauses, AStmt, B);
6304}
6305
6306StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
6307 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6308 SourceLocation EndLoc,
6309 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6310 if (!AStmt)
6311 return StmtError();
6312
6313 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6314 // 1.2.2 OpenMP Language Terminology
6315 // Structured block - An executable statement with a single entry at the
6316 // top and a single exit at the bottom.
6317 // The point of exit cannot be a branch out of the structured block.
6318 // longjmp() and throw() must not violate the entry/exit criteria.
6319 CS->getCapturedDecl()->setNothrow();
6320
6321 OMPLoopDirective::HelperExprs B;
6322 // In presence of clause 'collapse' with number of loops, it will
6323 // define the nested loops number.
6324 unsigned NestedLoopCount =
6325 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
6326 nullptr /*ordered not a clause on distribute*/, AStmt,
6327 *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
, VarsWithImplicitDSA, B);
6328 if (NestedLoopCount == 0)
6329 return StmtError();
6330
6331 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp teams distribute loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp teams distribute loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6332, __PRETTY_FUNCTION__))
6332 "omp teams distribute loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp teams distribute loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp teams distribute loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6332, __PRETTY_FUNCTION__))
;
6333
6334 getCurFunction()->setHasBranchProtectedScope();
6335 return OMPTeamsDistributeDirective::Create(
6336 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6337}
6338
6339StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
6340 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6341 SourceLocation EndLoc,
6342 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6343 if (!AStmt)
6344 return StmtError();
6345
6346 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6347 // 1.2.2 OpenMP Language Terminology
6348 // Structured block - An executable statement with a single entry at the
6349 // top and a single exit at the bottom.
6350 // The point of exit cannot be a branch out of the structured block.
6351 // longjmp() and throw() must not violate the entry/exit criteria.
6352 CS->getCapturedDecl()->setNothrow();
6353
6354 OMPLoopDirective::HelperExprs B;
6355 // In presence of clause 'collapse' with number of loops, it will
6356 // define the nested loops number.
6357 unsigned NestedLoopCount = CheckOpenMPLoop(
6358 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
6359 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6360 VarsWithImplicitDSA, B);
6361
6362 if (NestedLoopCount == 0)
6363 return StmtError();
6364
6365 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp teams distribute simd loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp teams distribute simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6366, __PRETTY_FUNCTION__))
6366 "omp teams distribute simd loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp teams distribute simd loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp teams distribute simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6366, __PRETTY_FUNCTION__))
;
6367
6368 if (!CurContext->isDependentContext()) {
6369 // Finalize the clauses that need pre-built expressions for CodeGen.
6370 for (auto C : Clauses) {
6371 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6372 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6373 B.NumIterations, *this, CurScope,
6374 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
6375 return StmtError();
6376 }
6377 }
6378
6379 if (checkSimdlenSafelenSpecified(*this, Clauses))
6380 return StmtError();
6381
6382 getCurFunction()->setHasBranchProtectedScope();
6383 return OMPTeamsDistributeSimdDirective::Create(
6384 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6385}
6386
6387StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
6388 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6389 SourceLocation EndLoc,
6390 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6391 if (!AStmt)
6392 return StmtError();
6393
6394 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6395 // 1.2.2 OpenMP Language Terminology
6396 // Structured block - An executable statement with a single entry at the
6397 // top and a single exit at the bottom.
6398 // The point of exit cannot be a branch out of the structured block.
6399 // longjmp() and throw() must not violate the entry/exit criteria.
6400 CS->getCapturedDecl()->setNothrow();
6401
6402 OMPLoopDirective::HelperExprs B;
6403 // In presence of clause 'collapse' with number of loops, it will
6404 // define the nested loops number.
6405 auto NestedLoopCount = CheckOpenMPLoop(
6406 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
6407 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6408 VarsWithImplicitDSA, B);
6409
6410 if (NestedLoopCount == 0)
6411 return StmtError();
6412
6413 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6414, __PRETTY_FUNCTION__))
6414 "omp for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6414, __PRETTY_FUNCTION__))
;
6415
6416 if (!CurContext->isDependentContext()) {
6417 // Finalize the clauses that need pre-built expressions for CodeGen.
6418 for (auto C : Clauses) {
6419 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6420 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6421 B.NumIterations, *this, CurScope,
6422 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
6423 return StmtError();
6424 }
6425 }
6426
6427 if (checkSimdlenSafelenSpecified(*this, Clauses))
6428 return StmtError();
6429
6430 getCurFunction()->setHasBranchProtectedScope();
6431 return OMPTeamsDistributeParallelForSimdDirective::Create(
6432 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6433}
6434
6435StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
6436 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6437 SourceLocation EndLoc,
6438 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6439 if (!AStmt)
6440 return StmtError();
6441
6442 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6443 // 1.2.2 OpenMP Language Terminology
6444 // Structured block - An executable statement with a single entry at the
6445 // top and a single exit at the bottom.
6446 // The point of exit cannot be a branch out of the structured block.
6447 // longjmp() and throw() must not violate the entry/exit criteria.
6448 CS->getCapturedDecl()->setNothrow();
6449
6450 OMPLoopDirective::HelperExprs B;
6451 // In presence of clause 'collapse' with number of loops, it will
6452 // define the nested loops number.
6453 unsigned NestedLoopCount = CheckOpenMPLoop(
6454 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
6455 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6456 VarsWithImplicitDSA, B);
6457
6458 if (NestedLoopCount == 0)
6459 return StmtError();
6460
6461 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6462, __PRETTY_FUNCTION__))
6462 "omp for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp for loop exprs were not built") ? static_cast<void>
(0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6462, __PRETTY_FUNCTION__))
;
6463
6464 if (!CurContext->isDependentContext()) {
6465 // Finalize the clauses that need pre-built expressions for CodeGen.
6466 for (auto C : Clauses) {
6467 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6468 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6469 B.NumIterations, *this, CurScope,
6470 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
6471 return StmtError();
6472 }
6473 }
6474
6475 getCurFunction()->setHasBranchProtectedScope();
6476 return OMPTeamsDistributeParallelForDirective::Create(
6477 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6478}
6479
6480StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
6481 Stmt *AStmt,
6482 SourceLocation StartLoc,
6483 SourceLocation EndLoc) {
6484 if (!AStmt)
6485 return StmtError();
6486
6487 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6488 // 1.2.2 OpenMP Language Terminology
6489 // Structured block - An executable statement with a single entry at the
6490 // top and a single exit at the bottom.
6491 // The point of exit cannot be a branch out of the structured block.
6492 // longjmp() and throw() must not violate the entry/exit criteria.
6493 CS->getCapturedDecl()->setNothrow();
6494
6495 getCurFunction()->setHasBranchProtectedScope();
6496
6497 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
6498 AStmt);
6499}
6500
6501StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
6502 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6503 SourceLocation EndLoc,
6504 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6505 if (!AStmt)
6506 return StmtError();
6507
6508 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6509 // 1.2.2 OpenMP Language Terminology
6510 // Structured block - An executable statement with a single entry at the
6511 // top and a single exit at the bottom.
6512 // The point of exit cannot be a branch out of the structured block.
6513 // longjmp() and throw() must not violate the entry/exit criteria.
6514 CS->getCapturedDecl()->setNothrow();
6515
6516 OMPLoopDirective::HelperExprs B;
6517 // In presence of clause 'collapse' with number of loops, it will
6518 // define the nested loops number.
6519 auto NestedLoopCount = CheckOpenMPLoop(
6520 OMPD_target_teams_distribute,
6521 getCollapseNumberExpr(Clauses),
6522 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6523 VarsWithImplicitDSA, B);
6524 if (NestedLoopCount == 0)
6525 return StmtError();
6526
6527 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target teams distribute loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target teams distribute loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6528, __PRETTY_FUNCTION__))
6528 "omp target teams distribute loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target teams distribute loop exprs were not built") ? static_cast
<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target teams distribute loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6528, __PRETTY_FUNCTION__))
;
6529
6530 getCurFunction()->setHasBranchProtectedScope();
6531 return OMPTargetTeamsDistributeDirective::Create(
6532 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6533}
6534
6535StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
6536 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6537 SourceLocation EndLoc,
6538 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6539 if (!AStmt)
6540 return StmtError();
6541
6542 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6543 // 1.2.2 OpenMP Language Terminology
6544 // Structured block - An executable statement with a single entry at the
6545 // top and a single exit at the bottom.
6546 // The point of exit cannot be a branch out of the structured block.
6547 // longjmp() and throw() must not violate the entry/exit criteria.
6548 CS->getCapturedDecl()->setNothrow();
6549
6550 OMPLoopDirective::HelperExprs B;
6551 // In presence of clause 'collapse' with number of loops, it will
6552 // define the nested loops number.
6553 auto NestedLoopCount = CheckOpenMPLoop(
6554 OMPD_target_teams_distribute_parallel_for,
6555 getCollapseNumberExpr(Clauses),
6556 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6557 VarsWithImplicitDSA, B);
6558 if (NestedLoopCount == 0)
6559 return StmtError();
6560
6561 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target teams distribute parallel for loop exprs were not built"
) ? static_cast<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target teams distribute parallel for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6562, __PRETTY_FUNCTION__))
6562 "omp target teams distribute parallel for loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target teams distribute parallel for loop exprs were not built"
) ? static_cast<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target teams distribute parallel for loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6562, __PRETTY_FUNCTION__))
;
6563
6564 if (!CurContext->isDependentContext()) {
6565 // Finalize the clauses that need pre-built expressions for CodeGen.
6566 for (auto C : Clauses) {
6567 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6568 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6569 B.NumIterations, *this, CurScope,
6570 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
6571 return StmtError();
6572 }
6573 }
6574
6575 getCurFunction()->setHasBranchProtectedScope();
6576 return OMPTargetTeamsDistributeParallelForDirective::Create(
6577 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6578}
6579
6580StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
6581 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6582 SourceLocation EndLoc,
6583 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6584 if (!AStmt)
6585 return StmtError();
6586
6587 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6588 // 1.2.2 OpenMP Language Terminology
6589 // Structured block - An executable statement with a single entry at the
6590 // top and a single exit at the bottom.
6591 // The point of exit cannot be a branch out of the structured block.
6592 // longjmp() and throw() must not violate the entry/exit criteria.
6593 CS->getCapturedDecl()->setNothrow();
6594
6595 OMPLoopDirective::HelperExprs B;
6596 // In presence of clause 'collapse' with number of loops, it will
6597 // define the nested loops number.
6598 auto NestedLoopCount = CheckOpenMPLoop(
6599 OMPD_target_teams_distribute_parallel_for_simd,
6600 getCollapseNumberExpr(Clauses),
6601 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6602 VarsWithImplicitDSA, B);
6603 if (NestedLoopCount == 0)
6604 return StmtError();
6605
6606 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target teams distribute parallel for simd loop exprs were not "
"built") ? static_cast<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target teams distribute parallel for simd loop exprs were not \" \"built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6608, __PRETTY_FUNCTION__))
6607 "omp target teams distribute parallel for simd loop exprs were not "(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target teams distribute parallel for simd loop exprs were not "
"built") ? static_cast<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target teams distribute parallel for simd loop exprs were not \" \"built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6608, __PRETTY_FUNCTION__))
6608 "built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target teams distribute parallel for simd loop exprs were not "
"built") ? static_cast<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target teams distribute parallel for simd loop exprs were not \" \"built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6608, __PRETTY_FUNCTION__))
;
6609
6610 if (!CurContext->isDependentContext()) {
6611 // Finalize the clauses that need pre-built expressions for CodeGen.
6612 for (auto C : Clauses) {
6613 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6614 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6615 B.NumIterations, *this, CurScope,
6616 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
))
6617 return StmtError();
6618 }
6619 }
6620
6621 getCurFunction()->setHasBranchProtectedScope();
6622 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
6623 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6624}
6625
6626StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
6627 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6628 SourceLocation EndLoc,
6629 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6630 if (!AStmt)
6631 return StmtError();
6632
6633 auto *CS = cast<CapturedStmt>(AStmt);
6634 // 1.2.2 OpenMP Language Terminology
6635 // Structured block - An executable statement with a single entry at the
6636 // top and a single exit at the bottom.
6637 // The point of exit cannot be a branch out of the structured block.
6638 // longjmp() and throw() must not violate the entry/exit criteria.
6639 CS->getCapturedDecl()->setNothrow();
6640
6641 OMPLoopDirective::HelperExprs B;
6642 // In presence of clause 'collapse' with number of loops, it will
6643 // define the nested loops number.
6644 auto NestedLoopCount = CheckOpenMPLoop(
6645 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
6646 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
,
6647 VarsWithImplicitDSA, B);
6648 if (NestedLoopCount == 0)
6649 return StmtError();
6650
6651 assert((CurContext->isDependentContext() || B.builtAll()) &&(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target teams distribute simd loop exprs were not built"
) ? static_cast<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target teams distribute simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6652, __PRETTY_FUNCTION__))
6652 "omp target teams distribute simd loop exprs were not built")(((CurContext->isDependentContext() || B.builtAll()) &&
"omp target teams distribute simd loop exprs were not built"
) ? static_cast<void> (0) : __assert_fail ("(CurContext->isDependentContext() || B.builtAll()) && \"omp target teams distribute simd loop exprs were not built\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6652, __PRETTY_FUNCTION__))
;
6653
6654 getCurFunction()->setHasBranchProtectedScope();
6655 return OMPTargetTeamsDistributeSimdDirective::Create(
6656 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6657}
6658
6659OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
6660 SourceLocation StartLoc,
6661 SourceLocation LParenLoc,
6662 SourceLocation EndLoc) {
6663 OMPClause *Res = nullptr;
6664 switch (Kind) {
6665 case OMPC_final:
6666 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6667 break;
6668 case OMPC_num_threads:
6669 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6670 break;
6671 case OMPC_safelen:
6672 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6673 break;
6674 case OMPC_simdlen:
6675 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6676 break;
6677 case OMPC_collapse:
6678 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6679 break;
6680 case OMPC_ordered:
6681 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6682 break;
6683 case OMPC_device:
6684 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6685 break;
6686 case OMPC_num_teams:
6687 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6688 break;
6689 case OMPC_thread_limit:
6690 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6691 break;
6692 case OMPC_priority:
6693 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6694 break;
6695 case OMPC_grainsize:
6696 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6697 break;
6698 case OMPC_num_tasks:
6699 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6700 break;
6701 case OMPC_hint:
6702 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6703 break;
6704 case OMPC_if:
6705 case OMPC_default:
6706 case OMPC_proc_bind:
6707 case OMPC_schedule:
6708 case OMPC_private:
6709 case OMPC_firstprivate:
6710 case OMPC_lastprivate:
6711 case OMPC_shared:
6712 case OMPC_reduction:
6713 case OMPC_linear:
6714 case OMPC_aligned:
6715 case OMPC_copyin:
6716 case OMPC_copyprivate:
6717 case OMPC_nowait:
6718 case OMPC_untied:
6719 case OMPC_mergeable:
6720 case OMPC_threadprivate:
6721 case OMPC_flush:
6722 case OMPC_read:
6723 case OMPC_write:
6724 case OMPC_update:
6725 case OMPC_capture:
6726 case OMPC_seq_cst:
6727 case OMPC_depend:
6728 case OMPC_threads:
6729 case OMPC_simd:
6730 case OMPC_map:
6731 case OMPC_nogroup:
6732 case OMPC_dist_schedule:
6733 case OMPC_defaultmap:
6734 case OMPC_unknown:
6735 case OMPC_uniform:
6736 case OMPC_to:
6737 case OMPC_from:
6738 case OMPC_use_device_ptr:
6739 case OMPC_is_device_ptr:
6740 llvm_unreachable("Clause is not allowed.")::llvm::llvm_unreachable_internal("Clause is not allowed.", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6740)
;
6741 }
6742 return Res;
6743}
6744
6745// An OpenMP directive such as 'target parallel' has two captured regions:
6746// for the 'target' and 'parallel' respectively. This function returns
6747// the region in which to capture expressions associated with a clause.
6748// A return value of OMPD_unknown signifies that the expression should not
6749// be captured.
6750static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
6751 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
6752 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
6753 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
6754
6755 switch (CKind) {
6756 case OMPC_if:
6757 switch (DKind) {
6758 case OMPD_target_parallel:
6759 // If this clause applies to the nested 'parallel' region, capture within
6760 // the 'target' region, otherwise do not capture.
6761 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
6762 CaptureRegion = OMPD_target;
6763 break;
6764 case OMPD_cancel:
6765 case OMPD_parallel:
6766 case OMPD_parallel_sections:
6767 case OMPD_parallel_for:
6768 case OMPD_parallel_for_simd:
6769 case OMPD_target:
6770 case OMPD_target_simd:
6771 case OMPD_target_parallel_for:
6772 case OMPD_target_parallel_for_simd:
6773 case OMPD_target_teams:
6774 case OMPD_target_teams_distribute:
6775 case OMPD_target_teams_distribute_simd:
6776 case OMPD_target_teams_distribute_parallel_for:
6777 case OMPD_target_teams_distribute_parallel_for_simd:
6778 case OMPD_teams_distribute_parallel_for:
6779 case OMPD_teams_distribute_parallel_for_simd:
6780 case OMPD_distribute_parallel_for:
6781 case OMPD_distribute_parallel_for_simd:
6782 case OMPD_task:
6783 case OMPD_taskloop:
6784 case OMPD_taskloop_simd:
6785 case OMPD_target_data:
6786 case OMPD_target_enter_data:
6787 case OMPD_target_exit_data:
6788 case OMPD_target_update:
6789 // Do not capture if-clause expressions.
6790 break;
6791 case OMPD_threadprivate:
6792 case OMPD_taskyield:
6793 case OMPD_barrier:
6794 case OMPD_taskwait:
6795 case OMPD_cancellation_point:
6796 case OMPD_flush:
6797 case OMPD_declare_reduction:
6798 case OMPD_declare_simd:
6799 case OMPD_declare_target:
6800 case OMPD_end_declare_target:
6801 case OMPD_teams:
6802 case OMPD_simd:
6803 case OMPD_for:
6804 case OMPD_for_simd:
6805 case OMPD_sections:
6806 case OMPD_section:
6807 case OMPD_single:
6808 case OMPD_master:
6809 case OMPD_critical:
6810 case OMPD_taskgroup:
6811 case OMPD_distribute:
6812 case OMPD_ordered:
6813 case OMPD_atomic:
6814 case OMPD_distribute_simd:
6815 case OMPD_teams_distribute:
6816 case OMPD_teams_distribute_simd:
6817 llvm_unreachable("Unexpected OpenMP directive with if-clause")::llvm::llvm_unreachable_internal("Unexpected OpenMP directive with if-clause"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6817)
;
6818 case OMPD_unknown:
6819 llvm_unreachable("Unknown OpenMP directive")::llvm::llvm_unreachable_internal("Unknown OpenMP directive",
"/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6819)
;
6820 }
6821 break;
6822 case OMPC_num_threads:
6823 switch (DKind) {
6824 case OMPD_target_parallel:
6825 CaptureRegion = OMPD_target;
6826 break;
6827 case OMPD_cancel:
6828 case OMPD_parallel:
6829 case OMPD_parallel_sections:
6830 case OMPD_parallel_for:
6831 case OMPD_parallel_for_simd:
6832 case OMPD_target:
6833 case OMPD_target_simd:
6834 case OMPD_target_parallel_for:
6835 case OMPD_target_parallel_for_simd:
6836 case OMPD_target_teams:
6837 case OMPD_target_teams_distribute:
6838 case OMPD_target_teams_distribute_simd:
6839 case OMPD_target_teams_distribute_parallel_for:
6840 case OMPD_target_teams_distribute_parallel_for_simd:
6841 case OMPD_teams_distribute_parallel_for:
6842 case OMPD_teams_distribute_parallel_for_simd:
6843 case OMPD_distribute_parallel_for:
6844 case OMPD_distribute_parallel_for_simd:
6845 case OMPD_task:
6846 case OMPD_taskloop:
6847 case OMPD_taskloop_simd:
6848 case OMPD_target_data:
6849 case OMPD_target_enter_data:
6850 case OMPD_target_exit_data:
6851 case OMPD_target_update:
6852 // Do not capture num_threads-clause expressions.
6853 break;
6854 case OMPD_threadprivate:
6855 case OMPD_taskyield:
6856 case OMPD_barrier:
6857 case OMPD_taskwait:
6858 case OMPD_cancellation_point:
6859 case OMPD_flush:
6860 case OMPD_declare_reduction:
6861 case OMPD_declare_simd:
6862 case OMPD_declare_target:
6863 case OMPD_end_declare_target:
6864 case OMPD_teams:
6865 case OMPD_simd:
6866 case OMPD_for:
6867 case OMPD_for_simd:
6868 case OMPD_sections:
6869 case OMPD_section:
6870 case OMPD_single:
6871 case OMPD_master:
6872 case OMPD_critical:
6873 case OMPD_taskgroup:
6874 case OMPD_distribute:
6875 case OMPD_ordered:
6876 case OMPD_atomic:
6877 case OMPD_distribute_simd:
6878 case OMPD_teams_distribute:
6879 case OMPD_teams_distribute_simd:
6880 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause")::llvm::llvm_unreachable_internal("Unexpected OpenMP directive with num_threads-clause"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6880)
;
6881 case OMPD_unknown:
6882 llvm_unreachable("Unknown OpenMP directive")::llvm::llvm_unreachable_internal("Unknown OpenMP directive",
"/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6882)
;
6883 }
6884 break;
6885 case OMPC_num_teams:
6886 switch (DKind) {
6887 case OMPD_target_teams:
6888 CaptureRegion = OMPD_target;
6889 break;
6890 case OMPD_cancel:
6891 case OMPD_parallel:
6892 case OMPD_parallel_sections:
6893 case OMPD_parallel_for:
6894 case OMPD_parallel_for_simd:
6895 case OMPD_target:
6896 case OMPD_target_simd:
6897 case OMPD_target_parallel:
6898 case OMPD_target_parallel_for:
6899 case OMPD_target_parallel_for_simd:
6900 case OMPD_target_teams_distribute:
6901 case OMPD_target_teams_distribute_simd:
6902 case OMPD_target_teams_distribute_parallel_for:
6903 case OMPD_target_teams_distribute_parallel_for_simd:
6904 case OMPD_teams_distribute_parallel_for:
6905 case OMPD_teams_distribute_parallel_for_simd:
6906 case OMPD_distribute_parallel_for:
6907 case OMPD_distribute_parallel_for_simd:
6908 case OMPD_task:
6909 case OMPD_taskloop:
6910 case OMPD_taskloop_simd:
6911 case OMPD_target_data:
6912 case OMPD_target_enter_data:
6913 case OMPD_target_exit_data:
6914 case OMPD_target_update:
6915 case OMPD_teams:
6916 case OMPD_teams_distribute:
6917 case OMPD_teams_distribute_simd:
6918 // Do not capture num_teams-clause expressions.
6919 break;
6920 case OMPD_threadprivate:
6921 case OMPD_taskyield:
6922 case OMPD_barrier:
6923 case OMPD_taskwait:
6924 case OMPD_cancellation_point:
6925 case OMPD_flush:
6926 case OMPD_declare_reduction:
6927 case OMPD_declare_simd:
6928 case OMPD_declare_target:
6929 case OMPD_end_declare_target:
6930 case OMPD_simd:
6931 case OMPD_for:
6932 case OMPD_for_simd:
6933 case OMPD_sections:
6934 case OMPD_section:
6935 case OMPD_single:
6936 case OMPD_master:
6937 case OMPD_critical:
6938 case OMPD_taskgroup:
6939 case OMPD_distribute:
6940 case OMPD_ordered:
6941 case OMPD_atomic:
6942 case OMPD_distribute_simd:
6943 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause")::llvm::llvm_unreachable_internal("Unexpected OpenMP directive with num_teams-clause"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6943)
;
6944 case OMPD_unknown:
6945 llvm_unreachable("Unknown OpenMP directive")::llvm::llvm_unreachable_internal("Unknown OpenMP directive",
"/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 6945)
;
6946 }
6947 break;
6948 case OMPC_thread_limit:
6949 switch (DKind) {
6950 case OMPD_target_teams:
6951 CaptureRegion = OMPD_target;
6952 break;
6953 case OMPD_cancel:
6954 case OMPD_parallel:
6955 case OMPD_parallel_sections:
6956 case OMPD_parallel_for:
6957 case OMPD_parallel_for_simd:
6958 case OMPD_target:
6959 case OMPD_target_simd:
6960 case OMPD_target_parallel:
6961 case OMPD_target_parallel_for:
6962 case OMPD_target_parallel_for_simd:
6963 case OMPD_target_teams_distribute:
6964 case OMPD_target_teams_distribute_simd:
6965 case OMPD_target_teams_distribute_parallel_for:
6966 case OMPD_target_teams_distribute_parallel_for_simd:
6967 case OMPD_teams_distribute_parallel_for:
6968 case OMPD_teams_distribute_parallel_for_simd:
6969 case OMPD_distribute_parallel_for:
6970 case OMPD_distribute_parallel_for_simd:
6971 case OMPD_task:
6972 case OMPD_taskloop:
6973 case OMPD_taskloop_simd:
6974 case OMPD_target_data:
6975 case OMPD_target_enter_data:
6976 case OMPD_target_exit_data:
6977 case OMPD_target_update:
6978 case OMPD_teams:
6979 case OMPD_teams_distribute:
6980 case OMPD_teams_distribute_simd:
6981 // Do not capture thread_limit-clause expressions.
6982 break;
6983 case OMPD_threadprivate:
6984 case OMPD_taskyield:
6985 case OMPD_barrier:
6986 case OMPD_taskwait:
6987 case OMPD_cancellation_point:
6988 case OMPD_flush:
6989 case OMPD_declare_reduction:
6990 case OMPD_declare_simd:
6991 case OMPD_declare_target:
6992 case OMPD_end_declare_target:
6993 case OMPD_simd:
6994 case OMPD_for:
6995 case OMPD_for_simd:
6996 case OMPD_sections:
6997 case OMPD_section:
6998 case OMPD_single:
6999 case OMPD_master:
7000 case OMPD_critical:
7001 case OMPD_taskgroup:
7002 case OMPD_distribute:
7003 case OMPD_ordered:
7004 case OMPD_atomic:
7005 case OMPD_distribute_simd:
7006 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause")::llvm::llvm_unreachable_internal("Unexpected OpenMP directive with thread_limit-clause"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7006)
;
7007 case OMPD_unknown:
7008 llvm_unreachable("Unknown OpenMP directive")::llvm::llvm_unreachable_internal("Unknown OpenMP directive",
"/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7008)
;
7009 }
7010 break;
7011 case OMPC_schedule:
7012 case OMPC_dist_schedule:
7013 case OMPC_firstprivate:
7014 case OMPC_lastprivate:
7015 case OMPC_reduction:
7016 case OMPC_linear:
7017 case OMPC_default:
7018 case OMPC_proc_bind:
7019 case OMPC_final:
7020 case OMPC_safelen:
7021 case OMPC_simdlen:
7022 case OMPC_collapse:
7023 case OMPC_private:
7024 case OMPC_shared:
7025 case OMPC_aligned:
7026 case OMPC_copyin:
7027 case OMPC_copyprivate:
7028 case OMPC_ordered:
7029 case OMPC_nowait:
7030 case OMPC_untied:
7031 case OMPC_mergeable:
7032 case OMPC_threadprivate:
7033 case OMPC_flush:
7034 case OMPC_read:
7035 case OMPC_write:
7036 case OMPC_update:
7037 case OMPC_capture:
7038 case OMPC_seq_cst:
7039 case OMPC_depend:
7040 case OMPC_device:
7041 case OMPC_threads:
7042 case OMPC_simd:
7043 case OMPC_map:
7044 case OMPC_priority:
7045 case OMPC_grainsize:
7046 case OMPC_nogroup:
7047 case OMPC_num_tasks:
7048 case OMPC_hint:
7049 case OMPC_defaultmap:
7050 case OMPC_unknown:
7051 case OMPC_uniform:
7052 case OMPC_to:
7053 case OMPC_from:
7054 case OMPC_use_device_ptr:
7055 case OMPC_is_device_ptr:
7056 llvm_unreachable("Unexpected OpenMP clause.")::llvm::llvm_unreachable_internal("Unexpected OpenMP clause."
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7056)
;
7057 }
7058 return CaptureRegion;
7059}
7060
7061OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7062 Expr *Condition, SourceLocation StartLoc,
7063 SourceLocation LParenLoc,
7064 SourceLocation NameModifierLoc,
7065 SourceLocation ColonLoc,
7066 SourceLocation EndLoc) {
7067 Expr *ValExpr = Condition;
7068 Stmt *HelperValStmt = nullptr;
7069 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
7070 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7071 !Condition->isInstantiationDependent() &&
7072 !Condition->containsUnexpandedParameterPack()) {
7073 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
7074 if (Val.isInvalid())
7075 return nullptr;
7076
7077 ValExpr = MakeFullExpr(Val.get()).get();
7078
7079 OpenMPDirectiveKind DKind = DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective();
7080 CaptureRegion =
7081 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
7082 if (CaptureRegion != OMPD_unknown) {
7083 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7084 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7085 HelperValStmt = buildPreInits(Context, Captures);
7086 }
7087 }
7088
7089 return new (Context)
7090 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
7091 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
7092}
7093
7094OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7095 SourceLocation StartLoc,
7096 SourceLocation LParenLoc,
7097 SourceLocation EndLoc) {
7098 Expr *ValExpr = Condition;
7099 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7100 !Condition->isInstantiationDependent() &&
7101 !Condition->containsUnexpandedParameterPack()) {
7102 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
7103 if (Val.isInvalid())
7104 return nullptr;
7105
7106 ValExpr = MakeFullExpr(Val.get()).get();
7107 }
7108
7109 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7110}
7111ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7112 Expr *Op) {
7113 if (!Op)
7114 return ExprError();
7115
7116 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7117 public:
7118 IntConvertDiagnoser()
7119 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
7120 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7121 QualType T) override {
7122 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7123 }
7124 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7125 QualType T) override {
7126 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7127 }
7128 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7129 QualType T,
7130 QualType ConvTy) override {
7131 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7132 }
7133 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7134 QualType ConvTy) override {
7135 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
7136 << ConvTy->isEnumeralType() << ConvTy;
7137 }
7138 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7139 QualType T) override {
7140 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7141 }
7142 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7143 QualType ConvTy) override {
7144 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
7145 << ConvTy->isEnumeralType() << ConvTy;
7146 }
7147 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7148 QualType) override {
7149 llvm_unreachable("conversion functions are permitted")::llvm::llvm_unreachable_internal("conversion functions are permitted"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7149)
;
7150 }
7151 } ConvertDiagnoser;
7152 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7153}
7154
7155static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
7156 OpenMPClauseKind CKind,
7157 bool StrictlyPositive) {
7158 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7159 !ValExpr->isInstantiationDependent()) {
7160 SourceLocation Loc = ValExpr->getExprLoc();
7161 ExprResult Value =
7162 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7163 if (Value.isInvalid())
7164 return false;
7165
7166 ValExpr = Value.get();
7167 // The expression must evaluate to a non-negative integer value.
7168 llvm::APSInt Result;
7169 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
7170 Result.isSigned() &&
7171 !((!StrictlyPositive && Result.isNonNegative()) ||
7172 (StrictlyPositive && Result.isStrictlyPositive()))) {
7173 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
7174 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7175 << ValExpr->getSourceRange();
7176 return false;
7177 }
7178 }
7179 return true;
7180}
7181
7182OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7183 SourceLocation StartLoc,
7184 SourceLocation LParenLoc,
7185 SourceLocation EndLoc) {
7186 Expr *ValExpr = NumThreads;
7187 Stmt *HelperValStmt = nullptr;
7188 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
7189
7190 // OpenMP [2.5, Restrictions]
7191 // The num_threads expression must evaluate to a positive integer value.
7192 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7193 /*StrictlyPositive=*/true))
7194 return nullptr;
7195
7196 OpenMPDirectiveKind DKind = DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getCurrentDirective();
7197 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
7198 if (CaptureRegion != OMPD_unknown) {
7199 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7200 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7201 HelperValStmt = buildPreInits(Context, Captures);
7202 }
7203
7204 return new (Context) OMPNumThreadsClause(
7205 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
7206}
7207
7208ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
7209 OpenMPClauseKind CKind,
7210 bool StrictlyPositive) {
7211 if (!E)
7212 return ExprError();
7213 if (E->isValueDependent() || E->isTypeDependent() ||
7214 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
7215 return E;
7216 llvm::APSInt Result;
7217 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7218 if (ICE.isInvalid())
7219 return ExprError();
7220 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7221 (!StrictlyPositive && !Result.isNonNegative())) {
7222 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
7223 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7224 << E->getSourceRange();
7225 return ExprError();
7226 }
7227 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7228 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7229 << E->getSourceRange();
7230 return ExprError();
7231 }
7232 if (CKind == OMPC_collapse && DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->getAssociatedLoops() == 1)
7233 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setAssociatedLoops(Result.getExtValue());
7234 else if (CKind == OMPC_ordered)
7235 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setAssociatedLoops(Result.getExtValue());
7236 return ICE;
7237}
7238
7239OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7240 SourceLocation LParenLoc,
7241 SourceLocation EndLoc) {
7242 // OpenMP [2.8.1, simd construct, Description]
7243 // The parameter of the safelen clause must be a constant
7244 // positive integer expression.
7245 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7246 if (Safelen.isInvalid())
7247 return nullptr;
7248 return new (Context)
7249 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
7250}
7251
7252OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7253 SourceLocation LParenLoc,
7254 SourceLocation EndLoc) {
7255 // OpenMP [2.8.1, simd construct, Description]
7256 // The parameter of the simdlen clause must be a constant
7257 // positive integer expression.
7258 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7259 if (Simdlen.isInvalid())
7260 return nullptr;
7261 return new (Context)
7262 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7263}
7264
7265OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7266 SourceLocation StartLoc,
7267 SourceLocation LParenLoc,
7268 SourceLocation EndLoc) {
7269 // OpenMP [2.7.1, loop construct, Description]
7270 // OpenMP [2.8.1, simd construct, Description]
7271 // OpenMP [2.9.6, distribute construct, Description]
7272 // The parameter of the collapse clause must be a constant
7273 // positive integer expression.
7274 ExprResult NumForLoopsResult =
7275 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7276 if (NumForLoopsResult.isInvalid())
7277 return nullptr;
7278 return new (Context)
7279 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
7280}
7281
7282OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7283 SourceLocation EndLoc,
7284 SourceLocation LParenLoc,
7285 Expr *NumForLoops) {
7286 // OpenMP [2.7.1, loop construct, Description]
7287 // OpenMP [2.8.1, simd construct, Description]
7288 // OpenMP [2.9.6, distribute construct, Description]
7289 // The parameter of the ordered clause must be a constant
7290 // positive integer expression if any.
7291 if (NumForLoops && LParenLoc.isValid()) {
7292 ExprResult NumForLoopsResult =
7293 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7294 if (NumForLoopsResult.isInvalid())
7295 return nullptr;
7296 NumForLoops = NumForLoopsResult.get();
7297 } else
7298 NumForLoops = nullptr;
7299 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
7300 return new (Context)
7301 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7302}
7303
7304OMPClause *Sema::ActOnOpenMPSimpleClause(
7305 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7306 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
7307 OMPClause *Res = nullptr;
7308 switch (Kind) {
7309 case OMPC_default:
7310 Res =
7311 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7312 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
7313 break;
7314 case OMPC_proc_bind:
7315 Res = ActOnOpenMPProcBindClause(
7316 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7317 LParenLoc, EndLoc);
7318 break;
7319 case OMPC_if:
7320 case OMPC_final:
7321 case OMPC_num_threads:
7322 case OMPC_safelen:
7323 case OMPC_simdlen:
7324 case OMPC_collapse:
7325 case OMPC_schedule:
7326 case OMPC_private:
7327 case OMPC_firstprivate:
7328 case OMPC_lastprivate:
7329 case OMPC_shared:
7330 case OMPC_reduction:
7331 case OMPC_linear:
7332 case OMPC_aligned:
7333 case OMPC_copyin:
7334 case OMPC_copyprivate:
7335 case OMPC_ordered:
7336 case OMPC_nowait:
7337 case OMPC_untied:
7338 case OMPC_mergeable:
7339 case OMPC_threadprivate:
7340 case OMPC_flush:
7341 case OMPC_read:
7342 case OMPC_write:
7343 case OMPC_update:
7344 case OMPC_capture:
7345 case OMPC_seq_cst:
7346 case OMPC_depend:
7347 case OMPC_device:
7348 case OMPC_threads:
7349 case OMPC_simd:
7350 case OMPC_map:
7351 case OMPC_num_teams:
7352 case OMPC_thread_limit:
7353 case OMPC_priority:
7354 case OMPC_grainsize:
7355 case OMPC_nogroup:
7356 case OMPC_num_tasks:
7357 case OMPC_hint:
7358 case OMPC_dist_schedule:
7359 case OMPC_defaultmap:
7360 case OMPC_unknown:
7361 case OMPC_uniform:
7362 case OMPC_to:
7363 case OMPC_from:
7364 case OMPC_use_device_ptr:
7365 case OMPC_is_device_ptr:
7366 llvm_unreachable("Clause is not allowed.")::llvm::llvm_unreachable_internal("Clause is not allowed.", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7366)
;
7367 }
7368 return Res;
7369}
7370
7371static std::string
7372getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7373 ArrayRef<unsigned> Exclude = llvm::None) {
7374 std::string Values;
7375 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7376 unsigned Skipped = Exclude.size();
7377 auto S = Exclude.begin(), E = Exclude.end();
7378 for (unsigned i = First; i < Last; ++i) {
7379 if (std::find(S, E, i) != E) {
7380 --Skipped;
7381 continue;
7382 }
7383 Values += "'";
7384 Values += getOpenMPSimpleClauseTypeName(K, i);
7385 Values += "'";
7386 if (i == Bound - Skipped)
7387 Values += " or ";
7388 else if (i != Bound + 1 - Skipped)
7389 Values += ", ";
7390 }
7391 return Values;
7392}
7393
7394OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7395 SourceLocation KindKwLoc,
7396 SourceLocation StartLoc,
7397 SourceLocation LParenLoc,
7398 SourceLocation EndLoc) {
7399 if (Kind == OMPC_DEFAULT_unknown) {
7400 static_assert(OMPC_DEFAULT_unknown > 0,
7401 "OMPC_DEFAULT_unknown not greater than 0");
7402 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7403 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7404 /*Last=*/OMPC_DEFAULT_unknown)
7405 << getOpenMPClauseName(OMPC_default);
7406 return nullptr;
7407 }
7408 switch (Kind) {
7409 case OMPC_DEFAULT_none:
7410 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setDefaultDSANone(KindKwLoc);
7411 break;
7412 case OMPC_DEFAULT_shared:
7413 DSAStackstatic_cast<DSAStackTy *>(VarDataSharingAttributesStack
)
->setDefaultDSAShared(KindKwLoc);
7414 break;
7415 case OMPC_DEFAULT_unknown:
7416 llvm_unreachable("Clause kind is not allowed.")::llvm::llvm_unreachable_internal("Clause kind is not allowed."
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7416)
;
7417 break;
7418 }
7419 return new (Context)
7420 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7421}
7422
7423OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7424 SourceLocation KindKwLoc,
7425 SourceLocation StartLoc,
7426 SourceLocation LParenLoc,
7427 SourceLocation EndLoc) {
7428 if (Kind == OMPC_PROC_BIND_unknown) {
7429 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7430 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7431 /*Last=*/OMPC_PROC_BIND_unknown)
7432 << getOpenMPClauseName(OMPC_proc_bind);
7433 return nullptr;
7434 }
7435 return new (Context)
7436 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7437}
7438
7439OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
7440 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
7441 SourceLocation StartLoc, SourceLocation LParenLoc,
7442 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
7443 SourceLocation EndLoc) {
7444 OMPClause *Res = nullptr;
7445 switch (Kind) {
7446 case OMPC_schedule:
7447 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7448 assert(Argument.size() == NumberOfElements &&((Argument.size() == NumberOfElements && ArgumentLoc.
size() == NumberOfElements) ? static_cast<void> (0) : __assert_fail
("Argument.size() == NumberOfElements && ArgumentLoc.size() == NumberOfElements"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7449, __PRETTY_FUNCTION__))
7449 ArgumentLoc.size() == NumberOfElements)((Argument.size() == NumberOfElements && ArgumentLoc.
size() == NumberOfElements) ? static_cast<void> (0) : __assert_fail
("Argument.size() == NumberOfElements && ArgumentLoc.size() == NumberOfElements"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7449, __PRETTY_FUNCTION__))
;
7450 Res = ActOnOpenMPScheduleClause(
7451 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7452 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7453 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7454 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7455 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
7456 break;
7457 case OMPC_if:
7458 assert(Argument.size() == 1 && ArgumentLoc.size() == 1)((Argument.size() == 1 && ArgumentLoc.size() == 1) ? static_cast
<void> (0) : __assert_fail ("Argument.size() == 1 && ArgumentLoc.size() == 1"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7458, __PRETTY_FUNCTION__))
;
7459 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7460 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7461 DelimLoc, EndLoc);
7462 break;
7463 case OMPC_dist_schedule:
7464 Res = ActOnOpenMPDistScheduleClause(
7465 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7466 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7467 break;
7468 case OMPC_defaultmap:
7469 enum { Modifier, DefaultmapKind };
7470 Res = ActOnOpenMPDefaultmapClause(
7471 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7472 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7473 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
7474 EndLoc);
7475 break;
7476 case OMPC_final:
7477 case OMPC_num_threads:
7478 case OMPC_safelen:
7479 case OMPC_simdlen:
7480 case OMPC_collapse:
7481 case OMPC_default:
7482 case OMPC_proc_bind:
7483 case OMPC_private:
7484 case OMPC_firstprivate:
7485 case OMPC_lastprivate:
7486 case OMPC_shared:
7487 case OMPC_reduction:
7488 case OMPC_linear:
7489 case OMPC_aligned:
7490 case OMPC_copyin:
7491 case OMPC_copyprivate:
7492 case OMPC_ordered:
7493 case OMPC_nowait:
7494 case OMPC_untied:
7495 case OMPC_mergeable:
7496 case OMPC_threadprivate:
7497 case OMPC_flush:
7498 case OMPC_read:
7499 case OMPC_write:
7500 case OMPC_update:
7501 case OMPC_capture:
7502 case OMPC_seq_cst:
7503 case OMPC_depend:
7504 case OMPC_device:
7505 case OMPC_threads:
7506 case OMPC_simd:
7507 case OMPC_map:
7508 case OMPC_num_teams:
7509 case OMPC_thread_limit:
7510 case OMPC_priority:
7511 case OMPC_grainsize:
7512 case OMPC_nogroup:
7513 case OMPC_num_tasks:
7514 case OMPC_hint:
7515 case OMPC_unknown:
7516 case OMPC_uniform:
7517 case OMPC_to:
7518 case OMPC_from:
7519 case OMPC_use_device_ptr:
7520 case OMPC_is_device_ptr:
7521 llvm_unreachable("Clause is not allowed.")::llvm::llvm_unreachable_internal("Clause is not allowed.", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301165/tools/clang/lib/Sema/SemaOpenMP.cpp"
, 7521)
;
7522 }
7523 return Res;
7524}
7525
7526static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,