Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 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 |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 11 | /// clauses. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclOpenMP.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtCXX.h" |
| 21 | #include "clang/AST/StmtOpenMP.h" |
| 22 | #include "clang/AST/StmtVisitor.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 23 | #include "clang/Basic/OpenMPKinds.h" |
| 24 | #include "clang/Lex/Preprocessor.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 25 | #include "clang/Sema/Initialization.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Lookup.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 27 | #include "clang/Sema/Scope.h" |
| 28 | #include "clang/Sema/ScopeInfo.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 29 | #include "clang/Sema/SemaInternal.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // Stack of data-sharing attributes for variables |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | namespace { |
| 37 | /// \brief Default data sharing attributes, which can be applied to directive. |
| 38 | enum DefaultDataSharingAttributes { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 39 | DSA_unspecified = 0, /// \brief Data sharing attribute not specified. |
| 40 | DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'. |
| 41 | DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'. |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 42 | }; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 43 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 44 | template <class T> struct MatchesAny { |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 45 | explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {} |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 46 | bool operator()(T Kind) { |
| 47 | for (auto KindEl : Arr) |
| 48 | if (KindEl == Kind) |
| 49 | return true; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | ArrayRef<T> Arr; |
| 55 | }; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 56 | struct MatchesAlways { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 57 | MatchesAlways() {} |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 58 | template <class T> bool operator()(T) { return true; } |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause; |
| 62 | typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 63 | |
| 64 | /// \brief Stack for tracking declarations used in OpenMP directives and |
| 65 | /// clauses and their data-sharing attributes. |
| 66 | class DSAStackTy { |
| 67 | public: |
| 68 | struct DSAVarData { |
| 69 | OpenMPDirectiveKind DKind; |
| 70 | OpenMPClauseKind CKind; |
| 71 | DeclRefExpr *RefExpr; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 72 | SourceLocation ImplicitDSALoc; |
| 73 | DSAVarData() |
| 74 | : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr), |
| 75 | ImplicitDSALoc() {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 76 | }; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 77 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 78 | private: |
| 79 | struct DSAInfo { |
| 80 | OpenMPClauseKind Attributes; |
| 81 | DeclRefExpr *RefExpr; |
| 82 | }; |
| 83 | typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 84 | typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy; |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 85 | typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 86 | |
| 87 | struct SharingMapTy { |
| 88 | DeclSAMapTy SharingMap; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 89 | AlignedMapTy AlignedMap; |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 90 | LoopControlVariablesSetTy LCVSet; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 91 | DefaultDataSharingAttributes DefaultAttr; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 92 | SourceLocation DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 93 | OpenMPDirectiveKind Directive; |
| 94 | DeclarationNameInfo DirectiveName; |
| 95 | Scope *CurScope; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 96 | SourceLocation ConstructLoc; |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 97 | bool OrderedRegion; |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 98 | unsigned CollapseNumber; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 99 | SourceLocation InnerTeamsRegionLoc; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 100 | SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 101 | Scope *CurScope, SourceLocation Loc) |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 102 | : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified), |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 103 | Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope), |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 104 | ConstructLoc(Loc), OrderedRegion(false), CollapseNumber(1), |
| 105 | InnerTeamsRegionLoc() {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 106 | SharingMapTy() |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 107 | : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified), |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 108 | Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr), |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 109 | ConstructLoc(), OrderedRegion(false), CollapseNumber(1), |
| 110 | InnerTeamsRegionLoc() {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | typedef SmallVector<SharingMapTy, 64> StackTy; |
| 114 | |
| 115 | /// \brief Stack of used declaration and their data-sharing attributes. |
| 116 | StackTy Stack; |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 117 | /// \brief true, if check for DSA must be from parent directive, false, if |
| 118 | /// from current directive. |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 119 | OpenMPClauseKind ClauseKindMode; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 120 | Sema &SemaRef; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 121 | |
| 122 | typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; |
| 123 | |
| 124 | DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 125 | |
| 126 | /// \brief Checks if the variable is a local for OpenMP region. |
| 127 | bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 128 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 129 | public: |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 130 | explicit DSAStackTy(Sema &S) |
| 131 | : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S) {} |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 132 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 133 | bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } |
| 134 | void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 135 | |
| 136 | void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 137 | Scope *CurScope, SourceLocation Loc) { |
| 138 | Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc)); |
| 139 | Stack.back().DefaultAttrLoc = Loc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void pop() { |
| 143 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!"); |
| 144 | Stack.pop_back(); |
| 145 | } |
| 146 | |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 147 | /// \brief If 'aligned' declaration for given variable \a D was not seen yet, |
Alp Toker | 15e62a3 | 2014-06-06 12:02:07 +0000 | [diff] [blame] | 148 | /// add it and return NULL; otherwise return previous occurrence's expression |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 149 | /// for diagnostics. |
| 150 | DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE); |
| 151 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 152 | /// \brief Register specified variable as loop control variable. |
| 153 | void addLoopControlVariable(VarDecl *D); |
| 154 | /// \brief Check if the specified variable is a loop control variable for |
| 155 | /// current region. |
| 156 | bool isLoopControlVariable(VarDecl *D); |
| 157 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 158 | /// \brief Adds explicit data sharing attribute to the specified declaration. |
| 159 | void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A); |
| 160 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 161 | /// \brief Returns data sharing attributes from top of the stack for the |
| 162 | /// specified declaration. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 163 | DSAVarData getTopDSA(VarDecl *D, bool FromParent); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 164 | /// \brief Returns data-sharing attributes for the specified declaration. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 165 | DSAVarData getImplicitDSA(VarDecl *D, bool FromParent); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 166 | /// \brief Checks if the specified variables has data-sharing attributes which |
| 167 | /// match specified \a CPred predicate in any directive which matches \a DPred |
| 168 | /// predicate. |
| 169 | template <class ClausesPredicate, class DirectivesPredicate> |
| 170 | DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 171 | DirectivesPredicate DPred, bool FromParent); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 172 | /// \brief Checks if the specified variables has data-sharing attributes which |
| 173 | /// match specified \a CPred predicate in any innermost directive which |
| 174 | /// matches \a DPred predicate. |
| 175 | template <class ClausesPredicate, class DirectivesPredicate> |
| 176 | DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 177 | DirectivesPredicate DPred, |
| 178 | bool FromParent); |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 179 | /// \brief Checks if the specified variables has explicit data-sharing |
| 180 | /// attributes which match specified \a CPred predicate at the specified |
| 181 | /// OpenMP region. |
| 182 | bool hasExplicitDSA(VarDecl *D, |
| 183 | const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 184 | unsigned Level); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 185 | /// \brief Finds a directive which matches specified \a DPred predicate. |
| 186 | template <class NamedDirectivesPredicate> |
| 187 | bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 188 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 189 | /// \brief Returns currently analyzed directive. |
| 190 | OpenMPDirectiveKind getCurrentDirective() const { |
| 191 | return Stack.back().Directive; |
| 192 | } |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 193 | /// \brief Returns parent directive. |
| 194 | OpenMPDirectiveKind getParentDirective() const { |
| 195 | if (Stack.size() > 2) |
| 196 | return Stack[Stack.size() - 2].Directive; |
| 197 | return OMPD_unknown; |
| 198 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 199 | |
| 200 | /// \brief Set default data sharing attribute to none. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 201 | void setDefaultDSANone(SourceLocation Loc) { |
| 202 | Stack.back().DefaultAttr = DSA_none; |
| 203 | Stack.back().DefaultAttrLoc = Loc; |
| 204 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 205 | /// \brief Set default data sharing attribute to shared. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 206 | void setDefaultDSAShared(SourceLocation Loc) { |
| 207 | Stack.back().DefaultAttr = DSA_shared; |
| 208 | Stack.back().DefaultAttrLoc = Loc; |
| 209 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 210 | |
| 211 | DefaultDataSharingAttributes getDefaultDSA() const { |
| 212 | return Stack.back().DefaultAttr; |
| 213 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 214 | SourceLocation getDefaultDSALocation() const { |
| 215 | return Stack.back().DefaultAttrLoc; |
| 216 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 217 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 218 | /// \brief Checks if the specified variable is a threadprivate. |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 219 | bool isThreadPrivate(VarDecl *D) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 220 | DSAVarData DVar = getTopDSA(D, false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 221 | return isOpenMPThreadPrivate(DVar.CKind); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 224 | /// \brief Marks current region as ordered (it has an 'ordered' clause). |
| 225 | void setOrderedRegion(bool IsOrdered = true) { |
| 226 | Stack.back().OrderedRegion = IsOrdered; |
| 227 | } |
| 228 | /// \brief Returns true, if parent region is ordered (has associated |
| 229 | /// 'ordered' clause), false - otherwise. |
| 230 | bool isParentOrderedRegion() const { |
| 231 | if (Stack.size() > 2) |
| 232 | return Stack[Stack.size() - 2].OrderedRegion; |
| 233 | return false; |
| 234 | } |
| 235 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 236 | /// \brief Set collapse value for the region. |
| 237 | void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; } |
| 238 | /// \brief Return collapse value for region. |
| 239 | unsigned getCollapseNumber() const { |
| 240 | return Stack.back().CollapseNumber; |
| 241 | } |
| 242 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 243 | /// \brief Marks current target region as one with closely nested teams |
| 244 | /// region. |
| 245 | void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { |
| 246 | if (Stack.size() > 2) |
| 247 | Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc; |
| 248 | } |
| 249 | /// \brief Returns true, if current region has closely nested teams region. |
| 250 | bool hasInnerTeamsRegion() const { |
| 251 | return getInnerTeamsRegionLoc().isValid(); |
| 252 | } |
| 253 | /// \brief Returns location of the nested teams region (if any). |
| 254 | SourceLocation getInnerTeamsRegionLoc() const { |
| 255 | if (Stack.size() > 1) |
| 256 | return Stack.back().InnerTeamsRegionLoc; |
| 257 | return SourceLocation(); |
| 258 | } |
| 259 | |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 260 | Scope *getCurScope() const { return Stack.back().CurScope; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 261 | Scope *getCurScope() { return Stack.back().CurScope; } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 262 | SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 263 | }; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 264 | bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { |
| 265 | return isOpenMPParallelDirective(DKind) || DKind == OMPD_task || |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 266 | isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 267 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 268 | } // namespace |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 269 | |
| 270 | DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter, |
| 271 | VarDecl *D) { |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 272 | D = D->getCanonicalDecl(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 273 | DSAVarData DVar; |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 274 | if (Iter == std::prev(Stack.rend())) { |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 275 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 276 | // in a region but not in construct] |
| 277 | // File-scope or namespace-scope variables referenced in called routines |
| 278 | // in the region are shared unless they appear in a threadprivate |
| 279 | // directive. |
Alexey Bataev | 8b9cb98 | 2014-07-24 02:33:58 +0000 | [diff] [blame] | 280 | if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D)) |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 281 | DVar.CKind = OMPC_shared; |
| 282 | |
| 283 | // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced |
| 284 | // in a region but not in construct] |
| 285 | // Variables with static storage duration that are declared in called |
| 286 | // routines in the region are shared. |
| 287 | if (D->hasGlobalStorage()) |
| 288 | DVar.CKind = OMPC_shared; |
| 289 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 290 | return DVar; |
| 291 | } |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 292 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 293 | DVar.DKind = Iter->Directive; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 294 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 295 | // in a Construct, C/C++, predetermined, p.1] |
| 296 | // Variables with automatic storage duration that are declared in a scope |
| 297 | // inside the construct are private. |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 298 | if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() && |
| 299 | (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) { |
| 300 | DVar.CKind = OMPC_private; |
| 301 | return DVar; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 304 | // Explicitly specified attributes and local variables with predetermined |
| 305 | // attributes. |
| 306 | if (Iter->SharingMap.count(D)) { |
| 307 | DVar.RefExpr = Iter->SharingMap[D].RefExpr; |
| 308 | DVar.CKind = Iter->SharingMap[D].Attributes; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 309 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 310 | return DVar; |
| 311 | } |
| 312 | |
| 313 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 314 | // in a Construct, C/C++, implicitly determined, p.1] |
| 315 | // In a parallel or task construct, the data-sharing attributes of these |
| 316 | // variables are determined by the default clause, if present. |
| 317 | switch (Iter->DefaultAttr) { |
| 318 | case DSA_shared: |
| 319 | DVar.CKind = OMPC_shared; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 320 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 321 | return DVar; |
| 322 | case DSA_none: |
| 323 | return DVar; |
| 324 | case DSA_unspecified: |
| 325 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 326 | // in a Construct, implicitly determined, p.2] |
| 327 | // In a parallel construct, if no default clause is present, these |
| 328 | // variables are shared. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 329 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 330 | if (isOpenMPParallelDirective(DVar.DKind) || |
| 331 | isOpenMPTeamsDirective(DVar.DKind)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 332 | DVar.CKind = OMPC_shared; |
| 333 | return DVar; |
| 334 | } |
| 335 | |
| 336 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 337 | // in a Construct, implicitly determined, p.4] |
| 338 | // In a task construct, if no default clause is present, a variable that in |
| 339 | // the enclosing context is determined to be shared by all implicit tasks |
| 340 | // bound to the current team is shared. |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 341 | if (DVar.DKind == OMPD_task) { |
| 342 | DSAVarData DVarTemp; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 343 | for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 344 | I != EE; ++I) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 345 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables |
| 346 | // Referenced |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 347 | // in a Construct, implicitly determined, p.6] |
| 348 | // In a task construct, if no default clause is present, a variable |
| 349 | // whose data-sharing attribute is not determined by the rules above is |
| 350 | // firstprivate. |
| 351 | DVarTemp = getDSA(I, D); |
| 352 | if (DVarTemp.CKind != OMPC_shared) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 353 | DVar.RefExpr = nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 354 | DVar.DKind = OMPD_task; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 355 | DVar.CKind = OMPC_firstprivate; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 356 | return DVar; |
| 357 | } |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 358 | if (isParallelOrTaskRegion(I->Directive)) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 359 | break; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 360 | } |
| 361 | DVar.DKind = OMPD_task; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 362 | DVar.CKind = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 363 | (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 364 | return DVar; |
| 365 | } |
| 366 | } |
| 367 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 368 | // in a Construct, implicitly determined, p.3] |
| 369 | // For constructs other than task, if no default clause is present, these |
| 370 | // variables inherit their data-sharing attributes from the enclosing |
| 371 | // context. |
Benjamin Kramer | 167e999 | 2014-03-02 12:20:24 +0000 | [diff] [blame] | 372 | return getDSA(std::next(Iter), D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 375 | DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) { |
| 376 | assert(Stack.size() > 1 && "Data sharing attributes stack is empty"); |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 377 | D = D->getCanonicalDecl(); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 378 | auto It = Stack.back().AlignedMap.find(D); |
| 379 | if (It == Stack.back().AlignedMap.end()) { |
| 380 | assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); |
| 381 | Stack.back().AlignedMap[D] = NewDE; |
| 382 | return nullptr; |
| 383 | } else { |
| 384 | assert(It->second && "Unexpected nullptr expr in the aligned map"); |
| 385 | return It->second; |
| 386 | } |
| 387 | return nullptr; |
| 388 | } |
| 389 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 390 | void DSAStackTy::addLoopControlVariable(VarDecl *D) { |
| 391 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); |
| 392 | D = D->getCanonicalDecl(); |
| 393 | Stack.back().LCVSet.insert(D); |
| 394 | } |
| 395 | |
| 396 | bool DSAStackTy::isLoopControlVariable(VarDecl *D) { |
| 397 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); |
| 398 | D = D->getCanonicalDecl(); |
| 399 | return Stack.back().LCVSet.count(D) > 0; |
| 400 | } |
| 401 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 402 | void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) { |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 403 | D = D->getCanonicalDecl(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 404 | if (A == OMPC_threadprivate) { |
| 405 | Stack[0].SharingMap[D].Attributes = A; |
| 406 | Stack[0].SharingMap[D].RefExpr = E; |
| 407 | } else { |
| 408 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); |
| 409 | Stack.back().SharingMap[D].Attributes = A; |
| 410 | Stack.back().SharingMap[D].RefExpr = E; |
| 411 | } |
| 412 | } |
| 413 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 414 | bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 415 | D = D->getCanonicalDecl(); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 416 | if (Stack.size() > 2) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 417 | reverse_iterator I = Iter, E = std::prev(Stack.rend()); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 418 | Scope *TopScope = nullptr; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 419 | while (I != E && !isParallelOrTaskRegion(I->Directive)) { |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 420 | ++I; |
| 421 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 422 | if (I == E) |
| 423 | return false; |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 424 | TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 425 | Scope *CurScope = getCurScope(); |
| 426 | while (CurScope != TopScope && !CurScope->isDeclScope(D)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 427 | CurScope = CurScope->getParent(); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 428 | } |
| 429 | return CurScope != TopScope; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 430 | } |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 431 | return false; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 434 | /// \brief Build a variable declaration for OpenMP loop iteration variable. |
| 435 | static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, |
| 436 | StringRef Name) { |
| 437 | DeclContext *DC = SemaRef.CurContext; |
| 438 | IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); |
| 439 | TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); |
| 440 | VarDecl *Decl = |
| 441 | VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); |
| 442 | Decl->setImplicit(); |
| 443 | return Decl; |
| 444 | } |
| 445 | |
| 446 | static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, |
| 447 | SourceLocation Loc, |
| 448 | bool RefersToCapture = false) { |
| 449 | D->setReferenced(); |
| 450 | D->markUsed(S.Context); |
| 451 | return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), |
| 452 | SourceLocation(), D, RefersToCapture, Loc, Ty, |
| 453 | VK_LValue); |
| 454 | } |
| 455 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 456 | DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) { |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 457 | D = D->getCanonicalDecl(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 458 | DSAVarData DVar; |
| 459 | |
| 460 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 461 | // in a Construct, C/C++, predetermined, p.1] |
| 462 | // Variables appearing in threadprivate directives are threadprivate. |
Alexey Bataev | 26a3924 | 2015-01-13 03:35:30 +0000 | [diff] [blame] | 463 | if (D->getTLSKind() != VarDecl::TLS_None || |
Alexey Bataev | 1a8b3f1 | 2015-05-06 06:34:55 +0000 | [diff] [blame] | 464 | (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() && |
| 465 | !D->isLocalVarDecl())) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 466 | addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(), |
| 467 | D->getLocation()), |
Alexey Bataev | f2453a0 | 2015-05-06 07:25:08 +0000 | [diff] [blame] | 468 | OMPC_threadprivate); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 469 | } |
| 470 | if (Stack[0].SharingMap.count(D)) { |
| 471 | DVar.RefExpr = Stack[0].SharingMap[D].RefExpr; |
| 472 | DVar.CKind = OMPC_threadprivate; |
| 473 | return DVar; |
| 474 | } |
| 475 | |
| 476 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 477 | // in a Construct, C/C++, predetermined, p.1] |
| 478 | // Variables with automatic storage duration that are declared in a scope |
| 479 | // inside the construct are private. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 480 | OpenMPDirectiveKind Kind = |
| 481 | FromParent ? getParentDirective() : getCurrentDirective(); |
| 482 | auto StartI = std::next(Stack.rbegin()); |
| 483 | auto EndI = std::prev(Stack.rend()); |
| 484 | if (FromParent && StartI != EndI) { |
| 485 | StartI = std::next(StartI); |
| 486 | } |
| 487 | if (!isParallelOrTaskRegion(Kind)) { |
Alexey Bataev | 8b9cb98 | 2014-07-24 02:33:58 +0000 | [diff] [blame] | 488 | if (isOpenMPLocal(D, StartI) && |
| 489 | ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto || |
| 490 | D->getStorageClass() == SC_None)) || |
| 491 | isa<ParmVarDecl>(D))) { |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 492 | DVar.CKind = OMPC_private; |
| 493 | return DVar; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 494 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 495 | |
Alexey Bataev | 24b04aa | 2015-01-16 07:11:33 +0000 | [diff] [blame] | 496 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 497 | // in a Construct, C/C++, predetermined, p.4] |
| 498 | // Static data members are shared. |
Alexey Bataev | 24b04aa | 2015-01-16 07:11:33 +0000 | [diff] [blame] | 499 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 500 | // in a Construct, C/C++, predetermined, p.7] |
| 501 | // Variables with static storage duration that are declared in a scope |
| 502 | // inside the construct are shared. |
Alexey Bataev | 42971a3 | 2015-01-20 07:03:46 +0000 | [diff] [blame] | 503 | if (D->isStaticDataMember() || D->isStaticLocal()) { |
| 504 | DSAVarData DVarTemp = |
| 505 | hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent); |
| 506 | if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) |
| 507 | return DVar; |
| 508 | |
Alexey Bataev | 24b04aa | 2015-01-16 07:11:33 +0000 | [diff] [blame] | 509 | DVar.CKind = OMPC_shared; |
| 510 | return DVar; |
| 511 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | QualType Type = D->getType().getNonReferenceType().getCanonicalType(); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 515 | bool IsConstant = Type.isConstant(SemaRef.getASTContext()); |
| 516 | Type = SemaRef.getASTContext().getBaseElementType(Type); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 517 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 518 | // in a Construct, C/C++, predetermined, p.6] |
| 519 | // Variables with const qualified type having no mutable member are |
| 520 | // shared. |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 521 | CXXRecordDecl *RD = |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 522 | SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 523 | if (IsConstant && |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 524 | !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 525 | // Variables with const-qualified type having no mutable member may be |
| 526 | // listed in a firstprivate clause, even if they are static data members. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 527 | DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate), |
| 528 | MatchesAlways(), FromParent); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 529 | if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) |
| 530 | return DVar; |
| 531 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 532 | DVar.CKind = OMPC_shared; |
| 533 | return DVar; |
| 534 | } |
| 535 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 536 | // Explicitly specified attributes and local variables with predetermined |
| 537 | // attributes. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 538 | auto I = std::prev(StartI); |
| 539 | if (I->SharingMap.count(D)) { |
| 540 | DVar.RefExpr = I->SharingMap[D].RefExpr; |
| 541 | DVar.CKind = I->SharingMap[D].Attributes; |
| 542 | DVar.ImplicitDSALoc = I->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | return DVar; |
| 546 | } |
| 547 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 548 | DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) { |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 549 | D = D->getCanonicalDecl(); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 550 | auto StartI = Stack.rbegin(); |
| 551 | auto EndI = std::prev(Stack.rend()); |
| 552 | if (FromParent && StartI != EndI) { |
| 553 | StartI = std::next(StartI); |
| 554 | } |
| 555 | return getDSA(StartI, D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 558 | template <class ClausesPredicate, class DirectivesPredicate> |
| 559 | DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 560 | DirectivesPredicate DPred, |
| 561 | bool FromParent) { |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 562 | D = D->getCanonicalDecl(); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 563 | auto StartI = std::next(Stack.rbegin()); |
| 564 | auto EndI = std::prev(Stack.rend()); |
| 565 | if (FromParent && StartI != EndI) { |
| 566 | StartI = std::next(StartI); |
| 567 | } |
| 568 | for (auto I = StartI, EE = EndI; I != EE; ++I) { |
| 569 | if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 570 | continue; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 571 | DSAVarData DVar = getDSA(I, D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 572 | if (CPred(DVar.CKind)) |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 573 | return DVar; |
| 574 | } |
| 575 | return DSAVarData(); |
| 576 | } |
| 577 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 578 | template <class ClausesPredicate, class DirectivesPredicate> |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 579 | DSAStackTy::DSAVarData |
| 580 | DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred, |
| 581 | DirectivesPredicate DPred, bool FromParent) { |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 582 | D = D->getCanonicalDecl(); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 583 | auto StartI = std::next(Stack.rbegin()); |
| 584 | auto EndI = std::prev(Stack.rend()); |
| 585 | if (FromParent && StartI != EndI) { |
| 586 | StartI = std::next(StartI); |
| 587 | } |
| 588 | for (auto I = StartI, EE = EndI; I != EE; ++I) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 589 | if (!DPred(I->Directive)) |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 590 | break; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 591 | DSAVarData DVar = getDSA(I, D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 592 | if (CPred(DVar.CKind)) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 593 | return DVar; |
| 594 | return DSAVarData(); |
| 595 | } |
| 596 | return DSAVarData(); |
| 597 | } |
| 598 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 599 | bool DSAStackTy::hasExplicitDSA( |
| 600 | VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 601 | unsigned Level) { |
| 602 | if (CPred(ClauseKindMode)) |
| 603 | return true; |
| 604 | if (isClauseParsingMode()) |
| 605 | ++Level; |
| 606 | D = D->getCanonicalDecl(); |
| 607 | auto StartI = Stack.rbegin(); |
| 608 | auto EndI = std::prev(Stack.rend()); |
NAKAMURA Takumi | 0332eda | 2015-06-23 10:01:20 +0000 | [diff] [blame^] | 609 | if (std::distance(StartI, EndI) <= (int)Level) |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 610 | return false; |
| 611 | std::advance(StartI, Level); |
| 612 | return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr && |
| 613 | CPred(StartI->SharingMap[D].Attributes); |
| 614 | } |
| 615 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 616 | template <class NamedDirectivesPredicate> |
| 617 | bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) { |
| 618 | auto StartI = std::next(Stack.rbegin()); |
| 619 | auto EndI = std::prev(Stack.rend()); |
| 620 | if (FromParent && StartI != EndI) { |
| 621 | StartI = std::next(StartI); |
| 622 | } |
| 623 | for (auto I = StartI, EE = EndI; I != EE; ++I) { |
| 624 | if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) |
| 625 | return true; |
| 626 | } |
| 627 | return false; |
| 628 | } |
| 629 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 630 | void Sema::InitDataSharingAttributesStack() { |
| 631 | VarDataSharingAttributesStack = new DSAStackTy(*this); |
| 632 | } |
| 633 | |
| 634 | #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) |
| 635 | |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 636 | bool Sema::IsOpenMPCapturedVar(VarDecl *VD) { |
| 637 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 638 | VD = VD->getCanonicalDecl(); |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 639 | if (DSAStack->getCurrentDirective() != OMPD_unknown) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 640 | if (DSAStack->isLoopControlVariable(VD) || |
| 641 | (VD->hasLocalStorage() && |
| 642 | isParallelOrTaskRegion(DSAStack->getCurrentDirective()))) |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 643 | return true; |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 644 | auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode()); |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 645 | if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) |
| 646 | return true; |
| 647 | DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 648 | DSAStack->isClauseParsingMode()); |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 649 | return DVarPrivate.CKind != OMPC_unknown; |
| 650 | } |
| 651 | return false; |
| 652 | } |
| 653 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 654 | bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) { |
| 655 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
| 656 | return DSAStack->hasExplicitDSA( |
| 657 | VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level); |
| 658 | } |
| 659 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 660 | void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 661 | |
| 662 | void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, |
| 663 | const DeclarationNameInfo &DirName, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 664 | Scope *CurScope, SourceLocation Loc) { |
| 665 | DSAStack->push(DKind, DirName, CurScope, Loc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 666 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
| 667 | } |
| 668 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 669 | void Sema::StartOpenMPClause(OpenMPClauseKind K) { |
| 670 | DSAStack->setClauseParsingMode(K); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 673 | void Sema::EndOpenMPClause() { |
| 674 | DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 677 | void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 678 | // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] |
| 679 | // A variable of class type (or array thereof) that appears in a lastprivate |
| 680 | // clause requires an accessible, unambiguous default constructor for the |
| 681 | // class type, unless the list item is also specified in a firstprivate |
| 682 | // clause. |
| 683 | if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 684 | for (auto *C : D->clauses()) { |
| 685 | if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { |
| 686 | SmallVector<Expr *, 8> PrivateCopies; |
| 687 | for (auto *DE : Clause->varlists()) { |
| 688 | if (DE->isValueDependent() || DE->isTypeDependent()) { |
| 689 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 690 | continue; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 691 | } |
| 692 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl()); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 693 | QualType Type = VD->getType(); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 694 | auto DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 695 | if (DVar.CKind == OMPC_lastprivate) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 696 | // Generate helper private variable and initialize it with the |
| 697 | // default value. The address of the original variable is replaced |
| 698 | // by the address of the new private variable in CodeGen. This new |
| 699 | // variable is not added to IdResolver, so the code in the OpenMP |
| 700 | // region uses original variable for proper diagnostics. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 701 | auto *VDPrivate = |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 702 | buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(), |
| 703 | VD->getName()); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 704 | ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); |
| 705 | if (VDPrivate->isInvalidDecl()) |
| 706 | continue; |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 707 | PrivateCopies.push_back(buildDeclRefExpr( |
| 708 | *this, VDPrivate, DE->getType(), DE->getExprLoc())); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 709 | } else { |
| 710 | // The variable is also a firstprivate, so initialization sequence |
| 711 | // for private copy is generated already. |
| 712 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 715 | // Set initializers to private copies if no errors were found. |
| 716 | if (PrivateCopies.size() == Clause->varlist_size()) { |
| 717 | Clause->setPrivateCopies(PrivateCopies); |
| 718 | } |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 723 | DSAStack->pop(); |
| 724 | DiscardCleanupsInEvaluationContext(); |
| 725 | PopExpressionEvaluationContext(); |
| 726 | } |
| 727 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 728 | static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, |
| 729 | Expr *NumIterations, Sema &SemaRef, |
| 730 | Scope *S); |
| 731 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 732 | namespace { |
| 733 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 734 | class VarDeclFilterCCC : public CorrectionCandidateCallback { |
| 735 | private: |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 736 | Sema &SemaRef; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 737 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 738 | public: |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 739 | explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 740 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 741 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 742 | if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) { |
| 743 | return VD->hasGlobalStorage() && |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 744 | SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), |
| 745 | SemaRef.getCurScope()); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 746 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 747 | return false; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 748 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 749 | }; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 750 | } // namespace |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 751 | |
| 752 | ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, |
| 753 | CXXScopeSpec &ScopeSpec, |
| 754 | const DeclarationNameInfo &Id) { |
| 755 | LookupResult Lookup(*this, Id, LookupOrdinaryName); |
| 756 | LookupParsedName(Lookup, CurScope, &ScopeSpec, true); |
| 757 | |
| 758 | if (Lookup.isAmbiguous()) |
| 759 | return ExprError(); |
| 760 | |
| 761 | VarDecl *VD; |
| 762 | if (!Lookup.isSingleResult()) { |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 763 | if (TypoCorrection Corrected = CorrectTypo( |
| 764 | Id, LookupOrdinaryName, CurScope, nullptr, |
| 765 | llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 766 | diagnoseTypo(Corrected, |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 767 | PDiag(Lookup.empty() |
| 768 | ? diag::err_undeclared_var_use_suggest |
| 769 | : diag::err_omp_expected_var_arg_suggest) |
| 770 | << Id.getName()); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 771 | VD = Corrected.getCorrectionDeclAs<VarDecl>(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 772 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 773 | Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use |
| 774 | : diag::err_omp_expected_var_arg) |
| 775 | << Id.getName(); |
| 776 | return ExprError(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 777 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 778 | } else { |
| 779 | if (!(VD = Lookup.getAsSingle<VarDecl>())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 780 | Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 781 | Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); |
| 782 | return ExprError(); |
| 783 | } |
| 784 | } |
| 785 | Lookup.suppressDiagnostics(); |
| 786 | |
| 787 | // OpenMP [2.9.2, Syntax, C/C++] |
| 788 | // Variables must be file-scope, namespace-scope, or static block-scope. |
| 789 | if (!VD->hasGlobalStorage()) { |
| 790 | Diag(Id.getLoc(), diag::err_omp_global_var_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 791 | << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); |
| 792 | bool IsDecl = |
| 793 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 794 | Diag(VD->getLocation(), |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 795 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 796 | << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 797 | return ExprError(); |
| 798 | } |
| 799 | |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 800 | VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 801 | NamedDecl *ND = cast<NamedDecl>(CanonicalVD); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 802 | // OpenMP [2.9.2, Restrictions, C/C++, p.2] |
| 803 | // A threadprivate directive for file-scope variables must appear outside |
| 804 | // any definition or declaration. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 805 | if (CanonicalVD->getDeclContext()->isTranslationUnit() && |
| 806 | !getCurLexicalContext()->isTranslationUnit()) { |
| 807 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 808 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 809 | bool IsDecl = |
| 810 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 811 | Diag(VD->getLocation(), |
| 812 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 813 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 814 | return ExprError(); |
| 815 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 816 | // OpenMP [2.9.2, Restrictions, C/C++, p.3] |
| 817 | // A threadprivate directive for static class member variables must appear |
| 818 | // in the class definition, in the same scope in which the member |
| 819 | // variables are declared. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 820 | if (CanonicalVD->isStaticDataMember() && |
| 821 | !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { |
| 822 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 823 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 824 | bool IsDecl = |
| 825 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 826 | Diag(VD->getLocation(), |
| 827 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 828 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 829 | return ExprError(); |
| 830 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 831 | // OpenMP [2.9.2, Restrictions, C/C++, p.4] |
| 832 | // A threadprivate directive for namespace-scope variables must appear |
| 833 | // outside any definition or declaration other than the namespace |
| 834 | // definition itself. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 835 | if (CanonicalVD->getDeclContext()->isNamespace() && |
| 836 | (!getCurLexicalContext()->isFileContext() || |
| 837 | !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { |
| 838 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 839 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 840 | bool IsDecl = |
| 841 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 842 | Diag(VD->getLocation(), |
| 843 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 844 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 845 | return ExprError(); |
| 846 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 847 | // OpenMP [2.9.2, Restrictions, C/C++, p.6] |
| 848 | // A threadprivate directive for static block-scope variables must appear |
| 849 | // in the scope of the variable and not in a nested scope. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 850 | if (CanonicalVD->isStaticLocal() && CurScope && |
| 851 | !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 852 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 853 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 854 | bool IsDecl = |
| 855 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 856 | Diag(VD->getLocation(), |
| 857 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 858 | << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 859 | return ExprError(); |
| 860 | } |
| 861 | |
| 862 | // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] |
| 863 | // A threadprivate directive must lexically precede all references to any |
| 864 | // of the variables in its list. |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 865 | if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 866 | Diag(Id.getLoc(), diag::err_omp_var_used) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 867 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 868 | return ExprError(); |
| 869 | } |
| 870 | |
| 871 | QualType ExprType = VD->getType().getNonReferenceType(); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 872 | ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc()); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 873 | return DE; |
| 874 | } |
| 875 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 876 | Sema::DeclGroupPtrTy |
| 877 | Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, |
| 878 | ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 879 | if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 880 | CurContext->addDecl(D); |
| 881 | return DeclGroupPtrTy::make(DeclGroupRef(D)); |
| 882 | } |
| 883 | return DeclGroupPtrTy(); |
| 884 | } |
| 885 | |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 886 | namespace { |
| 887 | class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { |
| 888 | Sema &SemaRef; |
| 889 | |
| 890 | public: |
| 891 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
| 892 | if (auto VD = dyn_cast<VarDecl>(E->getDecl())) { |
| 893 | if (VD->hasLocalStorage()) { |
| 894 | SemaRef.Diag(E->getLocStart(), |
| 895 | diag::err_omp_local_var_in_threadprivate_init) |
| 896 | << E->getSourceRange(); |
| 897 | SemaRef.Diag(VD->getLocation(), diag::note_defined_here) |
| 898 | << VD << VD->getSourceRange(); |
| 899 | return true; |
| 900 | } |
| 901 | } |
| 902 | return false; |
| 903 | } |
| 904 | bool VisitStmt(const Stmt *S) { |
| 905 | for (auto Child : S->children()) { |
| 906 | if (Child && Visit(Child)) |
| 907 | return true; |
| 908 | } |
| 909 | return false; |
| 910 | } |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 911 | explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 912 | }; |
| 913 | } // namespace |
| 914 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 915 | OMPThreadPrivateDecl * |
| 916 | Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 917 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 918 | for (auto &RefExpr : VarList) { |
| 919 | DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 920 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 921 | SourceLocation ILoc = DE->getExprLoc(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 922 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 923 | QualType QType = VD->getType(); |
| 924 | if (QType->isDependentType() || QType->isInstantiationDependentType()) { |
| 925 | // It will be analyzed later. |
| 926 | Vars.push_back(DE); |
| 927 | continue; |
| 928 | } |
| 929 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 930 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 931 | // A threadprivate variable must not have an incomplete type. |
| 932 | if (RequireCompleteType(ILoc, VD->getType(), |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 933 | diag::err_omp_threadprivate_incomplete_type)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 934 | continue; |
| 935 | } |
| 936 | |
| 937 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 938 | // A threadprivate variable must not have a reference type. |
| 939 | if (VD->getType()->isReferenceType()) { |
| 940 | Diag(ILoc, diag::err_omp_ref_type_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 941 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); |
| 942 | bool IsDecl = |
| 943 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 944 | Diag(VD->getLocation(), |
| 945 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 946 | << VD; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 947 | continue; |
| 948 | } |
| 949 | |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 950 | // Check if this is a TLS variable. |
Alexey Bataev | 26a3924 | 2015-01-13 03:35:30 +0000 | [diff] [blame] | 951 | if (VD->getTLSKind() != VarDecl::TLS_None || |
Alexey Bataev | 1a8b3f1 | 2015-05-06 06:34:55 +0000 | [diff] [blame] | 952 | (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && |
| 953 | !VD->isLocalVarDecl())) { |
Alexey Bataev | 26a3924 | 2015-01-13 03:35:30 +0000 | [diff] [blame] | 954 | Diag(ILoc, diag::err_omp_var_thread_local) |
| 955 | << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 956 | bool IsDecl = |
| 957 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 958 | Diag(VD->getLocation(), |
| 959 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 960 | << VD; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 961 | continue; |
| 962 | } |
| 963 | |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 964 | // Check if initial value of threadprivate variable reference variable with |
| 965 | // local storage (it is not supported by runtime). |
| 966 | if (auto Init = VD->getAnyInitializer()) { |
| 967 | LocalVarRefChecker Checker(*this); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 968 | if (Checker.Visit(Init)) |
| 969 | continue; |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 972 | Vars.push_back(RefExpr); |
Alexey Bataev | d178ad4 | 2014-03-07 08:03:37 +0000 | [diff] [blame] | 973 | DSAStack->addDSA(VD, DE, OMPC_threadprivate); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 974 | VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( |
| 975 | Context, SourceRange(Loc, Loc))); |
| 976 | if (auto *ML = Context.getASTMutationListener()) |
| 977 | ML->DeclarationMarkedOpenMPThreadPrivate(VD); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 978 | } |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 979 | OMPThreadPrivateDecl *D = nullptr; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 980 | if (!Vars.empty()) { |
| 981 | D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, |
| 982 | Vars); |
| 983 | D->setAccess(AS_public); |
| 984 | } |
| 985 | return D; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 986 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 987 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 988 | static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, |
| 989 | const VarDecl *VD, DSAStackTy::DSAVarData DVar, |
| 990 | bool IsLoopIterVar = false) { |
| 991 | if (DVar.RefExpr) { |
| 992 | SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) |
| 993 | << getOpenMPClauseName(DVar.CKind); |
| 994 | return; |
| 995 | } |
| 996 | enum { |
| 997 | PDSA_StaticMemberShared, |
| 998 | PDSA_StaticLocalVarShared, |
| 999 | PDSA_LoopIterVarPrivate, |
| 1000 | PDSA_LoopIterVarLinear, |
| 1001 | PDSA_LoopIterVarLastprivate, |
| 1002 | PDSA_ConstVarShared, |
| 1003 | PDSA_GlobalVarShared, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1004 | PDSA_TaskVarFirstprivate, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1005 | PDSA_LocalVarPrivate, |
| 1006 | PDSA_Implicit |
| 1007 | } Reason = PDSA_Implicit; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1008 | bool ReportHint = false; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1009 | auto ReportLoc = VD->getLocation(); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1010 | if (IsLoopIterVar) { |
| 1011 | if (DVar.CKind == OMPC_private) |
| 1012 | Reason = PDSA_LoopIterVarPrivate; |
| 1013 | else if (DVar.CKind == OMPC_lastprivate) |
| 1014 | Reason = PDSA_LoopIterVarLastprivate; |
| 1015 | else |
| 1016 | Reason = PDSA_LoopIterVarLinear; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1017 | } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) { |
| 1018 | Reason = PDSA_TaskVarFirstprivate; |
| 1019 | ReportLoc = DVar.ImplicitDSALoc; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1020 | } else if (VD->isStaticLocal()) |
| 1021 | Reason = PDSA_StaticLocalVarShared; |
| 1022 | else if (VD->isStaticDataMember()) |
| 1023 | Reason = PDSA_StaticMemberShared; |
| 1024 | else if (VD->isFileVarDecl()) |
| 1025 | Reason = PDSA_GlobalVarShared; |
| 1026 | else if (VD->getType().isConstant(SemaRef.getASTContext())) |
| 1027 | Reason = PDSA_ConstVarShared; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1028 | else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1029 | ReportHint = true; |
| 1030 | Reason = PDSA_LocalVarPrivate; |
| 1031 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1032 | if (Reason != PDSA_Implicit) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1033 | SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1034 | << Reason << ReportHint |
| 1035 | << getOpenMPDirectiveName(Stack->getCurrentDirective()); |
| 1036 | } else if (DVar.ImplicitDSALoc.isValid()) { |
| 1037 | SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) |
| 1038 | << getOpenMPClauseName(DVar.CKind); |
| 1039 | } |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1042 | namespace { |
| 1043 | class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { |
| 1044 | DSAStackTy *Stack; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1045 | Sema &SemaRef; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1046 | bool ErrorFound; |
| 1047 | CapturedStmt *CS; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1048 | llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1049 | llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1050 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1051 | public: |
| 1052 | void VisitDeclRefExpr(DeclRefExpr *E) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1053 | if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1054 | // Skip internally declared variables. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1055 | if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) |
| 1056 | return; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1057 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1058 | auto DVar = Stack->getTopDSA(VD, false); |
| 1059 | // Check if the variable has explicit DSA set and stop analysis if it so. |
| 1060 | if (DVar.RefExpr) return; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1061 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1062 | auto ELoc = E->getExprLoc(); |
| 1063 | auto DKind = Stack->getCurrentDirective(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1064 | // The default(none) clause requires that each variable that is referenced |
| 1065 | // in the construct, and does not have a predetermined data-sharing |
| 1066 | // attribute, must have its data-sharing attribute explicitly determined |
| 1067 | // by being listed in a data-sharing attribute clause. |
| 1068 | if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1069 | isParallelOrTaskRegion(DKind) && |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1070 | VarsWithInheritedDSA.count(VD) == 0) { |
| 1071 | VarsWithInheritedDSA[VD] = E; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | // OpenMP [2.9.3.6, Restrictions, p.2] |
| 1076 | // A list item that appears in a reduction clause of the innermost |
| 1077 | // enclosing worksharing or parallel construct may not be accessed in an |
| 1078 | // explicit task. |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1079 | DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1080 | [](OpenMPDirectiveKind K) -> bool { |
| 1081 | return isOpenMPParallelDirective(K) || |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1082 | isOpenMPWorksharingDirective(K) || |
| 1083 | isOpenMPTeamsDirective(K); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1084 | }, |
| 1085 | false); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 1086 | if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) { |
| 1087 | ErrorFound = true; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1088 | SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); |
| 1089 | ReportOriginalDSA(SemaRef, Stack, VD, DVar); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 1090 | return; |
| 1091 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Define implicit data-sharing attributes for task. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1094 | DVar = Stack->getImplicitDSA(VD, false); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1095 | if (DKind == OMPD_task && DVar.CKind != OMPC_shared) |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1096 | ImplicitFirstprivate.push_back(E); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | void VisitOMPExecutableDirective(OMPExecutableDirective *S) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1100 | for (auto *C : S->clauses()) { |
| 1101 | // Skip analysis of arguments of implicitly defined firstprivate clause |
| 1102 | // for task directives. |
| 1103 | if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid())) |
| 1104 | for (auto *CC : C->children()) { |
| 1105 | if (CC) |
| 1106 | Visit(CC); |
| 1107 | } |
| 1108 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1109 | } |
| 1110 | void VisitStmt(Stmt *S) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1111 | for (auto *C : S->children()) { |
| 1112 | if (C && !isa<OMPExecutableDirective>(C)) |
| 1113 | Visit(C); |
| 1114 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1115 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1116 | |
| 1117 | bool isErrorFound() { return ErrorFound; } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1118 | ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1119 | llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() { |
| 1120 | return VarsWithInheritedDSA; |
| 1121 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1122 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1123 | DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) |
| 1124 | : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1125 | }; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1126 | } // namespace |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1127 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1128 | void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1129 | switch (DKind) { |
| 1130 | case OMPD_parallel: { |
| 1131 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 1132 | QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1133 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1134 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1135 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1136 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1137 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1138 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1139 | Params); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | } |
| 1142 | case OMPD_simd: { |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1143 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1144 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1145 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1146 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1147 | Params); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1148 | break; |
| 1149 | } |
| 1150 | case OMPD_for: { |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1151 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1152 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1153 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1154 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1155 | Params); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1156 | break; |
| 1157 | } |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1158 | case OMPD_for_simd: { |
| 1159 | Sema::CapturedParamNameType Params[] = { |
| 1160 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1161 | }; |
| 1162 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1163 | Params); |
| 1164 | break; |
| 1165 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1166 | case OMPD_sections: { |
| 1167 | Sema::CapturedParamNameType Params[] = { |
| 1168 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1169 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1170 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1171 | Params); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1172 | break; |
| 1173 | } |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1174 | case OMPD_section: { |
| 1175 | Sema::CapturedParamNameType Params[] = { |
| 1176 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1177 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1178 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1179 | Params); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1180 | break; |
| 1181 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1182 | case OMPD_single: { |
| 1183 | Sema::CapturedParamNameType Params[] = { |
| 1184 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1185 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1186 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1187 | Params); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1188 | break; |
| 1189 | } |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1190 | case OMPD_master: { |
| 1191 | Sema::CapturedParamNameType Params[] = { |
| 1192 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1193 | }; |
| 1194 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1195 | Params); |
| 1196 | break; |
| 1197 | } |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1198 | case OMPD_critical: { |
| 1199 | Sema::CapturedParamNameType Params[] = { |
| 1200 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1201 | }; |
| 1202 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1203 | Params); |
| 1204 | break; |
| 1205 | } |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1206 | case OMPD_parallel_for: { |
| 1207 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 1208 | QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); |
| 1209 | Sema::CapturedParamNameType Params[] = { |
| 1210 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1211 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1212 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1213 | }; |
| 1214 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1215 | Params); |
| 1216 | break; |
| 1217 | } |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1218 | case OMPD_parallel_for_simd: { |
| 1219 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 1220 | QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); |
| 1221 | Sema::CapturedParamNameType Params[] = { |
| 1222 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1223 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1224 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1225 | }; |
| 1226 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1227 | Params); |
| 1228 | break; |
| 1229 | } |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1230 | case OMPD_parallel_sections: { |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1231 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 1232 | QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1233 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1234 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1235 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1236 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1237 | }; |
| 1238 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1239 | Params); |
| 1240 | break; |
| 1241 | } |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1242 | case OMPD_task: { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1243 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1244 | QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; |
| 1245 | FunctionProtoType::ExtProtoInfo EPI; |
| 1246 | EPI.Variadic = true; |
| 1247 | QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1248 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1249 | std::make_pair(".global_tid.", KmpInt32Ty), |
| 1250 | std::make_pair(".part_id.", KmpInt32Ty), |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1251 | std::make_pair(".privates.", |
| 1252 | Context.VoidPtrTy.withConst().withRestrict()), |
| 1253 | std::make_pair( |
| 1254 | ".copy_fn.", |
| 1255 | Context.getPointerType(CopyFnType).withConst().withRestrict()), |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1256 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1257 | }; |
| 1258 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1259 | Params); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1260 | // Mark this captured region as inlined, because we don't use outlined |
| 1261 | // function directly. |
| 1262 | getCurCapturedRegion()->TheCapturedDecl->addAttr( |
| 1263 | AlwaysInlineAttr::CreateImplicit( |
| 1264 | Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1265 | break; |
| 1266 | } |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1267 | case OMPD_ordered: { |
| 1268 | Sema::CapturedParamNameType Params[] = { |
| 1269 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1270 | }; |
| 1271 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1272 | Params); |
| 1273 | break; |
| 1274 | } |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1275 | case OMPD_atomic: { |
| 1276 | Sema::CapturedParamNameType Params[] = { |
| 1277 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1278 | }; |
| 1279 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1280 | Params); |
| 1281 | break; |
| 1282 | } |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1283 | case OMPD_target: { |
| 1284 | Sema::CapturedParamNameType Params[] = { |
| 1285 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1286 | }; |
| 1287 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1288 | Params); |
| 1289 | break; |
| 1290 | } |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1291 | case OMPD_teams: { |
| 1292 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 1293 | QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); |
| 1294 | Sema::CapturedParamNameType Params[] = { |
| 1295 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1296 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1297 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1298 | }; |
| 1299 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1300 | Params); |
| 1301 | break; |
| 1302 | } |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1303 | case OMPD_taskgroup: { |
| 1304 | Sema::CapturedParamNameType Params[] = { |
| 1305 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1306 | }; |
| 1307 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1308 | Params); |
| 1309 | break; |
| 1310 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1311 | case OMPD_threadprivate: |
Alexey Bataev | ee9af45 | 2014-11-21 11:33:46 +0000 | [diff] [blame] | 1312 | case OMPD_taskyield: |
| 1313 | case OMPD_barrier: |
| 1314 | case OMPD_taskwait: |
| 1315 | case OMPD_flush: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1316 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 1317 | case OMPD_unknown: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1318 | llvm_unreachable("Unknown OpenMP directive"); |
| 1319 | } |
| 1320 | } |
| 1321 | |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1322 | StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, |
| 1323 | ArrayRef<OMPClause *> Clauses) { |
| 1324 | if (!S.isUsable()) { |
| 1325 | ActOnCapturedRegionError(); |
| 1326 | return StmtError(); |
| 1327 | } |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1328 | // This is required for proper codegen. |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1329 | for (auto *Clause : Clauses) { |
Alexey Bataev | 16dc7b6 | 2015-05-20 03:46:04 +0000 | [diff] [blame] | 1330 | if (isOpenMPPrivate(Clause->getClauseKind()) || |
| 1331 | Clause->getClauseKind() == OMPC_copyprivate) { |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1332 | // Mark all variables in private list clauses as used in inner region. |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1333 | for (auto *VarRef : Clause->children()) { |
| 1334 | if (auto *E = cast_or_null<Expr>(VarRef)) { |
Alexey Bataev | 8bf6b3e | 2015-04-02 13:07:08 +0000 | [diff] [blame] | 1335 | MarkDeclarationsReferencedInExpr(E); |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1338 | } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && |
| 1339 | Clause->getClauseKind() == OMPC_schedule) { |
| 1340 | // Mark all variables in private list clauses as used in inner region. |
| 1341 | // Required for proper codegen of combined directives. |
| 1342 | // TODO: add processing for other clauses. |
| 1343 | if (auto *E = cast_or_null<Expr>( |
| 1344 | cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) { |
| 1345 | MarkDeclarationsReferencedInExpr(E); |
| 1346 | } |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | return ActOnCapturedRegionEnd(S.get()); |
| 1350 | } |
| 1351 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1352 | static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, |
| 1353 | OpenMPDirectiveKind CurrentRegion, |
| 1354 | const DeclarationNameInfo &CurrentName, |
| 1355 | SourceLocation StartLoc) { |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1356 | // Allowed nesting of constructs |
| 1357 | // +------------------+-----------------+------------------------------------+ |
| 1358 | // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)| |
| 1359 | // +------------------+-----------------+------------------------------------+ |
| 1360 | // | parallel | parallel | * | |
| 1361 | // | parallel | for | * | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1362 | // | parallel | for simd | * | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1363 | // | parallel | master | * | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1364 | // | parallel | critical | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1365 | // | parallel | simd | * | |
| 1366 | // | parallel | sections | * | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1367 | // | parallel | section | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1368 | // | parallel | single | * | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1369 | // | parallel | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1370 | // | parallel |parallel for simd| * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1371 | // | parallel |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1372 | // | parallel | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1373 | // | parallel | taskyield | * | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1374 | // | parallel | barrier | * | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1375 | // | parallel | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1376 | // | parallel | taskgroup | * | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1377 | // | parallel | flush | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1378 | // | parallel | ordered | + | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1379 | // | parallel | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1380 | // | parallel | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1381 | // | parallel | teams | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1382 | // +------------------+-----------------+------------------------------------+ |
| 1383 | // | for | parallel | * | |
| 1384 | // | for | for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1385 | // | for | for simd | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1386 | // | for | master | + | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1387 | // | for | critical | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1388 | // | for | simd | * | |
| 1389 | // | for | sections | + | |
| 1390 | // | for | section | + | |
| 1391 | // | for | single | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1392 | // | for | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1393 | // | for |parallel for simd| * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1394 | // | for |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1395 | // | for | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1396 | // | for | taskyield | * | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1397 | // | for | barrier | + | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1398 | // | for | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1399 | // | for | taskgroup | * | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1400 | // | for | flush | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1401 | // | for | ordered | * (if construct is ordered) | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1402 | // | for | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1403 | // | for | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1404 | // | for | teams | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1405 | // +------------------+-----------------+------------------------------------+ |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1406 | // | master | parallel | * | |
| 1407 | // | master | for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1408 | // | master | for simd | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1409 | // | master | master | * | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1410 | // | master | critical | * | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1411 | // | master | simd | * | |
| 1412 | // | master | sections | + | |
| 1413 | // | master | section | + | |
| 1414 | // | master | single | + | |
| 1415 | // | master | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1416 | // | master |parallel for simd| * | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1417 | // | master |parallel sections| * | |
| 1418 | // | master | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1419 | // | master | taskyield | * | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1420 | // | master | barrier | + | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1421 | // | master | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1422 | // | master | taskgroup | * | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1423 | // | master | flush | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1424 | // | master | ordered | + | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1425 | // | master | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1426 | // | master | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1427 | // | master | teams | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1428 | // +------------------+-----------------+------------------------------------+ |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1429 | // | critical | parallel | * | |
| 1430 | // | critical | for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1431 | // | critical | for simd | + | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1432 | // | critical | master | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1433 | // | critical | critical | * (should have different names) | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1434 | // | critical | simd | * | |
| 1435 | // | critical | sections | + | |
| 1436 | // | critical | section | + | |
| 1437 | // | critical | single | + | |
| 1438 | // | critical | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1439 | // | critical |parallel for simd| * | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1440 | // | critical |parallel sections| * | |
| 1441 | // | critical | task | * | |
| 1442 | // | critical | taskyield | * | |
| 1443 | // | critical | barrier | + | |
| 1444 | // | critical | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1445 | // | critical | taskgroup | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1446 | // | critical | ordered | + | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1447 | // | critical | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1448 | // | critical | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1449 | // | critical | teams | + | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1450 | // +------------------+-----------------+------------------------------------+ |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1451 | // | simd | parallel | | |
| 1452 | // | simd | for | | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1453 | // | simd | for simd | | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1454 | // | simd | master | | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1455 | // | simd | critical | | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1456 | // | simd | simd | | |
| 1457 | // | simd | sections | | |
| 1458 | // | simd | section | | |
| 1459 | // | simd | single | | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1460 | // | simd | parallel for | | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1461 | // | simd |parallel for simd| | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1462 | // | simd |parallel sections| | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1463 | // | simd | task | | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1464 | // | simd | taskyield | | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1465 | // | simd | barrier | | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1466 | // | simd | taskwait | | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1467 | // | simd | taskgroup | | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1468 | // | simd | flush | | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1469 | // | simd | ordered | | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1470 | // | simd | atomic | | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1471 | // | simd | target | | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1472 | // | simd | teams | | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1473 | // +------------------+-----------------+------------------------------------+ |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1474 | // | for simd | parallel | | |
| 1475 | // | for simd | for | | |
| 1476 | // | for simd | for simd | | |
| 1477 | // | for simd | master | | |
| 1478 | // | for simd | critical | | |
| 1479 | // | for simd | simd | | |
| 1480 | // | for simd | sections | | |
| 1481 | // | for simd | section | | |
| 1482 | // | for simd | single | | |
| 1483 | // | for simd | parallel for | | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1484 | // | for simd |parallel for simd| | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1485 | // | for simd |parallel sections| | |
| 1486 | // | for simd | task | | |
| 1487 | // | for simd | taskyield | | |
| 1488 | // | for simd | barrier | | |
| 1489 | // | for simd | taskwait | | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1490 | // | for simd | taskgroup | | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1491 | // | for simd | flush | | |
| 1492 | // | for simd | ordered | | |
| 1493 | // | for simd | atomic | | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1494 | // | for simd | target | | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1495 | // | for simd | teams | | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1496 | // +------------------+-----------------+------------------------------------+ |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1497 | // | parallel for simd| parallel | | |
| 1498 | // | parallel for simd| for | | |
| 1499 | // | parallel for simd| for simd | | |
| 1500 | // | parallel for simd| master | | |
| 1501 | // | parallel for simd| critical | | |
| 1502 | // | parallel for simd| simd | | |
| 1503 | // | parallel for simd| sections | | |
| 1504 | // | parallel for simd| section | | |
| 1505 | // | parallel for simd| single | | |
| 1506 | // | parallel for simd| parallel for | | |
| 1507 | // | parallel for simd|parallel for simd| | |
| 1508 | // | parallel for simd|parallel sections| | |
| 1509 | // | parallel for simd| task | | |
| 1510 | // | parallel for simd| taskyield | | |
| 1511 | // | parallel for simd| barrier | | |
| 1512 | // | parallel for simd| taskwait | | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1513 | // | parallel for simd| taskgroup | | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1514 | // | parallel for simd| flush | | |
| 1515 | // | parallel for simd| ordered | | |
| 1516 | // | parallel for simd| atomic | | |
| 1517 | // | parallel for simd| target | | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1518 | // | parallel for simd| teams | | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1519 | // +------------------+-----------------+------------------------------------+ |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1520 | // | sections | parallel | * | |
| 1521 | // | sections | for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1522 | // | sections | for simd | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1523 | // | sections | master | + | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1524 | // | sections | critical | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1525 | // | sections | simd | * | |
| 1526 | // | sections | sections | + | |
| 1527 | // | sections | section | * | |
| 1528 | // | sections | single | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1529 | // | sections | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1530 | // | sections |parallel for simd| * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1531 | // | sections |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1532 | // | sections | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1533 | // | sections | taskyield | * | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1534 | // | sections | barrier | + | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1535 | // | sections | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1536 | // | sections | taskgroup | * | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1537 | // | sections | flush | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1538 | // | sections | ordered | + | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1539 | // | sections | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1540 | // | sections | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1541 | // | sections | teams | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1542 | // +------------------+-----------------+------------------------------------+ |
| 1543 | // | section | parallel | * | |
| 1544 | // | section | for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1545 | // | section | for simd | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1546 | // | section | master | + | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1547 | // | section | critical | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1548 | // | section | simd | * | |
| 1549 | // | section | sections | + | |
| 1550 | // | section | section | + | |
| 1551 | // | section | single | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1552 | // | section | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1553 | // | section |parallel for simd| * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1554 | // | section |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1555 | // | section | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1556 | // | section | taskyield | * | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1557 | // | section | barrier | + | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1558 | // | section | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1559 | // | section | taskgroup | * | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1560 | // | section | flush | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1561 | // | section | ordered | + | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1562 | // | section | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1563 | // | section | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1564 | // | section | teams | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1565 | // +------------------+-----------------+------------------------------------+ |
| 1566 | // | single | parallel | * | |
| 1567 | // | single | for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1568 | // | single | for simd | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1569 | // | single | master | + | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1570 | // | single | critical | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1571 | // | single | simd | * | |
| 1572 | // | single | sections | + | |
| 1573 | // | single | section | + | |
| 1574 | // | single | single | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1575 | // | single | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1576 | // | single |parallel for simd| * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1577 | // | single |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1578 | // | single | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1579 | // | single | taskyield | * | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1580 | // | single | barrier | + | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1581 | // | single | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1582 | // | single | taskgroup | * | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1583 | // | single | flush | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1584 | // | single | ordered | + | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1585 | // | single | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1586 | // | single | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1587 | // | single | teams | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1588 | // +------------------+-----------------+------------------------------------+ |
| 1589 | // | parallel for | parallel | * | |
| 1590 | // | parallel for | for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1591 | // | parallel for | for simd | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1592 | // | parallel for | master | + | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1593 | // | parallel for | critical | * | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1594 | // | parallel for | simd | * | |
| 1595 | // | parallel for | sections | + | |
| 1596 | // | parallel for | section | + | |
| 1597 | // | parallel for | single | + | |
| 1598 | // | parallel for | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1599 | // | parallel for |parallel for simd| * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1600 | // | parallel for |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1601 | // | parallel for | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1602 | // | parallel for | taskyield | * | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1603 | // | parallel for | barrier | + | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1604 | // | parallel for | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1605 | // | parallel for | taskgroup | * | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1606 | // | parallel for | flush | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1607 | // | parallel for | ordered | * (if construct is ordered) | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1608 | // | parallel for | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1609 | // | parallel for | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1610 | // | parallel for | teams | + | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1611 | // +------------------+-----------------+------------------------------------+ |
| 1612 | // | parallel sections| parallel | * | |
| 1613 | // | parallel sections| for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1614 | // | parallel sections| for simd | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1615 | // | parallel sections| master | + | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1616 | // | parallel sections| critical | + | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1617 | // | parallel sections| simd | * | |
| 1618 | // | parallel sections| sections | + | |
| 1619 | // | parallel sections| section | * | |
| 1620 | // | parallel sections| single | + | |
| 1621 | // | parallel sections| parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1622 | // | parallel sections|parallel for simd| * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1623 | // | parallel sections|parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1624 | // | parallel sections| task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1625 | // | parallel sections| taskyield | * | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1626 | // | parallel sections| barrier | + | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1627 | // | parallel sections| taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1628 | // | parallel sections| taskgroup | * | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1629 | // | parallel sections| flush | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1630 | // | parallel sections| ordered | + | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1631 | // | parallel sections| atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1632 | // | parallel sections| target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1633 | // | parallel sections| teams | + | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1634 | // +------------------+-----------------+------------------------------------+ |
| 1635 | // | task | parallel | * | |
| 1636 | // | task | for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1637 | // | task | for simd | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1638 | // | task | master | + | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1639 | // | task | critical | * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1640 | // | task | simd | * | |
| 1641 | // | task | sections | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1642 | // | task | section | + | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1643 | // | task | single | + | |
| 1644 | // | task | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1645 | // | task |parallel for simd| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1646 | // | task |parallel sections| * | |
| 1647 | // | task | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1648 | // | task | taskyield | * | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1649 | // | task | barrier | + | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1650 | // | task | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1651 | // | task | taskgroup | * | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1652 | // | task | flush | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1653 | // | task | ordered | + | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1654 | // | task | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1655 | // | task | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1656 | // | task | teams | + | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1657 | // +------------------+-----------------+------------------------------------+ |
| 1658 | // | ordered | parallel | * | |
| 1659 | // | ordered | for | + | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1660 | // | ordered | for simd | + | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1661 | // | ordered | master | * | |
| 1662 | // | ordered | critical | * | |
| 1663 | // | ordered | simd | * | |
| 1664 | // | ordered | sections | + | |
| 1665 | // | ordered | section | + | |
| 1666 | // | ordered | single | + | |
| 1667 | // | ordered | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1668 | // | ordered |parallel for simd| * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1669 | // | ordered |parallel sections| * | |
| 1670 | // | ordered | task | * | |
| 1671 | // | ordered | taskyield | * | |
| 1672 | // | ordered | barrier | + | |
| 1673 | // | ordered | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1674 | // | ordered | taskgroup | * | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1675 | // | ordered | flush | * | |
| 1676 | // | ordered | ordered | + | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1677 | // | ordered | atomic | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1678 | // | ordered | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1679 | // | ordered | teams | + | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1680 | // +------------------+-----------------+------------------------------------+ |
| 1681 | // | atomic | parallel | | |
| 1682 | // | atomic | for | | |
| 1683 | // | atomic | for simd | | |
| 1684 | // | atomic | master | | |
| 1685 | // | atomic | critical | | |
| 1686 | // | atomic | simd | | |
| 1687 | // | atomic | sections | | |
| 1688 | // | atomic | section | | |
| 1689 | // | atomic | single | | |
| 1690 | // | atomic | parallel for | | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1691 | // | atomic |parallel for simd| | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1692 | // | atomic |parallel sections| | |
| 1693 | // | atomic | task | | |
| 1694 | // | atomic | taskyield | | |
| 1695 | // | atomic | barrier | | |
| 1696 | // | atomic | taskwait | | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1697 | // | atomic | taskgroup | | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1698 | // | atomic | flush | | |
| 1699 | // | atomic | ordered | | |
| 1700 | // | atomic | atomic | | |
| 1701 | // | atomic | target | | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1702 | // | atomic | teams | | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1703 | // +------------------+-----------------+------------------------------------+ |
| 1704 | // | target | parallel | * | |
| 1705 | // | target | for | * | |
| 1706 | // | target | for simd | * | |
| 1707 | // | target | master | * | |
| 1708 | // | target | critical | * | |
| 1709 | // | target | simd | * | |
| 1710 | // | target | sections | * | |
| 1711 | // | target | section | * | |
| 1712 | // | target | single | * | |
| 1713 | // | target | parallel for | * | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1714 | // | target |parallel for simd| * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1715 | // | target |parallel sections| * | |
| 1716 | // | target | task | * | |
| 1717 | // | target | taskyield | * | |
| 1718 | // | target | barrier | * | |
| 1719 | // | target | taskwait | * | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1720 | // | target | taskgroup | * | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1721 | // | target | flush | * | |
| 1722 | // | target | ordered | * | |
| 1723 | // | target | atomic | * | |
| 1724 | // | target | target | * | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1725 | // | target | teams | * | |
| 1726 | // +------------------+-----------------+------------------------------------+ |
| 1727 | // | teams | parallel | * | |
| 1728 | // | teams | for | + | |
| 1729 | // | teams | for simd | + | |
| 1730 | // | teams | master | + | |
| 1731 | // | teams | critical | + | |
| 1732 | // | teams | simd | + | |
| 1733 | // | teams | sections | + | |
| 1734 | // | teams | section | + | |
| 1735 | // | teams | single | + | |
| 1736 | // | teams | parallel for | * | |
| 1737 | // | teams |parallel for simd| * | |
| 1738 | // | teams |parallel sections| * | |
| 1739 | // | teams | task | + | |
| 1740 | // | teams | taskyield | + | |
| 1741 | // | teams | barrier | + | |
| 1742 | // | teams | taskwait | + | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1743 | // | teams | taskgroup | + | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1744 | // | teams | flush | + | |
| 1745 | // | teams | ordered | + | |
| 1746 | // | teams | atomic | + | |
| 1747 | // | teams | target | + | |
| 1748 | // | teams | teams | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1749 | // +------------------+-----------------+------------------------------------+ |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1750 | if (Stack->getCurScope()) { |
| 1751 | auto ParentRegion = Stack->getParentDirective(); |
| 1752 | bool NestingProhibited = false; |
| 1753 | bool CloseNesting = true; |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1754 | enum { |
| 1755 | NoRecommend, |
| 1756 | ShouldBeInParallelRegion, |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1757 | ShouldBeInOrderedRegion, |
| 1758 | ShouldBeInTargetRegion |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1759 | } Recommend = NoRecommend; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1760 | if (isOpenMPSimdDirective(ParentRegion)) { |
| 1761 | // OpenMP [2.16, Nesting of Regions] |
| 1762 | // OpenMP constructs may not be nested inside a simd region. |
| 1763 | SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd); |
| 1764 | return true; |
| 1765 | } |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1766 | if (ParentRegion == OMPD_atomic) { |
| 1767 | // OpenMP [2.16, Nesting of Regions] |
| 1768 | // OpenMP constructs may not be nested inside an atomic region. |
| 1769 | SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); |
| 1770 | return true; |
| 1771 | } |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1772 | if (CurrentRegion == OMPD_section) { |
| 1773 | // OpenMP [2.7.2, sections Construct, Restrictions] |
| 1774 | // Orphaned section directives are prohibited. That is, the section |
| 1775 | // directives must appear within the sections construct and must not be |
| 1776 | // encountered elsewhere in the sections region. |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1777 | if (ParentRegion != OMPD_sections && |
| 1778 | ParentRegion != OMPD_parallel_sections) { |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1779 | SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) |
| 1780 | << (ParentRegion != OMPD_unknown) |
| 1781 | << getOpenMPDirectiveName(ParentRegion); |
| 1782 | return true; |
| 1783 | } |
| 1784 | return false; |
| 1785 | } |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1786 | // Allow some constructs to be orphaned (they could be used in functions, |
| 1787 | // called from OpenMP regions with the required preconditions). |
| 1788 | if (ParentRegion == OMPD_unknown) |
| 1789 | return false; |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1790 | if (CurrentRegion == OMPD_master) { |
| 1791 | // OpenMP [2.16, Nesting of Regions] |
| 1792 | // A master region may not be closely nested inside a worksharing, |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1793 | // atomic, or explicit task region. |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1794 | NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || |
| 1795 | ParentRegion == OMPD_task; |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1796 | } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { |
| 1797 | // OpenMP [2.16, Nesting of Regions] |
| 1798 | // A critical region may not be nested (closely or otherwise) inside a |
| 1799 | // critical region with the same name. Note that this restriction is not |
| 1800 | // sufficient to prevent deadlock. |
| 1801 | SourceLocation PreviousCriticalLoc; |
| 1802 | bool DeadLock = |
| 1803 | Stack->hasDirective([CurrentName, &PreviousCriticalLoc]( |
| 1804 | OpenMPDirectiveKind K, |
| 1805 | const DeclarationNameInfo &DNI, |
| 1806 | SourceLocation Loc) |
| 1807 | ->bool { |
| 1808 | if (K == OMPD_critical && |
| 1809 | DNI.getName() == CurrentName.getName()) { |
| 1810 | PreviousCriticalLoc = Loc; |
| 1811 | return true; |
| 1812 | } else |
| 1813 | return false; |
| 1814 | }, |
| 1815 | false /* skip top directive */); |
| 1816 | if (DeadLock) { |
| 1817 | SemaRef.Diag(StartLoc, |
| 1818 | diag::err_omp_prohibited_region_critical_same_name) |
| 1819 | << CurrentName.getName(); |
| 1820 | if (PreviousCriticalLoc.isValid()) |
| 1821 | SemaRef.Diag(PreviousCriticalLoc, |
| 1822 | diag::note_omp_previous_critical_region); |
| 1823 | return true; |
| 1824 | } |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1825 | } else if (CurrentRegion == OMPD_barrier) { |
| 1826 | // OpenMP [2.16, Nesting of Regions] |
| 1827 | // A barrier region may not be closely nested inside a worksharing, |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1828 | // explicit task, critical, ordered, atomic, or master region. |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1829 | NestingProhibited = |
| 1830 | isOpenMPWorksharingDirective(ParentRegion) || |
| 1831 | ParentRegion == OMPD_task || ParentRegion == OMPD_master || |
| 1832 | ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1833 | } else if (isOpenMPWorksharingDirective(CurrentRegion) && |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1834 | !isOpenMPParallelDirective(CurrentRegion)) { |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1835 | // OpenMP [2.16, Nesting of Regions] |
| 1836 | // A worksharing region may not be closely nested inside a worksharing, |
| 1837 | // explicit task, critical, ordered, atomic, or master region. |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1838 | NestingProhibited = |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1839 | isOpenMPWorksharingDirective(ParentRegion) || |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1840 | ParentRegion == OMPD_task || ParentRegion == OMPD_master || |
| 1841 | ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; |
| 1842 | Recommend = ShouldBeInParallelRegion; |
| 1843 | } else if (CurrentRegion == OMPD_ordered) { |
| 1844 | // OpenMP [2.16, Nesting of Regions] |
| 1845 | // An ordered region may not be closely nested inside a critical, |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1846 | // atomic, or explicit task region. |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1847 | // An ordered region must be closely nested inside a loop region (or |
| 1848 | // parallel loop region) with an ordered clause. |
| 1849 | NestingProhibited = ParentRegion == OMPD_critical || |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1850 | ParentRegion == OMPD_task || |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1851 | !Stack->isParentOrderedRegion(); |
| 1852 | Recommend = ShouldBeInOrderedRegion; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1853 | } else if (isOpenMPTeamsDirective(CurrentRegion)) { |
| 1854 | // OpenMP [2.16, Nesting of Regions] |
| 1855 | // If specified, a teams construct must be contained within a target |
| 1856 | // construct. |
| 1857 | NestingProhibited = ParentRegion != OMPD_target; |
| 1858 | Recommend = ShouldBeInTargetRegion; |
| 1859 | Stack->setParentTeamsRegionLoc(Stack->getConstructLoc()); |
| 1860 | } |
| 1861 | if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) { |
| 1862 | // OpenMP [2.16, Nesting of Regions] |
| 1863 | // distribute, parallel, parallel sections, parallel workshare, and the |
| 1864 | // parallel loop and parallel loop SIMD constructs are the only OpenMP |
| 1865 | // constructs that can be closely nested in the teams region. |
| 1866 | // TODO: add distribute directive. |
| 1867 | NestingProhibited = !isOpenMPParallelDirective(CurrentRegion); |
| 1868 | Recommend = ShouldBeInParallelRegion; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1869 | } |
| 1870 | if (NestingProhibited) { |
| 1871 | SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1872 | << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend |
| 1873 | << getOpenMPDirectiveName(CurrentRegion); |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1874 | return true; |
| 1875 | } |
| 1876 | } |
| 1877 | return false; |
| 1878 | } |
| 1879 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1880 | StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1881 | const DeclarationNameInfo &DirName, |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1882 | ArrayRef<OMPClause *> Clauses, |
| 1883 | Stmt *AStmt, |
| 1884 | SourceLocation StartLoc, |
| 1885 | SourceLocation EndLoc) { |
| 1886 | StmtResult Res = StmtError(); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1887 | if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc)) |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1888 | return StmtError(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1889 | |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1890 | llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1891 | llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1892 | bool ErrorFound = false; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1893 | ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1894 | if (AStmt) { |
| 1895 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 1896 | |
| 1897 | // Check default data sharing attributes for referenced variables. |
| 1898 | DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); |
| 1899 | DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); |
| 1900 | if (DSAChecker.isErrorFound()) |
| 1901 | return StmtError(); |
| 1902 | // Generate list of implicitly defined firstprivate variables. |
| 1903 | VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1904 | |
| 1905 | if (!DSAChecker.getImplicitFirstprivate().empty()) { |
| 1906 | if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( |
| 1907 | DSAChecker.getImplicitFirstprivate(), SourceLocation(), |
| 1908 | SourceLocation(), SourceLocation())) { |
| 1909 | ClausesWithImplicit.push_back(Implicit); |
| 1910 | ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != |
| 1911 | DSAChecker.getImplicitFirstprivate().size(); |
| 1912 | } else |
| 1913 | ErrorFound = true; |
| 1914 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1915 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1916 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1917 | switch (Kind) { |
| 1918 | case OMPD_parallel: |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1919 | Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 1920 | EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1921 | break; |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1922 | case OMPD_simd: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1923 | Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, |
| 1924 | VarsWithInheritedDSA); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1925 | break; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1926 | case OMPD_for: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1927 | Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, |
| 1928 | VarsWithInheritedDSA); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1929 | break; |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1930 | case OMPD_for_simd: |
| 1931 | Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 1932 | EndLoc, VarsWithInheritedDSA); |
| 1933 | break; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1934 | case OMPD_sections: |
| 1935 | Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 1936 | EndLoc); |
| 1937 | break; |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1938 | case OMPD_section: |
| 1939 | assert(ClausesWithImplicit.empty() && |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1940 | "No clauses are allowed for 'omp section' directive"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1941 | Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); |
| 1942 | break; |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1943 | case OMPD_single: |
| 1944 | Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 1945 | EndLoc); |
| 1946 | break; |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1947 | case OMPD_master: |
| 1948 | assert(ClausesWithImplicit.empty() && |
| 1949 | "No clauses are allowed for 'omp master' directive"); |
| 1950 | Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); |
| 1951 | break; |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1952 | case OMPD_critical: |
| 1953 | assert(ClausesWithImplicit.empty() && |
| 1954 | "No clauses are allowed for 'omp critical' directive"); |
| 1955 | Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc); |
| 1956 | break; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1957 | case OMPD_parallel_for: |
| 1958 | Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 1959 | EndLoc, VarsWithInheritedDSA); |
| 1960 | break; |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1961 | case OMPD_parallel_for_simd: |
| 1962 | Res = ActOnOpenMPParallelForSimdDirective( |
| 1963 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 1964 | break; |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1965 | case OMPD_parallel_sections: |
| 1966 | Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, |
| 1967 | StartLoc, EndLoc); |
| 1968 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1969 | case OMPD_task: |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1970 | Res = |
| 1971 | ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); |
| 1972 | break; |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1973 | case OMPD_taskyield: |
| 1974 | assert(ClausesWithImplicit.empty() && |
| 1975 | "No clauses are allowed for 'omp taskyield' directive"); |
| 1976 | assert(AStmt == nullptr && |
| 1977 | "No associated statement allowed for 'omp taskyield' directive"); |
| 1978 | Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); |
| 1979 | break; |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1980 | case OMPD_barrier: |
| 1981 | assert(ClausesWithImplicit.empty() && |
| 1982 | "No clauses are allowed for 'omp barrier' directive"); |
| 1983 | assert(AStmt == nullptr && |
| 1984 | "No associated statement allowed for 'omp barrier' directive"); |
| 1985 | Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); |
| 1986 | break; |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1987 | case OMPD_taskwait: |
| 1988 | assert(ClausesWithImplicit.empty() && |
| 1989 | "No clauses are allowed for 'omp taskwait' directive"); |
| 1990 | assert(AStmt == nullptr && |
| 1991 | "No associated statement allowed for 'omp taskwait' directive"); |
| 1992 | Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); |
| 1993 | break; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1994 | case OMPD_taskgroup: |
| 1995 | assert(ClausesWithImplicit.empty() && |
| 1996 | "No clauses are allowed for 'omp taskgroup' directive"); |
| 1997 | Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc); |
| 1998 | break; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1999 | case OMPD_flush: |
| 2000 | assert(AStmt == nullptr && |
| 2001 | "No associated statement allowed for 'omp flush' directive"); |
| 2002 | Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); |
| 2003 | break; |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2004 | case OMPD_ordered: |
| 2005 | assert(ClausesWithImplicit.empty() && |
| 2006 | "No clauses are allowed for 'omp ordered' directive"); |
| 2007 | Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc); |
| 2008 | break; |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2009 | case OMPD_atomic: |
| 2010 | Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2011 | EndLoc); |
| 2012 | break; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2013 | case OMPD_teams: |
| 2014 | Res = |
| 2015 | ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); |
| 2016 | break; |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2017 | case OMPD_target: |
| 2018 | Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2019 | EndLoc); |
| 2020 | break; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2021 | case OMPD_threadprivate: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2022 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 2023 | case OMPD_unknown: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2024 | llvm_unreachable("Unknown OpenMP directive"); |
| 2025 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2026 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2027 | for (auto P : VarsWithInheritedDSA) { |
| 2028 | Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) |
| 2029 | << P.first << P.second->getSourceRange(); |
| 2030 | } |
| 2031 | if (!VarsWithInheritedDSA.empty()) |
| 2032 | return StmtError(); |
| 2033 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2034 | if (ErrorFound) |
| 2035 | return StmtError(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2036 | return Res; |
| 2037 | } |
| 2038 | |
| 2039 | StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, |
| 2040 | Stmt *AStmt, |
| 2041 | SourceLocation StartLoc, |
| 2042 | SourceLocation EndLoc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2043 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 2044 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 2045 | // 1.2.2 OpenMP Language Terminology |
| 2046 | // Structured block - An executable statement with a single entry at the |
| 2047 | // top and a single exit at the bottom. |
| 2048 | // The point of exit cannot be a branch out of the structured block. |
| 2049 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 2050 | CS->getCapturedDecl()->setNothrow(); |
| 2051 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2052 | getCurFunction()->setHasBranchProtectedScope(); |
| 2053 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2054 | return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 2055 | AStmt); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2058 | namespace { |
| 2059 | /// \brief Helper class for checking canonical form of the OpenMP loops and |
| 2060 | /// extracting iteration space of each loop in the loop nest, that will be used |
| 2061 | /// for IR generation. |
| 2062 | class OpenMPIterationSpaceChecker { |
| 2063 | /// \brief Reference to Sema. |
| 2064 | Sema &SemaRef; |
| 2065 | /// \brief A location for diagnostics (when there is no some better location). |
| 2066 | SourceLocation DefaultLoc; |
| 2067 | /// \brief A location for diagnostics (when increment is not compatible). |
| 2068 | SourceLocation ConditionLoc; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2069 | /// \brief A source location for referring to loop init later. |
| 2070 | SourceRange InitSrcRange; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2071 | /// \brief A source location for referring to condition later. |
| 2072 | SourceRange ConditionSrcRange; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2073 | /// \brief A source location for referring to increment later. |
| 2074 | SourceRange IncrementSrcRange; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2075 | /// \brief Loop variable. |
| 2076 | VarDecl *Var; |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2077 | /// \brief Reference to loop variable. |
| 2078 | DeclRefExpr *VarRef; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2079 | /// \brief Lower bound (initializer for the var). |
| 2080 | Expr *LB; |
| 2081 | /// \brief Upper bound. |
| 2082 | Expr *UB; |
| 2083 | /// \brief Loop step (increment). |
| 2084 | Expr *Step; |
| 2085 | /// \brief This flag is true when condition is one of: |
| 2086 | /// Var < UB |
| 2087 | /// Var <= UB |
| 2088 | /// UB > Var |
| 2089 | /// UB >= Var |
| 2090 | bool TestIsLessOp; |
| 2091 | /// \brief This flag is true when condition is strict ( < or > ). |
| 2092 | bool TestIsStrictOp; |
| 2093 | /// \brief This flag is true when step is subtracted on each iteration. |
| 2094 | bool SubtractStep; |
| 2095 | |
| 2096 | public: |
| 2097 | OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) |
| 2098 | : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc), |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2099 | InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()), |
| 2100 | IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr), |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2101 | LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false), |
| 2102 | TestIsStrictOp(false), SubtractStep(false) {} |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2103 | /// \brief Check init-expr for canonical loop form and save loop counter |
| 2104 | /// variable - #Var and its initialization value - #LB. |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2105 | bool CheckInit(Stmt *S, bool EmitDiags = true); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2106 | /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags |
| 2107 | /// for less/greater and for strict/non-strict comparison. |
| 2108 | bool CheckCond(Expr *S); |
| 2109 | /// \brief Check incr-expr for canonical loop form and return true if it |
| 2110 | /// does not conform, otherwise save loop step (#Step). |
| 2111 | bool CheckInc(Expr *S); |
| 2112 | /// \brief Return the loop counter variable. |
| 2113 | VarDecl *GetLoopVar() const { return Var; } |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2114 | /// \brief Return the reference expression to loop counter variable. |
| 2115 | DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2116 | /// \brief Source range of the loop init. |
| 2117 | SourceRange GetInitSrcRange() const { return InitSrcRange; } |
| 2118 | /// \brief Source range of the loop condition. |
| 2119 | SourceRange GetConditionSrcRange() const { return ConditionSrcRange; } |
| 2120 | /// \brief Source range of the loop increment. |
| 2121 | SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; } |
| 2122 | /// \brief True if the step should be subtracted. |
| 2123 | bool ShouldSubtractStep() const { return SubtractStep; } |
| 2124 | /// \brief Build the expression to calculate the number of iterations. |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 2125 | Expr *BuildNumIterations(Scope *S, const bool LimitedType) const; |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2126 | /// \brief Build the precondition expression for the loops. |
| 2127 | Expr *BuildPreCond(Scope *S, Expr *Cond) const; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2128 | /// \brief Build reference expression to the counter be used for codegen. |
| 2129 | Expr *BuildCounterVar() const; |
| 2130 | /// \brief Build initization of the counter be used for codegen. |
| 2131 | Expr *BuildCounterInit() const; |
| 2132 | /// \brief Build step of the counter be used for codegen. |
| 2133 | Expr *BuildCounterStep() const; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2134 | /// \brief Return true if any expression is dependent. |
| 2135 | bool Dependent() const; |
| 2136 | |
| 2137 | private: |
| 2138 | /// \brief Check the right-hand side of an assignment in the increment |
| 2139 | /// expression. |
| 2140 | bool CheckIncRHS(Expr *RHS); |
| 2141 | /// \brief Helper to set loop counter variable and its initializer. |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2142 | bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2143 | /// \brief Helper to set upper bound. |
| 2144 | bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR, |
| 2145 | const SourceLocation &SL); |
| 2146 | /// \brief Helper to set loop increment. |
| 2147 | bool SetStep(Expr *NewStep, bool Subtract); |
| 2148 | }; |
| 2149 | |
| 2150 | bool OpenMPIterationSpaceChecker::Dependent() const { |
| 2151 | if (!Var) { |
| 2152 | assert(!LB && !UB && !Step); |
| 2153 | return false; |
| 2154 | } |
| 2155 | return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) || |
| 2156 | (UB && UB->isValueDependent()) || (Step && Step->isValueDependent()); |
| 2157 | } |
| 2158 | |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2159 | bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, |
| 2160 | DeclRefExpr *NewVarRefExpr, |
| 2161 | Expr *NewLB) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2162 | // State consistency checking to ensure correct usage. |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2163 | assert(Var == nullptr && LB == nullptr && VarRef == nullptr && |
| 2164 | UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2165 | if (!NewVar || !NewLB) |
| 2166 | return true; |
| 2167 | Var = NewVar; |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2168 | VarRef = NewVarRefExpr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2169 | LB = NewLB; |
| 2170 | return false; |
| 2171 | } |
| 2172 | |
| 2173 | bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, |
| 2174 | const SourceRange &SR, |
| 2175 | const SourceLocation &SL) { |
| 2176 | // State consistency checking to ensure correct usage. |
| 2177 | assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr && |
| 2178 | !TestIsLessOp && !TestIsStrictOp); |
| 2179 | if (!NewUB) |
| 2180 | return true; |
| 2181 | UB = NewUB; |
| 2182 | TestIsLessOp = LessOp; |
| 2183 | TestIsStrictOp = StrictOp; |
| 2184 | ConditionSrcRange = SR; |
| 2185 | ConditionLoc = SL; |
| 2186 | return false; |
| 2187 | } |
| 2188 | |
| 2189 | bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { |
| 2190 | // State consistency checking to ensure correct usage. |
| 2191 | assert(Var != nullptr && LB != nullptr && Step == nullptr); |
| 2192 | if (!NewStep) |
| 2193 | return true; |
| 2194 | if (!NewStep->isValueDependent()) { |
| 2195 | // Check that the step is integer expression. |
| 2196 | SourceLocation StepLoc = NewStep->getLocStart(); |
| 2197 | ExprResult Val = |
| 2198 | SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); |
| 2199 | if (Val.isInvalid()) |
| 2200 | return true; |
| 2201 | NewStep = Val.get(); |
| 2202 | |
| 2203 | // OpenMP [2.6, Canonical Loop Form, Restrictions] |
| 2204 | // If test-expr is of form var relational-op b and relational-op is < or |
| 2205 | // <= then incr-expr must cause var to increase on each iteration of the |
| 2206 | // loop. If test-expr is of form var relational-op b and relational-op is |
| 2207 | // > or >= then incr-expr must cause var to decrease on each iteration of |
| 2208 | // the loop. |
| 2209 | // If test-expr is of form b relational-op var and relational-op is < or |
| 2210 | // <= then incr-expr must cause var to decrease on each iteration of the |
| 2211 | // loop. If test-expr is of form b relational-op var and relational-op is |
| 2212 | // > or >= then incr-expr must cause var to increase on each iteration of |
| 2213 | // the loop. |
| 2214 | llvm::APSInt Result; |
| 2215 | bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); |
| 2216 | bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); |
| 2217 | bool IsConstNeg = |
| 2218 | IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2219 | bool IsConstPos = |
| 2220 | IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2221 | bool IsConstZero = IsConstant && !Result.getBoolValue(); |
| 2222 | if (UB && (IsConstZero || |
| 2223 | (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2224 | : (IsConstPos || (IsUnsigned && !Subtract))))) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2225 | SemaRef.Diag(NewStep->getExprLoc(), |
| 2226 | diag::err_omp_loop_incr_not_compatible) |
| 2227 | << Var << TestIsLessOp << NewStep->getSourceRange(); |
| 2228 | SemaRef.Diag(ConditionLoc, |
| 2229 | diag::note_omp_loop_cond_requres_compatible_incr) |
| 2230 | << TestIsLessOp << ConditionSrcRange; |
| 2231 | return true; |
| 2232 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2233 | if (TestIsLessOp == Subtract) { |
| 2234 | NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, |
| 2235 | NewStep).get(); |
| 2236 | Subtract = !Subtract; |
| 2237 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | Step = NewStep; |
| 2241 | SubtractStep = Subtract; |
| 2242 | return false; |
| 2243 | } |
| 2244 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2245 | bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2246 | // Check init-expr for canonical loop form and save loop counter |
| 2247 | // variable - #Var and its initialization value - #LB. |
| 2248 | // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: |
| 2249 | // var = lb |
| 2250 | // integer-type var = lb |
| 2251 | // random-access-iterator-type var = lb |
| 2252 | // pointer-type var = lb |
| 2253 | // |
| 2254 | if (!S) { |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2255 | if (EmitDiags) { |
| 2256 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); |
| 2257 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2258 | return true; |
| 2259 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2260 | InitSrcRange = S->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2261 | if (Expr *E = dyn_cast<Expr>(S)) |
| 2262 | S = E->IgnoreParens(); |
| 2263 | if (auto BO = dyn_cast<BinaryOperator>(S)) { |
| 2264 | if (BO->getOpcode() == BO_Assign) |
| 2265 | if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2266 | return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2267 | BO->getRHS()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2268 | } else if (auto DS = dyn_cast<DeclStmt>(S)) { |
| 2269 | if (DS->isSingleDecl()) { |
| 2270 | if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { |
| 2271 | if (Var->hasInit()) { |
| 2272 | // Accept non-canonical init form here but emit ext. warning. |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2273 | if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2274 | SemaRef.Diag(S->getLocStart(), |
| 2275 | diag::ext_omp_loop_not_canonical_init) |
| 2276 | << S->getSourceRange(); |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2277 | return SetVarAndLB(Var, nullptr, Var->getInit()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) |
| 2282 | if (CE->getOperator() == OO_Equal) |
| 2283 | if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0))) |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2284 | return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE, |
| 2285 | CE->getArg(1)); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2286 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2287 | if (EmitDiags) { |
| 2288 | SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) |
| 2289 | << S->getSourceRange(); |
| 2290 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2291 | return true; |
| 2292 | } |
| 2293 | |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 2294 | /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2295 | /// variable (which may be the loop variable) if possible. |
| 2296 | static const VarDecl *GetInitVarDecl(const Expr *E) { |
| 2297 | if (!E) |
Craig Topper | 4b56692 | 2014-06-09 02:04:02 +0000 | [diff] [blame] | 2298 | return nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2299 | E = E->IgnoreParenImpCasts(); |
| 2300 | if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) |
| 2301 | if (const CXXConstructorDecl *Ctor = CE->getConstructor()) |
| 2302 | if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 && |
| 2303 | CE->getArg(0) != nullptr) |
| 2304 | E = CE->getArg(0)->IgnoreParenImpCasts(); |
| 2305 | auto DRE = dyn_cast_or_null<DeclRefExpr>(E); |
| 2306 | if (!DRE) |
| 2307 | return nullptr; |
| 2308 | return dyn_cast<VarDecl>(DRE->getDecl()); |
| 2309 | } |
| 2310 | |
| 2311 | bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { |
| 2312 | // Check test-expr for canonical form, save upper-bound UB, flags for |
| 2313 | // less/greater and for strict/non-strict comparison. |
| 2314 | // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: |
| 2315 | // var relational-op b |
| 2316 | // b relational-op var |
| 2317 | // |
| 2318 | if (!S) { |
| 2319 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var; |
| 2320 | return true; |
| 2321 | } |
| 2322 | S = S->IgnoreParenImpCasts(); |
| 2323 | SourceLocation CondLoc = S->getLocStart(); |
| 2324 | if (auto BO = dyn_cast<BinaryOperator>(S)) { |
| 2325 | if (BO->isRelationalOp()) { |
| 2326 | if (GetInitVarDecl(BO->getLHS()) == Var) |
| 2327 | return SetUB(BO->getRHS(), |
| 2328 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), |
| 2329 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), |
| 2330 | BO->getSourceRange(), BO->getOperatorLoc()); |
| 2331 | if (GetInitVarDecl(BO->getRHS()) == Var) |
| 2332 | return SetUB(BO->getLHS(), |
| 2333 | (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), |
| 2334 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), |
| 2335 | BO->getSourceRange(), BO->getOperatorLoc()); |
| 2336 | } |
| 2337 | } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
| 2338 | if (CE->getNumArgs() == 2) { |
| 2339 | auto Op = CE->getOperator(); |
| 2340 | switch (Op) { |
| 2341 | case OO_Greater: |
| 2342 | case OO_GreaterEqual: |
| 2343 | case OO_Less: |
| 2344 | case OO_LessEqual: |
| 2345 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 2346 | return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, |
| 2347 | Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), |
| 2348 | CE->getOperatorLoc()); |
| 2349 | if (GetInitVarDecl(CE->getArg(1)) == Var) |
| 2350 | return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, |
| 2351 | Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), |
| 2352 | CE->getOperatorLoc()); |
| 2353 | break; |
| 2354 | default: |
| 2355 | break; |
| 2356 | } |
| 2357 | } |
| 2358 | } |
| 2359 | SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) |
| 2360 | << S->getSourceRange() << Var; |
| 2361 | return true; |
| 2362 | } |
| 2363 | |
| 2364 | bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { |
| 2365 | // RHS of canonical loop form increment can be: |
| 2366 | // var + incr |
| 2367 | // incr + var |
| 2368 | // var - incr |
| 2369 | // |
| 2370 | RHS = RHS->IgnoreParenImpCasts(); |
| 2371 | if (auto BO = dyn_cast<BinaryOperator>(RHS)) { |
| 2372 | if (BO->isAdditiveOp()) { |
| 2373 | bool IsAdd = BO->getOpcode() == BO_Add; |
| 2374 | if (GetInitVarDecl(BO->getLHS()) == Var) |
| 2375 | return SetStep(BO->getRHS(), !IsAdd); |
| 2376 | if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var) |
| 2377 | return SetStep(BO->getLHS(), false); |
| 2378 | } |
| 2379 | } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { |
| 2380 | bool IsAdd = CE->getOperator() == OO_Plus; |
| 2381 | if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { |
| 2382 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 2383 | return SetStep(CE->getArg(1), !IsAdd); |
| 2384 | if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var) |
| 2385 | return SetStep(CE->getArg(0), false); |
| 2386 | } |
| 2387 | } |
| 2388 | SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) |
| 2389 | << RHS->getSourceRange() << Var; |
| 2390 | return true; |
| 2391 | } |
| 2392 | |
| 2393 | bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { |
| 2394 | // Check incr-expr for canonical loop form and return true if it |
| 2395 | // does not conform. |
| 2396 | // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: |
| 2397 | // ++var |
| 2398 | // var++ |
| 2399 | // --var |
| 2400 | // var-- |
| 2401 | // var += incr |
| 2402 | // var -= incr |
| 2403 | // var = var + incr |
| 2404 | // var = incr + var |
| 2405 | // var = var - incr |
| 2406 | // |
| 2407 | if (!S) { |
| 2408 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var; |
| 2409 | return true; |
| 2410 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2411 | IncrementSrcRange = S->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2412 | S = S->IgnoreParens(); |
| 2413 | if (auto UO = dyn_cast<UnaryOperator>(S)) { |
| 2414 | if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var) |
| 2415 | return SetStep( |
| 2416 | SemaRef.ActOnIntegerConstant(UO->getLocStart(), |
| 2417 | (UO->isDecrementOp() ? -1 : 1)).get(), |
| 2418 | false); |
| 2419 | } else if (auto BO = dyn_cast<BinaryOperator>(S)) { |
| 2420 | switch (BO->getOpcode()) { |
| 2421 | case BO_AddAssign: |
| 2422 | case BO_SubAssign: |
| 2423 | if (GetInitVarDecl(BO->getLHS()) == Var) |
| 2424 | return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); |
| 2425 | break; |
| 2426 | case BO_Assign: |
| 2427 | if (GetInitVarDecl(BO->getLHS()) == Var) |
| 2428 | return CheckIncRHS(BO->getRHS()); |
| 2429 | break; |
| 2430 | default: |
| 2431 | break; |
| 2432 | } |
| 2433 | } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
| 2434 | switch (CE->getOperator()) { |
| 2435 | case OO_PlusPlus: |
| 2436 | case OO_MinusMinus: |
| 2437 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 2438 | return SetStep( |
| 2439 | SemaRef.ActOnIntegerConstant( |
| 2440 | CE->getLocStart(), |
| 2441 | ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(), |
| 2442 | false); |
| 2443 | break; |
| 2444 | case OO_PlusEqual: |
| 2445 | case OO_MinusEqual: |
| 2446 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 2447 | return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); |
| 2448 | break; |
| 2449 | case OO_Equal: |
| 2450 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 2451 | return CheckIncRHS(CE->getArg(1)); |
| 2452 | break; |
| 2453 | default: |
| 2454 | break; |
| 2455 | } |
| 2456 | } |
| 2457 | SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) |
| 2458 | << S->getSourceRange() << Var; |
| 2459 | return true; |
| 2460 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2461 | |
| 2462 | /// \brief Build the expression to calculate the number of iterations. |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 2463 | Expr * |
| 2464 | OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S, |
| 2465 | const bool LimitedType) const { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2466 | ExprResult Diff; |
| 2467 | if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() || |
| 2468 | SemaRef.getLangOpts().CPlusPlus) { |
| 2469 | // Upper - Lower |
| 2470 | Expr *Upper = TestIsLessOp ? UB : LB; |
| 2471 | Expr *Lower = TestIsLessOp ? LB : UB; |
| 2472 | |
| 2473 | Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); |
| 2474 | |
| 2475 | if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) { |
| 2476 | // BuildBinOp already emitted error, this one is to point user to upper |
| 2477 | // and lower bound, and to tell what is passed to 'operator-'. |
| 2478 | SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) |
| 2479 | << Upper->getSourceRange() << Lower->getSourceRange(); |
| 2480 | return nullptr; |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | if (!Diff.isUsable()) |
| 2485 | return nullptr; |
| 2486 | |
| 2487 | // Upper - Lower [- 1] |
| 2488 | if (TestIsStrictOp) |
| 2489 | Diff = SemaRef.BuildBinOp( |
| 2490 | S, DefaultLoc, BO_Sub, Diff.get(), |
| 2491 | SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); |
| 2492 | if (!Diff.isUsable()) |
| 2493 | return nullptr; |
| 2494 | |
| 2495 | // Upper - Lower [- 1] + Step |
| 2496 | Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), |
| 2497 | Step->IgnoreImplicit()); |
| 2498 | if (!Diff.isUsable()) |
| 2499 | return nullptr; |
| 2500 | |
| 2501 | // Parentheses (for dumping/debugging purposes only). |
| 2502 | Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); |
| 2503 | if (!Diff.isUsable()) |
| 2504 | return nullptr; |
| 2505 | |
| 2506 | // (Upper - Lower [- 1] + Step) / Step |
| 2507 | Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), |
| 2508 | Step->IgnoreImplicit()); |
| 2509 | if (!Diff.isUsable()) |
| 2510 | return nullptr; |
| 2511 | |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 2512 | // OpenMP runtime requires 32-bit or 64-bit loop variables. |
| 2513 | if (LimitedType) { |
| 2514 | auto &C = SemaRef.Context; |
| 2515 | QualType Type = Diff.get()->getType(); |
| 2516 | unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; |
| 2517 | if (NewSize != C.getTypeSize(Type)) { |
| 2518 | if (NewSize < C.getTypeSize(Type)) { |
| 2519 | assert(NewSize == 64 && "incorrect loop var size"); |
| 2520 | SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) |
| 2521 | << InitSrcRange << ConditionSrcRange; |
| 2522 | } |
| 2523 | QualType NewType = C.getIntTypeForBitwidth( |
| 2524 | NewSize, Type->hasSignedIntegerRepresentation()); |
| 2525 | Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, |
| 2526 | Sema::AA_Converting, true); |
| 2527 | if (!Diff.isUsable()) |
| 2528 | return nullptr; |
| 2529 | } |
| 2530 | } |
| 2531 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2532 | return Diff.get(); |
| 2533 | } |
| 2534 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2535 | Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const { |
| 2536 | // Try to build LB <op> UB, where <op> is <, >, <=, or >=. |
| 2537 | bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); |
| 2538 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); |
| 2539 | auto CondExpr = SemaRef.BuildBinOp( |
| 2540 | S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) |
| 2541 | : (TestIsStrictOp ? BO_GT : BO_GE), |
| 2542 | LB, UB); |
| 2543 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); |
| 2544 | // Otherwise use original loop conditon and evaluate it in runtime. |
| 2545 | return CondExpr.isUsable() ? CondExpr.get() : Cond; |
| 2546 | } |
| 2547 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2548 | /// \brief Build reference expression to the counter be used for codegen. |
| 2549 | Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 2550 | return buildDeclRefExpr(SemaRef, Var, Var->getType(), DefaultLoc); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | /// \brief Build initization of the counter be used for codegen. |
| 2554 | Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; } |
| 2555 | |
| 2556 | /// \brief Build step of the counter be used for codegen. |
| 2557 | Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; } |
| 2558 | |
| 2559 | /// \brief Iteration space of a single for loop. |
| 2560 | struct LoopIterationSpace { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2561 | /// \brief Condition of the loop. |
| 2562 | Expr *PreCond; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2563 | /// \brief This expression calculates the number of iterations in the loop. |
| 2564 | /// It is always possible to calculate it before starting the loop. |
| 2565 | Expr *NumIterations; |
| 2566 | /// \brief The loop counter variable. |
| 2567 | Expr *CounterVar; |
| 2568 | /// \brief This is initializer for the initial value of #CounterVar. |
| 2569 | Expr *CounterInit; |
| 2570 | /// \brief This is step for the #CounterVar used to generate its update: |
| 2571 | /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. |
| 2572 | Expr *CounterStep; |
| 2573 | /// \brief Should step be subtracted? |
| 2574 | bool Subtract; |
| 2575 | /// \brief Source range of the loop init. |
| 2576 | SourceRange InitSrcRange; |
| 2577 | /// \brief Source range of the loop condition. |
| 2578 | SourceRange CondSrcRange; |
| 2579 | /// \brief Source range of the loop increment. |
| 2580 | SourceRange IncSrcRange; |
| 2581 | }; |
| 2582 | |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 2583 | } // namespace |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2584 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2585 | void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { |
| 2586 | assert(getLangOpts().OpenMP && "OpenMP is not active."); |
| 2587 | assert(Init && "Expected loop in canonical form."); |
| 2588 | unsigned CollapseIteration = DSAStack->getCollapseNumber(); |
| 2589 | if (CollapseIteration > 0 && |
| 2590 | isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { |
| 2591 | OpenMPIterationSpaceChecker ISC(*this, ForLoc); |
| 2592 | if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) { |
| 2593 | DSAStack->addLoopControlVariable(ISC.GetLoopVar()); |
| 2594 | } |
| 2595 | DSAStack->setCollapseNumber(CollapseIteration - 1); |
| 2596 | } |
| 2597 | } |
| 2598 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2599 | /// \brief Called on a for stmt to check and extract its iteration space |
| 2600 | /// for further processing (such as collapsing). |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2601 | static bool CheckOpenMPIterationSpace( |
| 2602 | OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, |
| 2603 | unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, |
| 2604 | Expr *NestedLoopCountExpr, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2605 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA, |
| 2606 | LoopIterationSpace &ResultIterSpace) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2607 | // OpenMP [2.6, Canonical Loop Form] |
| 2608 | // for (init-expr; test-expr; incr-expr) structured-block |
| 2609 | auto For = dyn_cast_or_null<ForStmt>(S); |
| 2610 | if (!For) { |
| 2611 | SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 2612 | << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind) |
| 2613 | << NestedLoopCount << (CurrentNestedLoopCount > 0) |
| 2614 | << CurrentNestedLoopCount; |
| 2615 | if (NestedLoopCount > 1) |
| 2616 | SemaRef.Diag(NestedLoopCountExpr->getExprLoc(), |
| 2617 | diag::note_omp_collapse_expr) |
| 2618 | << NestedLoopCountExpr->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2619 | return true; |
| 2620 | } |
| 2621 | assert(For->getBody()); |
| 2622 | |
| 2623 | OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); |
| 2624 | |
| 2625 | // Check init. |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 2626 | auto Init = For->getInit(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2627 | if (ISC.CheckInit(Init)) { |
| 2628 | return true; |
| 2629 | } |
| 2630 | |
| 2631 | bool HasErrors = false; |
| 2632 | |
| 2633 | // Check loop variable's type. |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 2634 | auto Var = ISC.GetLoopVar(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2635 | |
| 2636 | // OpenMP [2.6, Canonical Loop Form] |
| 2637 | // Var is one of the following: |
| 2638 | // A variable of signed or unsigned integer type. |
| 2639 | // For C++, a variable of a random access iterator type. |
| 2640 | // For C, a variable of a pointer type. |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 2641 | auto VarType = Var->getType(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2642 | if (!VarType->isDependentType() && !VarType->isIntegerType() && |
| 2643 | !VarType->isPointerType() && |
| 2644 | !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { |
| 2645 | SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) |
| 2646 | << SemaRef.getLangOpts().CPlusPlus; |
| 2647 | HasErrors = true; |
| 2648 | } |
| 2649 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2650 | // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a |
| 2651 | // Construct |
| 2652 | // The loop iteration variable(s) in the associated for-loop(s) of a for or |
| 2653 | // parallel for construct is (are) private. |
| 2654 | // The loop iteration variable in the associated for-loop of a simd construct |
| 2655 | // with just one associated for-loop is linear with a constant-linear-step |
| 2656 | // that is the increment of the associated for-loop. |
| 2657 | // Exclude loop var from the list of variables with implicitly defined data |
| 2658 | // sharing attributes. |
Benjamin Kramer | ad8e079 | 2014-10-10 15:32:48 +0000 | [diff] [blame] | 2659 | VarsWithImplicitDSA.erase(Var); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2660 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2661 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in |
| 2662 | // a Construct, C/C++]. |
Alexey Bataev | cefffae | 2014-06-23 08:21:53 +0000 | [diff] [blame] | 2663 | // The loop iteration variable in the associated for-loop of a simd construct |
| 2664 | // with just one associated for-loop may be listed in a linear clause with a |
| 2665 | // constant-linear-step that is the increment of the associated for-loop. |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2666 | // The loop iteration variable(s) in the associated for-loop(s) of a for or |
| 2667 | // parallel for construct may be listed in a private or lastprivate clause. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2668 | DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false); |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2669 | auto LoopVarRefExpr = ISC.GetLoopVarRefExpr(); |
| 2670 | // If LoopVarRefExpr is nullptr it means the corresponding loop variable is |
| 2671 | // declared in the loop and it is predetermined as a private. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2672 | auto PredeterminedCKind = |
| 2673 | isOpenMPSimdDirective(DKind) |
| 2674 | ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) |
| 2675 | : OMPC_private; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2676 | if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && |
Alexey Bataev | 0c024df | 2015-05-12 09:02:07 +0000 | [diff] [blame] | 2677 | DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) || |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2678 | (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) && |
| 2679 | DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private && |
Alexey Bataev | 0c024df | 2015-05-12 09:02:07 +0000 | [diff] [blame] | 2680 | DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) && |
| 2681 | ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) || |
| 2682 | DVar.RefExpr != nullptr)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2683 | SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2684 | << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) |
| 2685 | << getOpenMPClauseName(PredeterminedCKind); |
Alexey Bataev | f2453a0 | 2015-05-06 07:25:08 +0000 | [diff] [blame] | 2686 | if (DVar.RefExpr == nullptr) |
| 2687 | DVar.CKind = PredeterminedCKind; |
| 2688 | ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2689 | HasErrors = true; |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2690 | } else if (LoopVarRefExpr != nullptr) { |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2691 | // Make the loop iteration variable private (for worksharing constructs), |
| 2692 | // linear (for simd directives with the only one associated loop) or |
| 2693 | // lastprivate (for simd directives with several collapsed loops). |
Alexey Bataev | 9aba41c | 2014-11-14 04:08:45 +0000 | [diff] [blame] | 2694 | if (DVar.CKind == OMPC_unknown) |
| 2695 | DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(), |
| 2696 | /*FromParent=*/false); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2697 | DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 2700 | assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2701 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2702 | // Check test-expr. |
| 2703 | HasErrors |= ISC.CheckCond(For->getCond()); |
| 2704 | |
| 2705 | // Check incr-expr. |
| 2706 | HasErrors |= ISC.CheckInc(For->getInc()); |
| 2707 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2708 | if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2709 | return HasErrors; |
| 2710 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2711 | // Build the loop's iteration space representation. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2712 | ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond()); |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 2713 | ResultIterSpace.NumIterations = ISC.BuildNumIterations( |
| 2714 | DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2715 | ResultIterSpace.CounterVar = ISC.BuildCounterVar(); |
| 2716 | ResultIterSpace.CounterInit = ISC.BuildCounterInit(); |
| 2717 | ResultIterSpace.CounterStep = ISC.BuildCounterStep(); |
| 2718 | ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange(); |
| 2719 | ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange(); |
| 2720 | ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange(); |
| 2721 | ResultIterSpace.Subtract = ISC.ShouldSubtractStep(); |
| 2722 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2723 | HasErrors |= (ResultIterSpace.PreCond == nullptr || |
| 2724 | ResultIterSpace.NumIterations == nullptr || |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2725 | ResultIterSpace.CounterVar == nullptr || |
| 2726 | ResultIterSpace.CounterInit == nullptr || |
| 2727 | ResultIterSpace.CounterStep == nullptr); |
| 2728 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2729 | return HasErrors; |
| 2730 | } |
| 2731 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2732 | /// \brief Build 'VarRef = Start + Iter * Step'. |
| 2733 | static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S, |
| 2734 | SourceLocation Loc, ExprResult VarRef, |
| 2735 | ExprResult Start, ExprResult Iter, |
| 2736 | ExprResult Step, bool Subtract) { |
| 2737 | // Add parentheses (for debugging purposes only). |
| 2738 | Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); |
| 2739 | if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || |
| 2740 | !Step.isUsable()) |
| 2741 | return ExprError(); |
| 2742 | |
| 2743 | ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), |
| 2744 | Step.get()->IgnoreImplicit()); |
| 2745 | if (!Update.isUsable()) |
| 2746 | return ExprError(); |
| 2747 | |
| 2748 | // Build 'VarRef = Start + Iter * Step'. |
| 2749 | Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add), |
| 2750 | Start.get()->IgnoreImplicit(), Update.get()); |
| 2751 | if (!Update.isUsable()) |
| 2752 | return ExprError(); |
| 2753 | |
| 2754 | Update = SemaRef.PerformImplicitConversion( |
| 2755 | Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); |
| 2756 | if (!Update.isUsable()) |
| 2757 | return ExprError(); |
| 2758 | |
| 2759 | Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); |
| 2760 | return Update; |
| 2761 | } |
| 2762 | |
| 2763 | /// \brief Convert integer expression \a E to make it have at least \a Bits |
| 2764 | /// bits. |
| 2765 | static ExprResult WidenIterationCount(unsigned Bits, Expr *E, |
| 2766 | Sema &SemaRef) { |
| 2767 | if (E == nullptr) |
| 2768 | return ExprError(); |
| 2769 | auto &C = SemaRef.Context; |
| 2770 | QualType OldType = E->getType(); |
| 2771 | unsigned HasBits = C.getTypeSize(OldType); |
| 2772 | if (HasBits >= Bits) |
| 2773 | return ExprResult(E); |
| 2774 | // OK to convert to signed, because new type has more bits than old. |
| 2775 | QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); |
| 2776 | return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, |
| 2777 | true); |
| 2778 | } |
| 2779 | |
| 2780 | /// \brief Check if the given expression \a E is a constant integer that fits |
| 2781 | /// into \a Bits bits. |
| 2782 | static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) { |
| 2783 | if (E == nullptr) |
| 2784 | return false; |
| 2785 | llvm::APSInt Result; |
| 2786 | if (E->isIntegerConstantExpr(Result, SemaRef.Context)) |
| 2787 | return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); |
| 2788 | return false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | /// \brief Called on a for stmt to check itself and nested loops (if any). |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 2792 | /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, |
| 2793 | /// number of collapsed loops otherwise. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2794 | static unsigned |
| 2795 | CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr, |
| 2796 | Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2797 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA, |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2798 | OMPLoopDirective::HelperExprs &Built) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 2799 | unsigned NestedLoopCount = 1; |
| 2800 | if (NestedLoopCountExpr) { |
| 2801 | // Found 'collapse' clause - calculate collapse number. |
| 2802 | llvm::APSInt Result; |
| 2803 | if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) |
| 2804 | NestedLoopCount = Result.getLimitedValue(); |
| 2805 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2806 | // This is helper routine for loop directives (e.g., 'for', 'simd', |
| 2807 | // 'for simd', etc.). |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2808 | SmallVector<LoopIterationSpace, 4> IterSpaces; |
| 2809 | IterSpaces.resize(NestedLoopCount); |
| 2810 | Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2811 | for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 2812 | if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2813 | NestedLoopCount, NestedLoopCountExpr, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2814 | VarsWithImplicitDSA, IterSpaces[Cnt])) |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 2815 | return 0; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2816 | // Move on to the next nested for loop, or to the loop body. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2817 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 2818 | // All loops associated with the construct must be perfectly nested; that |
| 2819 | // is, there must be no intervening code nor any OpenMP directive between |
| 2820 | // any two loops. |
| 2821 | CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2824 | Built.clear(/* size */ NestedLoopCount); |
| 2825 | |
| 2826 | if (SemaRef.CurContext->isDependentContext()) |
| 2827 | return NestedLoopCount; |
| 2828 | |
| 2829 | // An example of what is generated for the following code: |
| 2830 | // |
| 2831 | // #pragma omp simd collapse(2) |
| 2832 | // for (i = 0; i < NI; ++i) |
| 2833 | // for (j = J0; j < NJ; j+=2) { |
| 2834 | // <loop body> |
| 2835 | // } |
| 2836 | // |
| 2837 | // We generate the code below. |
| 2838 | // Note: the loop body may be outlined in CodeGen. |
| 2839 | // Note: some counters may be C++ classes, operator- is used to find number of |
| 2840 | // iterations and operator+= to calculate counter value. |
| 2841 | // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 |
| 2842 | // or i64 is currently supported). |
| 2843 | // |
| 2844 | // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) |
| 2845 | // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { |
| 2846 | // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); |
| 2847 | // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; |
| 2848 | // // similar updates for vars in clauses (e.g. 'linear') |
| 2849 | // <loop body (using local i and j)> |
| 2850 | // } |
| 2851 | // i = NI; // assign final values of counters |
| 2852 | // j = NJ; |
| 2853 | // |
| 2854 | |
| 2855 | // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are |
| 2856 | // the iteration counts of the collapsed for loops. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2857 | // Precondition tests if there is at least one iteration (all conditions are |
| 2858 | // true). |
| 2859 | auto PreCond = ExprResult(IterSpaces[0].PreCond); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2860 | auto N0 = IterSpaces[0].NumIterations; |
| 2861 | ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef); |
| 2862 | ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef); |
| 2863 | |
| 2864 | if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) |
| 2865 | return NestedLoopCount; |
| 2866 | |
| 2867 | auto &C = SemaRef.Context; |
| 2868 | bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; |
| 2869 | |
| 2870 | Scope *CurScope = DSA.getCurScope(); |
| 2871 | for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2872 | if (PreCond.isUsable()) { |
| 2873 | PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd, |
| 2874 | PreCond.get(), IterSpaces[Cnt].PreCond); |
| 2875 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2876 | auto N = IterSpaces[Cnt].NumIterations; |
| 2877 | AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; |
| 2878 | if (LastIteration32.isUsable()) |
| 2879 | LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul, |
| 2880 | LastIteration32.get(), N); |
| 2881 | if (LastIteration64.isUsable()) |
| 2882 | LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul, |
| 2883 | LastIteration64.get(), N); |
| 2884 | } |
| 2885 | |
| 2886 | // Choose either the 32-bit or 64-bit version. |
| 2887 | ExprResult LastIteration = LastIteration64; |
| 2888 | if (LastIteration32.isUsable() && |
| 2889 | C.getTypeSize(LastIteration32.get()->getType()) == 32 && |
| 2890 | (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || |
| 2891 | FitsInto( |
| 2892 | 32 /* Bits */, |
| 2893 | LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), |
| 2894 | LastIteration64.get(), SemaRef))) |
| 2895 | LastIteration = LastIteration32; |
| 2896 | |
| 2897 | if (!LastIteration.isUsable()) |
| 2898 | return 0; |
| 2899 | |
| 2900 | // Save the number of iterations. |
| 2901 | ExprResult NumIterations = LastIteration; |
| 2902 | { |
| 2903 | LastIteration = SemaRef.BuildBinOp( |
| 2904 | CurScope, SourceLocation(), BO_Sub, LastIteration.get(), |
| 2905 | SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); |
| 2906 | if (!LastIteration.isUsable()) |
| 2907 | return 0; |
| 2908 | } |
| 2909 | |
| 2910 | // Calculate the last iteration number beforehand instead of doing this on |
| 2911 | // each iteration. Do not do this if the number of iterations may be kfold-ed. |
| 2912 | llvm::APSInt Result; |
| 2913 | bool IsConstant = |
| 2914 | LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); |
| 2915 | ExprResult CalcLastIteration; |
| 2916 | if (!IsConstant) { |
| 2917 | SourceLocation SaveLoc; |
| 2918 | VarDecl *SaveVar = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 2919 | buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(), |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2920 | ".omp.last.iteration"); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 2921 | ExprResult SaveRef = buildDeclRefExpr( |
| 2922 | SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2923 | CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign, |
| 2924 | SaveRef.get(), LastIteration.get()); |
| 2925 | LastIteration = SaveRef; |
| 2926 | |
| 2927 | // Prepare SaveRef + 1. |
| 2928 | NumIterations = SemaRef.BuildBinOp( |
| 2929 | CurScope, SaveLoc, BO_Add, SaveRef.get(), |
| 2930 | SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); |
| 2931 | if (!NumIterations.isUsable()) |
| 2932 | return 0; |
| 2933 | } |
| 2934 | |
| 2935 | SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); |
| 2936 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2937 | QualType VType = LastIteration.get()->getType(); |
| 2938 | // Build variables passed into runtime, nesessary for worksharing directives. |
| 2939 | ExprResult LB, UB, IL, ST, EUB; |
| 2940 | if (isOpenMPWorksharingDirective(DKind)) { |
| 2941 | // Lower bound variable, initialized with zero. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 2942 | VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); |
| 2943 | LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2944 | SemaRef.AddInitializerToDecl( |
| 2945 | LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), |
| 2946 | /*DirectInit*/ false, /*TypeMayContainAuto*/ false); |
| 2947 | |
| 2948 | // Upper bound variable, initialized with last iteration number. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 2949 | VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); |
| 2950 | UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2951 | SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), |
| 2952 | /*DirectInit*/ false, |
| 2953 | /*TypeMayContainAuto*/ false); |
| 2954 | |
| 2955 | // A 32-bit variable-flag where runtime returns 1 for the last iteration. |
| 2956 | // This will be used to implement clause 'lastprivate'. |
| 2957 | QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 2958 | VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); |
| 2959 | IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2960 | SemaRef.AddInitializerToDecl( |
| 2961 | ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), |
| 2962 | /*DirectInit*/ false, /*TypeMayContainAuto*/ false); |
| 2963 | |
| 2964 | // Stride variable returned by runtime (we initialize it to 1 by default). |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 2965 | VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride"); |
| 2966 | ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2967 | SemaRef.AddInitializerToDecl( |
| 2968 | STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), |
| 2969 | /*DirectInit*/ false, /*TypeMayContainAuto*/ false); |
| 2970 | |
| 2971 | // Build expression: UB = min(UB, LastIteration) |
| 2972 | // It is nesessary for CodeGen of directives with static scheduling. |
| 2973 | ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, |
| 2974 | UB.get(), LastIteration.get()); |
| 2975 | ExprResult CondOp = SemaRef.ActOnConditionalOp( |
| 2976 | InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get()); |
| 2977 | EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), |
| 2978 | CondOp.get()); |
| 2979 | EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); |
| 2980 | } |
| 2981 | |
| 2982 | // Build the iteration variable and its initialization before loop. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2983 | ExprResult IV; |
| 2984 | ExprResult Init; |
| 2985 | { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 2986 | VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv"); |
| 2987 | IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2988 | Expr *RHS = isOpenMPWorksharingDirective(DKind) |
| 2989 | ? LB.get() |
| 2990 | : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); |
| 2991 | Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); |
| 2992 | Init = SemaRef.ActOnFinishFullExpr(Init.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2995 | // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2996 | SourceLocation CondLoc; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2997 | ExprResult Cond = |
| 2998 | isOpenMPWorksharingDirective(DKind) |
| 2999 | ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) |
| 3000 | : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), |
| 3001 | NumIterations.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3002 | |
| 3003 | // Loop increment (IV = IV + 1) |
| 3004 | SourceLocation IncLoc; |
| 3005 | ExprResult Inc = |
| 3006 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), |
| 3007 | SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); |
| 3008 | if (!Inc.isUsable()) |
| 3009 | return 0; |
| 3010 | Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3011 | Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); |
| 3012 | if (!Inc.isUsable()) |
| 3013 | return 0; |
| 3014 | |
| 3015 | // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). |
| 3016 | // Used for directives with static scheduling. |
| 3017 | ExprResult NextLB, NextUB; |
| 3018 | if (isOpenMPWorksharingDirective(DKind)) { |
| 3019 | // LB + ST |
| 3020 | NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); |
| 3021 | if (!NextLB.isUsable()) |
| 3022 | return 0; |
| 3023 | // LB = LB + ST |
| 3024 | NextLB = |
| 3025 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); |
| 3026 | NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); |
| 3027 | if (!NextLB.isUsable()) |
| 3028 | return 0; |
| 3029 | // UB + ST |
| 3030 | NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); |
| 3031 | if (!NextUB.isUsable()) |
| 3032 | return 0; |
| 3033 | // UB = UB + ST |
| 3034 | NextUB = |
| 3035 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); |
| 3036 | NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); |
| 3037 | if (!NextUB.isUsable()) |
| 3038 | return 0; |
| 3039 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3040 | |
| 3041 | // Build updates and final values of the loop counters. |
| 3042 | bool HasErrors = false; |
| 3043 | Built.Counters.resize(NestedLoopCount); |
| 3044 | Built.Updates.resize(NestedLoopCount); |
| 3045 | Built.Finals.resize(NestedLoopCount); |
| 3046 | { |
| 3047 | ExprResult Div; |
| 3048 | // Go from inner nested loop to outer. |
| 3049 | for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { |
| 3050 | LoopIterationSpace &IS = IterSpaces[Cnt]; |
| 3051 | SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); |
| 3052 | // Build: Iter = (IV / Div) % IS.NumIters |
| 3053 | // where Div is product of previous iterations' IS.NumIters. |
| 3054 | ExprResult Iter; |
| 3055 | if (Div.isUsable()) { |
| 3056 | Iter = |
| 3057 | SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); |
| 3058 | } else { |
| 3059 | Iter = IV; |
| 3060 | assert((Cnt == (int)NestedLoopCount - 1) && |
| 3061 | "unusable div expected on first iteration only"); |
| 3062 | } |
| 3063 | |
| 3064 | if (Cnt != 0 && Iter.isUsable()) |
| 3065 | Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), |
| 3066 | IS.NumIterations); |
| 3067 | if (!Iter.isUsable()) { |
| 3068 | HasErrors = true; |
| 3069 | break; |
| 3070 | } |
| 3071 | |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 3072 | // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step |
| 3073 | auto *CounterVar = buildDeclRefExpr( |
| 3074 | SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()), |
| 3075 | IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), |
| 3076 | /*RefersToCapture=*/true); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3077 | ExprResult Update = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 3078 | BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3079 | IS.CounterInit, Iter, IS.CounterStep, IS.Subtract); |
| 3080 | if (!Update.isUsable()) { |
| 3081 | HasErrors = true; |
| 3082 | break; |
| 3083 | } |
| 3084 | |
| 3085 | // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step |
| 3086 | ExprResult Final = BuildCounterUpdate( |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 3087 | SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3088 | IS.NumIterations, IS.CounterStep, IS.Subtract); |
| 3089 | if (!Final.isUsable()) { |
| 3090 | HasErrors = true; |
| 3091 | break; |
| 3092 | } |
| 3093 | |
| 3094 | // Build Div for the next iteration: Div <- Div * IS.NumIters |
| 3095 | if (Cnt != 0) { |
| 3096 | if (Div.isUnset()) |
| 3097 | Div = IS.NumIterations; |
| 3098 | else |
| 3099 | Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), |
| 3100 | IS.NumIterations); |
| 3101 | |
| 3102 | // Add parentheses (for debugging purposes only). |
| 3103 | if (Div.isUsable()) |
| 3104 | Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get()); |
| 3105 | if (!Div.isUsable()) { |
| 3106 | HasErrors = true; |
| 3107 | break; |
| 3108 | } |
| 3109 | } |
| 3110 | if (!Update.isUsable() || !Final.isUsable()) { |
| 3111 | HasErrors = true; |
| 3112 | break; |
| 3113 | } |
| 3114 | // Save results |
| 3115 | Built.Counters[Cnt] = IS.CounterVar; |
| 3116 | Built.Updates[Cnt] = Update.get(); |
| 3117 | Built.Finals[Cnt] = Final.get(); |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | if (HasErrors) |
| 3122 | return 0; |
| 3123 | |
| 3124 | // Save results |
| 3125 | Built.IterationVarRef = IV.get(); |
| 3126 | Built.LastIteration = LastIteration.get(); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 3127 | Built.NumIterations = NumIterations.get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3128 | Built.CalcLastIteration = CalcLastIteration.get(); |
| 3129 | Built.PreCond = PreCond.get(); |
| 3130 | Built.Cond = Cond.get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3131 | Built.Init = Init.get(); |
| 3132 | Built.Inc = Inc.get(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3133 | Built.LB = LB.get(); |
| 3134 | Built.UB = UB.get(); |
| 3135 | Built.IL = IL.get(); |
| 3136 | Built.ST = ST.get(); |
| 3137 | Built.EUB = EUB.get(); |
| 3138 | Built.NLB = NextLB.get(); |
| 3139 | Built.NUB = NextUB.get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3140 | |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 3141 | return NestedLoopCount; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3144 | static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 3145 | auto &&CollapseFilter = [](const OMPClause *C) -> bool { |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 3146 | return C->getClauseKind() == OMPC_collapse; |
| 3147 | }; |
| 3148 | OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I( |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 3149 | Clauses, std::move(CollapseFilter)); |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3150 | if (I) |
| 3151 | return cast<OMPCollapseClause>(*I)->getNumForLoops(); |
| 3152 | return nullptr; |
| 3153 | } |
| 3154 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3155 | StmtResult Sema::ActOnOpenMPSimdDirective( |
| 3156 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 3157 | SourceLocation EndLoc, |
| 3158 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3159 | OMPLoopDirective::HelperExprs B; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3160 | // In presence of clause 'collapse', it will define the nested loops number. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3161 | unsigned NestedLoopCount = |
| 3162 | CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3163 | *DSAStack, VarsWithImplicitDSA, B); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 3164 | if (NestedLoopCount == 0) |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 3165 | return StmtError(); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 3166 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3167 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 3168 | "omp simd loop exprs were not built"); |
| 3169 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 3170 | if (!CurContext->isDependentContext()) { |
| 3171 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 3172 | for (auto C : Clauses) { |
| 3173 | if (auto LC = dyn_cast<OMPLinearClause>(C)) |
| 3174 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 3175 | B.NumIterations, *this, CurScope)) |
| 3176 | return StmtError(); |
| 3177 | } |
| 3178 | } |
| 3179 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 3180 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3181 | return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
| 3182 | Clauses, AStmt, B); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3185 | StmtResult Sema::ActOnOpenMPForDirective( |
| 3186 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 3187 | SourceLocation EndLoc, |
| 3188 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3189 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3190 | // In presence of clause 'collapse', it will define the nested loops number. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3191 | unsigned NestedLoopCount = |
| 3192 | CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3193 | *DSAStack, VarsWithImplicitDSA, B); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 3194 | if (NestedLoopCount == 0) |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3195 | return StmtError(); |
| 3196 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3197 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 3198 | "omp for loop exprs were not built"); |
| 3199 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3200 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3201 | return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
| 3202 | Clauses, AStmt, B); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 3205 | StmtResult Sema::ActOnOpenMPForSimdDirective( |
| 3206 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 3207 | SourceLocation EndLoc, |
| 3208 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3209 | OMPLoopDirective::HelperExprs B; |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 3210 | // In presence of clause 'collapse', it will define the nested loops number. |
| 3211 | unsigned NestedLoopCount = |
| 3212 | CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3213 | *this, *DSAStack, VarsWithImplicitDSA, B); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 3214 | if (NestedLoopCount == 0) |
| 3215 | return StmtError(); |
| 3216 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3217 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 3218 | "omp for simd loop exprs were not built"); |
| 3219 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 3220 | if (!CurContext->isDependentContext()) { |
| 3221 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 3222 | for (auto C : Clauses) { |
| 3223 | if (auto LC = dyn_cast<OMPLinearClause>(C)) |
| 3224 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 3225 | B.NumIterations, *this, CurScope)) |
| 3226 | return StmtError(); |
| 3227 | } |
| 3228 | } |
| 3229 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 3230 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3231 | return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
| 3232 | Clauses, AStmt, B); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 3235 | StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, |
| 3236 | Stmt *AStmt, |
| 3237 | SourceLocation StartLoc, |
| 3238 | SourceLocation EndLoc) { |
| 3239 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3240 | auto BaseStmt = AStmt; |
| 3241 | while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) |
| 3242 | BaseStmt = CS->getCapturedStmt(); |
| 3243 | if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { |
| 3244 | auto S = C->children(); |
| 3245 | if (!S) |
| 3246 | return StmtError(); |
| 3247 | // All associated statements must be '#pragma omp section' except for |
| 3248 | // the first one. |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 3249 | for (++S; S; ++S) { |
| 3250 | auto SectionStmt = *S; |
| 3251 | if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { |
| 3252 | if (SectionStmt) |
| 3253 | Diag(SectionStmt->getLocStart(), |
| 3254 | diag::err_omp_sections_substmt_not_section); |
| 3255 | return StmtError(); |
| 3256 | } |
| 3257 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 3258 | } else { |
| 3259 | Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); |
| 3260 | return StmtError(); |
| 3261 | } |
| 3262 | |
| 3263 | getCurFunction()->setHasBranchProtectedScope(); |
| 3264 | |
| 3265 | return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 3266 | AStmt); |
| 3267 | } |
| 3268 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 3269 | StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, |
| 3270 | SourceLocation StartLoc, |
| 3271 | SourceLocation EndLoc) { |
| 3272 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3273 | |
| 3274 | getCurFunction()->setHasBranchProtectedScope(); |
| 3275 | |
| 3276 | return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 3277 | } |
| 3278 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 3279 | StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, |
| 3280 | Stmt *AStmt, |
| 3281 | SourceLocation StartLoc, |
| 3282 | SourceLocation EndLoc) { |
Alexey Bataev | 74a05c9 | 2014-07-15 02:55:09 +0000 | [diff] [blame] | 3283 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3284 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 3285 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | 74a05c9 | 2014-07-15 02:55:09 +0000 | [diff] [blame] | 3286 | |
Alexey Bataev | 3255bf3 | 2015-01-19 05:20:46 +0000 | [diff] [blame] | 3287 | // OpenMP [2.7.3, single Construct, Restrictions] |
| 3288 | // The copyprivate clause must not be used with the nowait clause. |
| 3289 | OMPClause *Nowait = nullptr; |
| 3290 | OMPClause *Copyprivate = nullptr; |
| 3291 | for (auto *Clause : Clauses) { |
| 3292 | if (Clause->getClauseKind() == OMPC_nowait) |
| 3293 | Nowait = Clause; |
| 3294 | else if (Clause->getClauseKind() == OMPC_copyprivate) |
| 3295 | Copyprivate = Clause; |
| 3296 | if (Copyprivate && Nowait) { |
| 3297 | Diag(Copyprivate->getLocStart(), |
| 3298 | diag::err_omp_single_copyprivate_with_nowait); |
| 3299 | Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); |
| 3300 | return StmtError(); |
| 3301 | } |
| 3302 | } |
| 3303 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 3304 | return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 3305 | } |
| 3306 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 3307 | StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, |
| 3308 | SourceLocation StartLoc, |
| 3309 | SourceLocation EndLoc) { |
| 3310 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3311 | |
| 3312 | getCurFunction()->setHasBranchProtectedScope(); |
| 3313 | |
| 3314 | return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 3315 | } |
| 3316 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 3317 | StmtResult |
| 3318 | Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName, |
| 3319 | Stmt *AStmt, SourceLocation StartLoc, |
| 3320 | SourceLocation EndLoc) { |
| 3321 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3322 | |
| 3323 | getCurFunction()->setHasBranchProtectedScope(); |
| 3324 | |
| 3325 | return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, |
| 3326 | AStmt); |
| 3327 | } |
| 3328 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3329 | StmtResult Sema::ActOnOpenMPParallelForDirective( |
| 3330 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 3331 | SourceLocation EndLoc, |
| 3332 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
| 3333 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3334 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 3335 | // 1.2.2 OpenMP Language Terminology |
| 3336 | // Structured block - An executable statement with a single entry at the |
| 3337 | // top and a single exit at the bottom. |
| 3338 | // The point of exit cannot be a branch out of the structured block. |
| 3339 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 3340 | CS->getCapturedDecl()->setNothrow(); |
| 3341 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3342 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3343 | // In presence of clause 'collapse', it will define the nested loops number. |
| 3344 | unsigned NestedLoopCount = |
| 3345 | CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt, |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3346 | *this, *DSAStack, VarsWithImplicitDSA, B); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3347 | if (NestedLoopCount == 0) |
| 3348 | return StmtError(); |
| 3349 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3350 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 3351 | "omp parallel for loop exprs were not built"); |
| 3352 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3353 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3354 | return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, |
| 3355 | NestedLoopCount, Clauses, AStmt, B); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 3358 | StmtResult Sema::ActOnOpenMPParallelForSimdDirective( |
| 3359 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 3360 | SourceLocation EndLoc, |
| 3361 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
| 3362 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3363 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 3364 | // 1.2.2 OpenMP Language Terminology |
| 3365 | // Structured block - An executable statement with a single entry at the |
| 3366 | // top and a single exit at the bottom. |
| 3367 | // The point of exit cannot be a branch out of the structured block. |
| 3368 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 3369 | CS->getCapturedDecl()->setNothrow(); |
| 3370 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3371 | OMPLoopDirective::HelperExprs B; |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 3372 | // In presence of clause 'collapse', it will define the nested loops number. |
| 3373 | unsigned NestedLoopCount = |
| 3374 | CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses), |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3375 | AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 3376 | if (NestedLoopCount == 0) |
| 3377 | return StmtError(); |
| 3378 | |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 3379 | if (!CurContext->isDependentContext()) { |
| 3380 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 3381 | for (auto C : Clauses) { |
| 3382 | if (auto LC = dyn_cast<OMPLinearClause>(C)) |
| 3383 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 3384 | B.NumIterations, *this, CurScope)) |
| 3385 | return StmtError(); |
| 3386 | } |
| 3387 | } |
| 3388 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 3389 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3390 | return OMPParallelForSimdDirective::Create( |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3391 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 3394 | StmtResult |
| 3395 | Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, |
| 3396 | Stmt *AStmt, SourceLocation StartLoc, |
| 3397 | SourceLocation EndLoc) { |
| 3398 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3399 | auto BaseStmt = AStmt; |
| 3400 | while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) |
| 3401 | BaseStmt = CS->getCapturedStmt(); |
| 3402 | if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { |
| 3403 | auto S = C->children(); |
| 3404 | if (!S) |
| 3405 | return StmtError(); |
| 3406 | // All associated statements must be '#pragma omp section' except for |
| 3407 | // the first one. |
| 3408 | for (++S; S; ++S) { |
| 3409 | auto SectionStmt = *S; |
| 3410 | if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { |
| 3411 | if (SectionStmt) |
| 3412 | Diag(SectionStmt->getLocStart(), |
| 3413 | diag::err_omp_parallel_sections_substmt_not_section); |
| 3414 | return StmtError(); |
| 3415 | } |
| 3416 | } |
| 3417 | } else { |
| 3418 | Diag(AStmt->getLocStart(), |
| 3419 | diag::err_omp_parallel_sections_not_compound_stmt); |
| 3420 | return StmtError(); |
| 3421 | } |
| 3422 | |
| 3423 | getCurFunction()->setHasBranchProtectedScope(); |
| 3424 | |
| 3425 | return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc, |
| 3426 | Clauses, AStmt); |
| 3427 | } |
| 3428 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3429 | StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, |
| 3430 | Stmt *AStmt, SourceLocation StartLoc, |
| 3431 | SourceLocation EndLoc) { |
| 3432 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3433 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 3434 | // 1.2.2 OpenMP Language Terminology |
| 3435 | // Structured block - An executable statement with a single entry at the |
| 3436 | // top and a single exit at the bottom. |
| 3437 | // The point of exit cannot be a branch out of the structured block. |
| 3438 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 3439 | CS->getCapturedDecl()->setNothrow(); |
| 3440 | |
| 3441 | getCurFunction()->setHasBranchProtectedScope(); |
| 3442 | |
| 3443 | return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 3444 | } |
| 3445 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 3446 | StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, |
| 3447 | SourceLocation EndLoc) { |
| 3448 | return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); |
| 3449 | } |
| 3450 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 3451 | StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, |
| 3452 | SourceLocation EndLoc) { |
| 3453 | return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); |
| 3454 | } |
| 3455 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 3456 | StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, |
| 3457 | SourceLocation EndLoc) { |
| 3458 | return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); |
| 3459 | } |
| 3460 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3461 | StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt, |
| 3462 | SourceLocation StartLoc, |
| 3463 | SourceLocation EndLoc) { |
| 3464 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3465 | |
| 3466 | getCurFunction()->setHasBranchProtectedScope(); |
| 3467 | |
| 3468 | return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 3469 | } |
| 3470 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 3471 | StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, |
| 3472 | SourceLocation StartLoc, |
| 3473 | SourceLocation EndLoc) { |
| 3474 | assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); |
| 3475 | return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); |
| 3476 | } |
| 3477 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 3478 | StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt, |
| 3479 | SourceLocation StartLoc, |
| 3480 | SourceLocation EndLoc) { |
| 3481 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 3482 | |
| 3483 | getCurFunction()->setHasBranchProtectedScope(); |
| 3484 | |
| 3485 | return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 3486 | } |
| 3487 | |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3488 | namespace { |
| 3489 | /// \brief Helper class for checking expression in 'omp atomic [update]' |
| 3490 | /// construct. |
| 3491 | class OpenMPAtomicUpdateChecker { |
| 3492 | /// \brief Error results for atomic update expressions. |
| 3493 | enum ExprAnalysisErrorCode { |
| 3494 | /// \brief A statement is not an expression statement. |
| 3495 | NotAnExpression, |
| 3496 | /// \brief Expression is not builtin binary or unary operation. |
| 3497 | NotABinaryOrUnaryExpression, |
| 3498 | /// \brief Unary operation is not post-/pre- increment/decrement operation. |
| 3499 | NotAnUnaryIncDecExpression, |
| 3500 | /// \brief An expression is not of scalar type. |
| 3501 | NotAScalarType, |
| 3502 | /// \brief A binary operation is not an assignment operation. |
| 3503 | NotAnAssignmentOp, |
| 3504 | /// \brief RHS part of the binary operation is not a binary expression. |
| 3505 | NotABinaryExpression, |
| 3506 | /// \brief RHS part is not additive/multiplicative/shift/biwise binary |
| 3507 | /// expression. |
| 3508 | NotABinaryOperator, |
| 3509 | /// \brief RHS binary operation does not have reference to the updated LHS |
| 3510 | /// part. |
| 3511 | NotAnUpdateExpression, |
| 3512 | /// \brief No errors is found. |
| 3513 | NoError |
| 3514 | }; |
| 3515 | /// \brief Reference to Sema. |
| 3516 | Sema &SemaRef; |
| 3517 | /// \brief A location for note diagnostics (when error is found). |
| 3518 | SourceLocation NoteLoc; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3519 | /// \brief 'x' lvalue part of the source atomic expression. |
| 3520 | Expr *X; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3521 | /// \brief 'expr' rvalue part of the source atomic expression. |
| 3522 | Expr *E; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3523 | /// \brief Helper expression of the form |
| 3524 | /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or |
| 3525 | /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. |
| 3526 | Expr *UpdateExpr; |
| 3527 | /// \brief Is 'x' a LHS in a RHS part of full update expression. It is |
| 3528 | /// important for non-associative operations. |
| 3529 | bool IsXLHSInRHSPart; |
| 3530 | BinaryOperatorKind Op; |
| 3531 | SourceLocation OpLoc; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3532 | /// \brief true if the source expression is a postfix unary operation, false |
| 3533 | /// if it is a prefix unary operation. |
| 3534 | bool IsPostfixUpdate; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3535 | |
| 3536 | public: |
| 3537 | OpenMPAtomicUpdateChecker(Sema &SemaRef) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3538 | : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3539 | IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3540 | /// \brief Check specified statement that it is suitable for 'atomic update' |
| 3541 | /// constructs and extract 'x', 'expr' and Operation from the original |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3542 | /// expression. If DiagId and NoteId == 0, then only check is performed |
| 3543 | /// without error notification. |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3544 | /// \param DiagId Diagnostic which should be emitted if error is found. |
| 3545 | /// \param NoteId Diagnostic note for the main error message. |
| 3546 | /// \return true if statement is not an update expression, false otherwise. |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3547 | bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3548 | /// \brief Return the 'x' lvalue part of the source atomic expression. |
| 3549 | Expr *getX() const { return X; } |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3550 | /// \brief Return the 'expr' rvalue part of the source atomic expression. |
| 3551 | Expr *getExpr() const { return E; } |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3552 | /// \brief Return the update expression used in calculation of the updated |
| 3553 | /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or |
| 3554 | /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. |
| 3555 | Expr *getUpdateExpr() const { return UpdateExpr; } |
| 3556 | /// \brief Return true if 'x' is LHS in RHS part of full update expression, |
| 3557 | /// false otherwise. |
| 3558 | bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } |
| 3559 | |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3560 | /// \brief true if the source expression is a postfix unary operation, false |
| 3561 | /// if it is a prefix unary operation. |
| 3562 | bool isPostfixUpdate() const { return IsPostfixUpdate; } |
| 3563 | |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3564 | private: |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3565 | bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, |
| 3566 | unsigned NoteId = 0); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3567 | }; |
| 3568 | } // namespace |
| 3569 | |
| 3570 | bool OpenMPAtomicUpdateChecker::checkBinaryOperation( |
| 3571 | BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { |
| 3572 | ExprAnalysisErrorCode ErrorFound = NoError; |
| 3573 | SourceLocation ErrorLoc, NoteLoc; |
| 3574 | SourceRange ErrorRange, NoteRange; |
| 3575 | // Allowed constructs are: |
| 3576 | // x = x binop expr; |
| 3577 | // x = expr binop x; |
| 3578 | if (AtomicBinOp->getOpcode() == BO_Assign) { |
| 3579 | X = AtomicBinOp->getLHS(); |
| 3580 | if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( |
| 3581 | AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { |
| 3582 | if (AtomicInnerBinOp->isMultiplicativeOp() || |
| 3583 | AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || |
| 3584 | AtomicInnerBinOp->isBitwiseOp()) { |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3585 | Op = AtomicInnerBinOp->getOpcode(); |
| 3586 | OpLoc = AtomicInnerBinOp->getOperatorLoc(); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3587 | auto *LHS = AtomicInnerBinOp->getLHS(); |
| 3588 | auto *RHS = AtomicInnerBinOp->getRHS(); |
| 3589 | llvm::FoldingSetNodeID XId, LHSId, RHSId; |
| 3590 | X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), |
| 3591 | /*Canonical=*/true); |
| 3592 | LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), |
| 3593 | /*Canonical=*/true); |
| 3594 | RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), |
| 3595 | /*Canonical=*/true); |
| 3596 | if (XId == LHSId) { |
| 3597 | E = RHS; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3598 | IsXLHSInRHSPart = true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3599 | } else if (XId == RHSId) { |
| 3600 | E = LHS; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3601 | IsXLHSInRHSPart = false; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3602 | } else { |
| 3603 | ErrorLoc = AtomicInnerBinOp->getExprLoc(); |
| 3604 | ErrorRange = AtomicInnerBinOp->getSourceRange(); |
| 3605 | NoteLoc = X->getExprLoc(); |
| 3606 | NoteRange = X->getSourceRange(); |
| 3607 | ErrorFound = NotAnUpdateExpression; |
| 3608 | } |
| 3609 | } else { |
| 3610 | ErrorLoc = AtomicInnerBinOp->getExprLoc(); |
| 3611 | ErrorRange = AtomicInnerBinOp->getSourceRange(); |
| 3612 | NoteLoc = AtomicInnerBinOp->getOperatorLoc(); |
| 3613 | NoteRange = SourceRange(NoteLoc, NoteLoc); |
| 3614 | ErrorFound = NotABinaryOperator; |
| 3615 | } |
| 3616 | } else { |
| 3617 | NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); |
| 3618 | NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); |
| 3619 | ErrorFound = NotABinaryExpression; |
| 3620 | } |
| 3621 | } else { |
| 3622 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 3623 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 3624 | NoteLoc = AtomicBinOp->getOperatorLoc(); |
| 3625 | NoteRange = SourceRange(NoteLoc, NoteLoc); |
| 3626 | ErrorFound = NotAnAssignmentOp; |
| 3627 | } |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3628 | if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3629 | SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; |
| 3630 | SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; |
| 3631 | return true; |
| 3632 | } else if (SemaRef.CurContext->isDependentContext()) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3633 | E = X = UpdateExpr = nullptr; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3634 | return ErrorFound != NoError; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, |
| 3638 | unsigned NoteId) { |
| 3639 | ExprAnalysisErrorCode ErrorFound = NoError; |
| 3640 | SourceLocation ErrorLoc, NoteLoc; |
| 3641 | SourceRange ErrorRange, NoteRange; |
| 3642 | // Allowed constructs are: |
| 3643 | // x++; |
| 3644 | // x--; |
| 3645 | // ++x; |
| 3646 | // --x; |
| 3647 | // x binop= expr; |
| 3648 | // x = x binop expr; |
| 3649 | // x = expr binop x; |
| 3650 | if (auto *AtomicBody = dyn_cast<Expr>(S)) { |
| 3651 | AtomicBody = AtomicBody->IgnoreParenImpCasts(); |
| 3652 | if (AtomicBody->getType()->isScalarType() || |
| 3653 | AtomicBody->isInstantiationDependent()) { |
| 3654 | if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( |
| 3655 | AtomicBody->IgnoreParenImpCasts())) { |
| 3656 | // Check for Compound Assignment Operation |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3657 | Op = BinaryOperator::getOpForCompoundAssignment( |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3658 | AtomicCompAssignOp->getOpcode()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3659 | OpLoc = AtomicCompAssignOp->getOperatorLoc(); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3660 | E = AtomicCompAssignOp->getRHS(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3661 | X = AtomicCompAssignOp->getLHS(); |
| 3662 | IsXLHSInRHSPart = true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3663 | } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( |
| 3664 | AtomicBody->IgnoreParenImpCasts())) { |
| 3665 | // Check for Binary Operation |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3666 | if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) |
| 3667 | return true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3668 | } else if (auto *AtomicUnaryOp = |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3669 | dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) { |
| 3670 | // Check for Unary Operation |
| 3671 | if (AtomicUnaryOp->isIncrementDecrementOp()) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3672 | IsPostfixUpdate = AtomicUnaryOp->isPostfix(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3673 | Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; |
| 3674 | OpLoc = AtomicUnaryOp->getOperatorLoc(); |
| 3675 | X = AtomicUnaryOp->getSubExpr(); |
| 3676 | E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); |
| 3677 | IsXLHSInRHSPart = true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3678 | } else { |
| 3679 | ErrorFound = NotAnUnaryIncDecExpression; |
| 3680 | ErrorLoc = AtomicUnaryOp->getExprLoc(); |
| 3681 | ErrorRange = AtomicUnaryOp->getSourceRange(); |
| 3682 | NoteLoc = AtomicUnaryOp->getOperatorLoc(); |
| 3683 | NoteRange = SourceRange(NoteLoc, NoteLoc); |
| 3684 | } |
| 3685 | } else { |
| 3686 | ErrorFound = NotABinaryOrUnaryExpression; |
| 3687 | NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); |
| 3688 | NoteRange = ErrorRange = AtomicBody->getSourceRange(); |
| 3689 | } |
| 3690 | } else { |
| 3691 | ErrorFound = NotAScalarType; |
| 3692 | NoteLoc = ErrorLoc = AtomicBody->getLocStart(); |
| 3693 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
| 3694 | } |
| 3695 | } else { |
| 3696 | ErrorFound = NotAnExpression; |
| 3697 | NoteLoc = ErrorLoc = S->getLocStart(); |
| 3698 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
| 3699 | } |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3700 | if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3701 | SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; |
| 3702 | SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; |
| 3703 | return true; |
| 3704 | } else if (SemaRef.CurContext->isDependentContext()) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3705 | E = X = UpdateExpr = nullptr; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3706 | if (ErrorFound == NoError && E && X) { |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3707 | // Build an update expression of form 'OpaqueValueExpr(x) binop |
| 3708 | // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop |
| 3709 | // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. |
| 3710 | auto *OVEX = new (SemaRef.getASTContext()) |
| 3711 | OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); |
| 3712 | auto *OVEExpr = new (SemaRef.getASTContext()) |
| 3713 | OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); |
| 3714 | auto Update = |
| 3715 | SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, |
| 3716 | IsXLHSInRHSPart ? OVEExpr : OVEX); |
| 3717 | if (Update.isInvalid()) |
| 3718 | return true; |
| 3719 | Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), |
| 3720 | Sema::AA_Casting); |
| 3721 | if (Update.isInvalid()) |
| 3722 | return true; |
| 3723 | UpdateExpr = Update.get(); |
| 3724 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3725 | return ErrorFound != NoError; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 3728 | StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, |
| 3729 | Stmt *AStmt, |
| 3730 | SourceLocation StartLoc, |
| 3731 | SourceLocation EndLoc) { |
| 3732 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 3733 | auto CS = cast<CapturedStmt>(AStmt); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 3734 | // 1.2.2 OpenMP Language Terminology |
| 3735 | // Structured block - An executable statement with a single entry at the |
| 3736 | // top and a single exit at the bottom. |
| 3737 | // The point of exit cannot be a branch out of the structured block. |
| 3738 | // longjmp() and throw() must not violate the entry/exit criteria. |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 3739 | OpenMPClauseKind AtomicKind = OMPC_unknown; |
| 3740 | SourceLocation AtomicKindLoc; |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 3741 | for (auto *C : Clauses) { |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 3742 | if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 3743 | C->getClauseKind() == OMPC_update || |
| 3744 | C->getClauseKind() == OMPC_capture) { |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 3745 | if (AtomicKind != OMPC_unknown) { |
| 3746 | Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) |
| 3747 | << SourceRange(C->getLocStart(), C->getLocEnd()); |
| 3748 | Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) |
| 3749 | << getOpenMPClauseName(AtomicKind); |
| 3750 | } else { |
| 3751 | AtomicKind = C->getClauseKind(); |
| 3752 | AtomicKindLoc = C->getLocStart(); |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 3753 | } |
| 3754 | } |
| 3755 | } |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 3756 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 3757 | auto Body = CS->getCapturedStmt(); |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3758 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) |
| 3759 | Body = EWC->getSubExpr(); |
| 3760 | |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 3761 | Expr *X = nullptr; |
| 3762 | Expr *V = nullptr; |
| 3763 | Expr *E = nullptr; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3764 | Expr *UE = nullptr; |
| 3765 | bool IsXLHSInRHSPart = false; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3766 | bool IsPostfixUpdate = false; |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 3767 | // OpenMP [2.12.6, atomic Construct] |
| 3768 | // In the next expressions: |
| 3769 | // * x and v (as applicable) are both l-value expressions with scalar type. |
| 3770 | // * During the execution of an atomic region, multiple syntactic |
| 3771 | // occurrences of x must designate the same storage location. |
| 3772 | // * Neither of v and expr (as applicable) may access the storage location |
| 3773 | // designated by x. |
| 3774 | // * Neither of x and expr (as applicable) may access the storage location |
| 3775 | // designated by v. |
| 3776 | // * expr is an expression with scalar type. |
| 3777 | // * binop is one of +, *, -, /, &, ^, |, <<, or >>. |
| 3778 | // * binop, binop=, ++, and -- are not overloaded operators. |
| 3779 | // * The expression x binop expr must be numerically equivalent to x binop |
| 3780 | // (expr). This requirement is satisfied if the operators in expr have |
| 3781 | // precedence greater than binop, or by using parentheses around expr or |
| 3782 | // subexpressions of expr. |
| 3783 | // * The expression expr binop x must be numerically equivalent to (expr) |
| 3784 | // binop x. This requirement is satisfied if the operators in expr have |
| 3785 | // precedence equal to or greater than binop, or by using parentheses around |
| 3786 | // expr or subexpressions of expr. |
| 3787 | // * For forms that allow multiple occurrences of x, the number of times |
| 3788 | // that x is evaluated is unspecified. |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 3789 | if (AtomicKind == OMPC_read) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3790 | enum { |
| 3791 | NotAnExpression, |
| 3792 | NotAnAssignmentOp, |
| 3793 | NotAScalarType, |
| 3794 | NotAnLValue, |
| 3795 | NoError |
| 3796 | } ErrorFound = NoError; |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 3797 | SourceLocation ErrorLoc, NoteLoc; |
| 3798 | SourceRange ErrorRange, NoteRange; |
| 3799 | // If clause is read: |
| 3800 | // v = x; |
| 3801 | if (auto AtomicBody = dyn_cast<Expr>(Body)) { |
| 3802 | auto AtomicBinOp = |
| 3803 | dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); |
| 3804 | if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { |
| 3805 | X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); |
| 3806 | V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); |
| 3807 | if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && |
| 3808 | (V->isInstantiationDependent() || V->getType()->isScalarType())) { |
| 3809 | if (!X->isLValue() || !V->isLValue()) { |
| 3810 | auto NotLValueExpr = X->isLValue() ? V : X; |
| 3811 | ErrorFound = NotAnLValue; |
| 3812 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 3813 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 3814 | NoteLoc = NotLValueExpr->getExprLoc(); |
| 3815 | NoteRange = NotLValueExpr->getSourceRange(); |
| 3816 | } |
| 3817 | } else if (!X->isInstantiationDependent() || |
| 3818 | !V->isInstantiationDependent()) { |
| 3819 | auto NotScalarExpr = |
| 3820 | (X->isInstantiationDependent() || X->getType()->isScalarType()) |
| 3821 | ? V |
| 3822 | : X; |
| 3823 | ErrorFound = NotAScalarType; |
| 3824 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 3825 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 3826 | NoteLoc = NotScalarExpr->getExprLoc(); |
| 3827 | NoteRange = NotScalarExpr->getSourceRange(); |
| 3828 | } |
| 3829 | } else { |
| 3830 | ErrorFound = NotAnAssignmentOp; |
| 3831 | ErrorLoc = AtomicBody->getExprLoc(); |
| 3832 | ErrorRange = AtomicBody->getSourceRange(); |
| 3833 | NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() |
| 3834 | : AtomicBody->getExprLoc(); |
| 3835 | NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() |
| 3836 | : AtomicBody->getSourceRange(); |
| 3837 | } |
| 3838 | } else { |
| 3839 | ErrorFound = NotAnExpression; |
| 3840 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 3841 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 3842 | } |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 3843 | if (ErrorFound != NoError) { |
| 3844 | Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) |
| 3845 | << ErrorRange; |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 3846 | Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound |
| 3847 | << NoteRange; |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 3848 | return StmtError(); |
| 3849 | } else if (CurContext->isDependentContext()) |
| 3850 | V = X = nullptr; |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 3851 | } else if (AtomicKind == OMPC_write) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3852 | enum { |
| 3853 | NotAnExpression, |
| 3854 | NotAnAssignmentOp, |
| 3855 | NotAScalarType, |
| 3856 | NotAnLValue, |
| 3857 | NoError |
| 3858 | } ErrorFound = NoError; |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 3859 | SourceLocation ErrorLoc, NoteLoc; |
| 3860 | SourceRange ErrorRange, NoteRange; |
| 3861 | // If clause is write: |
| 3862 | // x = expr; |
| 3863 | if (auto AtomicBody = dyn_cast<Expr>(Body)) { |
| 3864 | auto AtomicBinOp = |
| 3865 | dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); |
| 3866 | if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3867 | X = AtomicBinOp->getLHS(); |
| 3868 | E = AtomicBinOp->getRHS(); |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 3869 | if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && |
| 3870 | (E->isInstantiationDependent() || E->getType()->isScalarType())) { |
| 3871 | if (!X->isLValue()) { |
| 3872 | ErrorFound = NotAnLValue; |
| 3873 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 3874 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 3875 | NoteLoc = X->getExprLoc(); |
| 3876 | NoteRange = X->getSourceRange(); |
| 3877 | } |
| 3878 | } else if (!X->isInstantiationDependent() || |
| 3879 | !E->isInstantiationDependent()) { |
| 3880 | auto NotScalarExpr = |
| 3881 | (X->isInstantiationDependent() || X->getType()->isScalarType()) |
| 3882 | ? E |
| 3883 | : X; |
| 3884 | ErrorFound = NotAScalarType; |
| 3885 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 3886 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 3887 | NoteLoc = NotScalarExpr->getExprLoc(); |
| 3888 | NoteRange = NotScalarExpr->getSourceRange(); |
| 3889 | } |
| 3890 | } else { |
| 3891 | ErrorFound = NotAnAssignmentOp; |
| 3892 | ErrorLoc = AtomicBody->getExprLoc(); |
| 3893 | ErrorRange = AtomicBody->getSourceRange(); |
| 3894 | NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() |
| 3895 | : AtomicBody->getExprLoc(); |
| 3896 | NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() |
| 3897 | : AtomicBody->getSourceRange(); |
| 3898 | } |
| 3899 | } else { |
| 3900 | ErrorFound = NotAnExpression; |
| 3901 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 3902 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 3903 | } |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 3904 | if (ErrorFound != NoError) { |
| 3905 | Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) |
| 3906 | << ErrorRange; |
| 3907 | Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound |
| 3908 | << NoteRange; |
| 3909 | return StmtError(); |
| 3910 | } else if (CurContext->isDependentContext()) |
| 3911 | E = X = nullptr; |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 3912 | } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3913 | // If clause is update: |
| 3914 | // x++; |
| 3915 | // x--; |
| 3916 | // ++x; |
| 3917 | // --x; |
| 3918 | // x binop= expr; |
| 3919 | // x = x binop expr; |
| 3920 | // x = expr binop x; |
| 3921 | OpenMPAtomicUpdateChecker Checker(*this); |
| 3922 | if (Checker.checkStatement( |
| 3923 | Body, (AtomicKind == OMPC_update) |
| 3924 | ? diag::err_omp_atomic_update_not_expression_statement |
| 3925 | : diag::err_omp_atomic_not_expression_statement, |
| 3926 | diag::note_omp_atomic_update)) |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 3927 | return StmtError(); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 3928 | if (!CurContext->isDependentContext()) { |
| 3929 | E = Checker.getExpr(); |
| 3930 | X = Checker.getX(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3931 | UE = Checker.getUpdateExpr(); |
| 3932 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 3933 | } |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 3934 | } else if (AtomicKind == OMPC_capture) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 3935 | enum { |
| 3936 | NotAnAssignmentOp, |
| 3937 | NotACompoundStatement, |
| 3938 | NotTwoSubstatements, |
| 3939 | NotASpecificExpression, |
| 3940 | NoError |
| 3941 | } ErrorFound = NoError; |
| 3942 | SourceLocation ErrorLoc, NoteLoc; |
| 3943 | SourceRange ErrorRange, NoteRange; |
| 3944 | if (auto *AtomicBody = dyn_cast<Expr>(Body)) { |
| 3945 | // If clause is a capture: |
| 3946 | // v = x++; |
| 3947 | // v = x--; |
| 3948 | // v = ++x; |
| 3949 | // v = --x; |
| 3950 | // v = x binop= expr; |
| 3951 | // v = x = x binop expr; |
| 3952 | // v = x = expr binop x; |
| 3953 | auto *AtomicBinOp = |
| 3954 | dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); |
| 3955 | if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { |
| 3956 | V = AtomicBinOp->getLHS(); |
| 3957 | Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); |
| 3958 | OpenMPAtomicUpdateChecker Checker(*this); |
| 3959 | if (Checker.checkStatement( |
| 3960 | Body, diag::err_omp_atomic_capture_not_expression_statement, |
| 3961 | diag::note_omp_atomic_update)) |
| 3962 | return StmtError(); |
| 3963 | E = Checker.getExpr(); |
| 3964 | X = Checker.getX(); |
| 3965 | UE = Checker.getUpdateExpr(); |
| 3966 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
| 3967 | IsPostfixUpdate = Checker.isPostfixUpdate(); |
| 3968 | } else { |
| 3969 | ErrorLoc = AtomicBody->getExprLoc(); |
| 3970 | ErrorRange = AtomicBody->getSourceRange(); |
| 3971 | NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() |
| 3972 | : AtomicBody->getExprLoc(); |
| 3973 | NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() |
| 3974 | : AtomicBody->getSourceRange(); |
| 3975 | ErrorFound = NotAnAssignmentOp; |
| 3976 | } |
| 3977 | if (ErrorFound != NoError) { |
| 3978 | Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) |
| 3979 | << ErrorRange; |
| 3980 | Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; |
| 3981 | return StmtError(); |
| 3982 | } else if (CurContext->isDependentContext()) { |
| 3983 | UE = V = E = X = nullptr; |
| 3984 | } |
| 3985 | } else { |
| 3986 | // If clause is a capture: |
| 3987 | // { v = x; x = expr; } |
| 3988 | // { v = x; x++; } |
| 3989 | // { v = x; x--; } |
| 3990 | // { v = x; ++x; } |
| 3991 | // { v = x; --x; } |
| 3992 | // { v = x; x binop= expr; } |
| 3993 | // { v = x; x = x binop expr; } |
| 3994 | // { v = x; x = expr binop x; } |
| 3995 | // { x++; v = x; } |
| 3996 | // { x--; v = x; } |
| 3997 | // { ++x; v = x; } |
| 3998 | // { --x; v = x; } |
| 3999 | // { x binop= expr; v = x; } |
| 4000 | // { x = x binop expr; v = x; } |
| 4001 | // { x = expr binop x; v = x; } |
| 4002 | if (auto *CS = dyn_cast<CompoundStmt>(Body)) { |
| 4003 | // Check that this is { expr1; expr2; } |
| 4004 | if (CS->size() == 2) { |
| 4005 | auto *First = CS->body_front(); |
| 4006 | auto *Second = CS->body_back(); |
| 4007 | if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) |
| 4008 | First = EWC->getSubExpr()->IgnoreParenImpCasts(); |
| 4009 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) |
| 4010 | Second = EWC->getSubExpr()->IgnoreParenImpCasts(); |
| 4011 | // Need to find what subexpression is 'v' and what is 'x'. |
| 4012 | OpenMPAtomicUpdateChecker Checker(*this); |
| 4013 | bool IsUpdateExprFound = !Checker.checkStatement(Second); |
| 4014 | BinaryOperator *BinOp = nullptr; |
| 4015 | if (IsUpdateExprFound) { |
| 4016 | BinOp = dyn_cast<BinaryOperator>(First); |
| 4017 | IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; |
| 4018 | } |
| 4019 | if (IsUpdateExprFound && !CurContext->isDependentContext()) { |
| 4020 | // { v = x; x++; } |
| 4021 | // { v = x; x--; } |
| 4022 | // { v = x; ++x; } |
| 4023 | // { v = x; --x; } |
| 4024 | // { v = x; x binop= expr; } |
| 4025 | // { v = x; x = x binop expr; } |
| 4026 | // { v = x; x = expr binop x; } |
| 4027 | // Check that the first expression has form v = x. |
| 4028 | auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); |
| 4029 | llvm::FoldingSetNodeID XId, PossibleXId; |
| 4030 | Checker.getX()->Profile(XId, Context, /*Canonical=*/true); |
| 4031 | PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); |
| 4032 | IsUpdateExprFound = XId == PossibleXId; |
| 4033 | if (IsUpdateExprFound) { |
| 4034 | V = BinOp->getLHS(); |
| 4035 | X = Checker.getX(); |
| 4036 | E = Checker.getExpr(); |
| 4037 | UE = Checker.getUpdateExpr(); |
| 4038 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4039 | IsPostfixUpdate = true; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4040 | } |
| 4041 | } |
| 4042 | if (!IsUpdateExprFound) { |
| 4043 | IsUpdateExprFound = !Checker.checkStatement(First); |
| 4044 | BinOp = nullptr; |
| 4045 | if (IsUpdateExprFound) { |
| 4046 | BinOp = dyn_cast<BinaryOperator>(Second); |
| 4047 | IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; |
| 4048 | } |
| 4049 | if (IsUpdateExprFound && !CurContext->isDependentContext()) { |
| 4050 | // { x++; v = x; } |
| 4051 | // { x--; v = x; } |
| 4052 | // { ++x; v = x; } |
| 4053 | // { --x; v = x; } |
| 4054 | // { x binop= expr; v = x; } |
| 4055 | // { x = x binop expr; v = x; } |
| 4056 | // { x = expr binop x; v = x; } |
| 4057 | // Check that the second expression has form v = x. |
| 4058 | auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); |
| 4059 | llvm::FoldingSetNodeID XId, PossibleXId; |
| 4060 | Checker.getX()->Profile(XId, Context, /*Canonical=*/true); |
| 4061 | PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); |
| 4062 | IsUpdateExprFound = XId == PossibleXId; |
| 4063 | if (IsUpdateExprFound) { |
| 4064 | V = BinOp->getLHS(); |
| 4065 | X = Checker.getX(); |
| 4066 | E = Checker.getExpr(); |
| 4067 | UE = Checker.getUpdateExpr(); |
| 4068 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4069 | IsPostfixUpdate = false; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4070 | } |
| 4071 | } |
| 4072 | } |
| 4073 | if (!IsUpdateExprFound) { |
| 4074 | // { v = x; x = expr; } |
| 4075 | auto *FirstBinOp = dyn_cast<BinaryOperator>(First); |
| 4076 | if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { |
| 4077 | ErrorFound = NotAnAssignmentOp; |
| 4078 | NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() |
| 4079 | : First->getLocStart(); |
| 4080 | NoteRange = ErrorRange = FirstBinOp |
| 4081 | ? FirstBinOp->getSourceRange() |
| 4082 | : SourceRange(ErrorLoc, ErrorLoc); |
| 4083 | } else { |
| 4084 | auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); |
| 4085 | if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { |
| 4086 | ErrorFound = NotAnAssignmentOp; |
| 4087 | NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc() |
| 4088 | : Second->getLocStart(); |
| 4089 | NoteRange = ErrorRange = SecondBinOp |
| 4090 | ? SecondBinOp->getSourceRange() |
| 4091 | : SourceRange(ErrorLoc, ErrorLoc); |
| 4092 | } else { |
| 4093 | auto *PossibleXRHSInFirst = |
| 4094 | FirstBinOp->getRHS()->IgnoreParenImpCasts(); |
| 4095 | auto *PossibleXLHSInSecond = |
| 4096 | SecondBinOp->getLHS()->IgnoreParenImpCasts(); |
| 4097 | llvm::FoldingSetNodeID X1Id, X2Id; |
| 4098 | PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true); |
| 4099 | PossibleXLHSInSecond->Profile(X2Id, Context, |
| 4100 | /*Canonical=*/true); |
| 4101 | IsUpdateExprFound = X1Id == X2Id; |
| 4102 | if (IsUpdateExprFound) { |
| 4103 | V = FirstBinOp->getLHS(); |
| 4104 | X = SecondBinOp->getLHS(); |
| 4105 | E = SecondBinOp->getRHS(); |
| 4106 | UE = nullptr; |
| 4107 | IsXLHSInRHSPart = false; |
| 4108 | IsPostfixUpdate = true; |
| 4109 | } else { |
| 4110 | ErrorFound = NotASpecificExpression; |
| 4111 | ErrorLoc = FirstBinOp->getExprLoc(); |
| 4112 | ErrorRange = FirstBinOp->getSourceRange(); |
| 4113 | NoteLoc = SecondBinOp->getLHS()->getExprLoc(); |
| 4114 | NoteRange = SecondBinOp->getRHS()->getSourceRange(); |
| 4115 | } |
| 4116 | } |
| 4117 | } |
| 4118 | } |
| 4119 | } else { |
| 4120 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 4121 | NoteRange = ErrorRange = |
| 4122 | SourceRange(Body->getLocStart(), Body->getLocStart()); |
| 4123 | ErrorFound = NotTwoSubstatements; |
| 4124 | } |
| 4125 | } else { |
| 4126 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 4127 | NoteRange = ErrorRange = |
| 4128 | SourceRange(Body->getLocStart(), Body->getLocStart()); |
| 4129 | ErrorFound = NotACompoundStatement; |
| 4130 | } |
| 4131 | if (ErrorFound != NoError) { |
| 4132 | Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) |
| 4133 | << ErrorRange; |
| 4134 | Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; |
| 4135 | return StmtError(); |
| 4136 | } else if (CurContext->isDependentContext()) { |
| 4137 | UE = V = E = X = nullptr; |
| 4138 | } |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 4139 | } |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 4140 | } |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 4141 | |
| 4142 | getCurFunction()->setHasBranchProtectedScope(); |
| 4143 | |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 4144 | return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4145 | X, V, E, UE, IsXLHSInRHSPart, |
| 4146 | IsPostfixUpdate); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 4149 | StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, |
| 4150 | Stmt *AStmt, |
| 4151 | SourceLocation StartLoc, |
| 4152 | SourceLocation EndLoc) { |
| 4153 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 4154 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4155 | // OpenMP [2.16, Nesting of Regions] |
| 4156 | // If specified, a teams construct must be contained within a target |
| 4157 | // construct. That target construct must contain no statements or directives |
| 4158 | // outside of the teams construct. |
| 4159 | if (DSAStack->hasInnerTeamsRegion()) { |
| 4160 | auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true); |
| 4161 | bool OMPTeamsFound = true; |
| 4162 | if (auto *CS = dyn_cast<CompoundStmt>(S)) { |
| 4163 | auto I = CS->body_begin(); |
| 4164 | while (I != CS->body_end()) { |
| 4165 | auto OED = dyn_cast<OMPExecutableDirective>(*I); |
| 4166 | if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { |
| 4167 | OMPTeamsFound = false; |
| 4168 | break; |
| 4169 | } |
| 4170 | ++I; |
| 4171 | } |
| 4172 | assert(I != CS->body_end() && "Not found statement"); |
| 4173 | S = *I; |
| 4174 | } |
| 4175 | if (!OMPTeamsFound) { |
| 4176 | Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); |
| 4177 | Diag(DSAStack->getInnerTeamsRegionLoc(), |
| 4178 | diag::note_omp_nested_teams_construct_here); |
| 4179 | Diag(S->getLocStart(), diag::note_omp_nested_statement_here) |
| 4180 | << isa<OMPExecutableDirective>(S); |
| 4181 | return StmtError(); |
| 4182 | } |
| 4183 | } |
| 4184 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 4185 | getCurFunction()->setHasBranchProtectedScope(); |
| 4186 | |
| 4187 | return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 4188 | } |
| 4189 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4190 | StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, |
| 4191 | Stmt *AStmt, SourceLocation StartLoc, |
| 4192 | SourceLocation EndLoc) { |
| 4193 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 4194 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 4195 | // 1.2.2 OpenMP Language Terminology |
| 4196 | // Structured block - An executable statement with a single entry at the |
| 4197 | // top and a single exit at the bottom. |
| 4198 | // The point of exit cannot be a branch out of the structured block. |
| 4199 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 4200 | CS->getCapturedDecl()->setNothrow(); |
| 4201 | |
| 4202 | getCurFunction()->setHasBranchProtectedScope(); |
| 4203 | |
| 4204 | return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 4205 | } |
| 4206 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4207 | OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4208 | SourceLocation StartLoc, |
| 4209 | SourceLocation LParenLoc, |
| 4210 | SourceLocation EndLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 4211 | OMPClause *Res = nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4212 | switch (Kind) { |
| 4213 | case OMPC_if: |
| 4214 | Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 4215 | break; |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 4216 | case OMPC_final: |
| 4217 | Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 4218 | break; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4219 | case OMPC_num_threads: |
| 4220 | Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 4221 | break; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 4222 | case OMPC_safelen: |
| 4223 | Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 4224 | break; |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 4225 | case OMPC_collapse: |
| 4226 | Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 4227 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4228 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 4229 | case OMPC_proc_bind: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4230 | case OMPC_schedule: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4231 | case OMPC_private: |
| 4232 | case OMPC_firstprivate: |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 4233 | case OMPC_lastprivate: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4234 | case OMPC_shared: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 4235 | case OMPC_reduction: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 4236 | case OMPC_linear: |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 4237 | case OMPC_aligned: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 4238 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 4239 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4240 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 4241 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 4242 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 4243 | case OMPC_mergeable: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4244 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 4245 | case OMPC_flush: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 4246 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 4247 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 4248 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 4249 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 4250 | case OMPC_seq_cst: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4251 | case OMPC_unknown: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4252 | llvm_unreachable("Clause is not allowed."); |
| 4253 | } |
| 4254 | return Res; |
| 4255 | } |
| 4256 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4257 | OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4258 | SourceLocation LParenLoc, |
| 4259 | SourceLocation EndLoc) { |
| 4260 | Expr *ValExpr = Condition; |
| 4261 | if (!Condition->isValueDependent() && !Condition->isTypeDependent() && |
| 4262 | !Condition->isInstantiationDependent() && |
| 4263 | !Condition->containsUnexpandedParameterPack()) { |
| 4264 | ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4265 | Condition->getExprLoc(), Condition); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4266 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 4267 | return nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4268 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4269 | ValExpr = Val.get(); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
| 4272 | return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 4273 | } |
| 4274 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 4275 | OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, |
| 4276 | SourceLocation StartLoc, |
| 4277 | SourceLocation LParenLoc, |
| 4278 | SourceLocation EndLoc) { |
| 4279 | Expr *ValExpr = Condition; |
| 4280 | if (!Condition->isValueDependent() && !Condition->isTypeDependent() && |
| 4281 | !Condition->isInstantiationDependent() && |
| 4282 | !Condition->containsUnexpandedParameterPack()) { |
| 4283 | ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), |
| 4284 | Condition->getExprLoc(), Condition); |
| 4285 | if (Val.isInvalid()) |
| 4286 | return nullptr; |
| 4287 | |
| 4288 | ValExpr = Val.get(); |
| 4289 | } |
| 4290 | |
| 4291 | return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 4292 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 4293 | ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, |
| 4294 | Expr *Op) { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4295 | if (!Op) |
| 4296 | return ExprError(); |
| 4297 | |
| 4298 | class IntConvertDiagnoser : public ICEConvertDiagnoser { |
| 4299 | public: |
| 4300 | IntConvertDiagnoser() |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4301 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4302 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 4303 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4304 | return S.Diag(Loc, diag::err_omp_not_integral) << T; |
| 4305 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4306 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
| 4307 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4308 | return S.Diag(Loc, diag::err_omp_incomplete_type) << T; |
| 4309 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4310 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
| 4311 | QualType T, |
| 4312 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4313 | return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; |
| 4314 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4315 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
| 4316 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4317 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4318 | << ConvTy->isEnumeralType() << ConvTy; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4319 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4320 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 4321 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4322 | return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; |
| 4323 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4324 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
| 4325 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4326 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4327 | << ConvTy->isEnumeralType() << ConvTy; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4328 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4329 | SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, |
| 4330 | QualType) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4331 | llvm_unreachable("conversion functions are permitted"); |
| 4332 | } |
| 4333 | } ConvertDiagnoser; |
| 4334 | return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); |
| 4335 | } |
| 4336 | |
| 4337 | OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, |
| 4338 | SourceLocation StartLoc, |
| 4339 | SourceLocation LParenLoc, |
| 4340 | SourceLocation EndLoc) { |
| 4341 | Expr *ValExpr = NumThreads; |
| 4342 | if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() && |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4343 | !NumThreads->containsUnexpandedParameterPack()) { |
| 4344 | SourceLocation NumThreadsLoc = NumThreads->getLocStart(); |
| 4345 | ExprResult Val = |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 4346 | PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4347 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 4348 | return nullptr; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4349 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4350 | ValExpr = Val.get(); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4351 | |
| 4352 | // OpenMP [2.5, Restrictions] |
| 4353 | // The num_threads expression must evaluate to a positive integer value. |
| 4354 | llvm::APSInt Result; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4355 | if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() && |
| 4356 | !Result.isStrictlyPositive()) { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4357 | Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause) |
| 4358 | << "num_threads" << NumThreads->getSourceRange(); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 4359 | return nullptr; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4360 | } |
| 4361 | } |
| 4362 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4363 | return new (Context) |
| 4364 | OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 4367 | ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, |
| 4368 | OpenMPClauseKind CKind) { |
| 4369 | if (!E) |
| 4370 | return ExprError(); |
| 4371 | if (E->isValueDependent() || E->isTypeDependent() || |
| 4372 | E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4373 | return E; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 4374 | llvm::APSInt Result; |
| 4375 | ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); |
| 4376 | if (ICE.isInvalid()) |
| 4377 | return ExprError(); |
| 4378 | if (!Result.isStrictlyPositive()) { |
| 4379 | Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) |
| 4380 | << getOpenMPClauseName(CKind) << E->getSourceRange(); |
| 4381 | return ExprError(); |
| 4382 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 4383 | if (CKind == OMPC_aligned && !Result.isPowerOf2()) { |
| 4384 | Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) |
| 4385 | << E->getSourceRange(); |
| 4386 | return ExprError(); |
| 4387 | } |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 4388 | if (CKind == OMPC_collapse) { |
| 4389 | DSAStack->setCollapseNumber(Result.getExtValue()); |
| 4390 | } |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 4391 | return ICE; |
| 4392 | } |
| 4393 | |
| 4394 | OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, |
| 4395 | SourceLocation LParenLoc, |
| 4396 | SourceLocation EndLoc) { |
| 4397 | // OpenMP [2.8.1, simd construct, Description] |
| 4398 | // The parameter of the safelen clause must be a constant |
| 4399 | // positive integer expression. |
| 4400 | ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); |
| 4401 | if (Safelen.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 4402 | return nullptr; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 4403 | return new (Context) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4404 | OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 4407 | OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, |
| 4408 | SourceLocation StartLoc, |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 4409 | SourceLocation LParenLoc, |
| 4410 | SourceLocation EndLoc) { |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 4411 | // OpenMP [2.7.1, loop construct, Description] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 4412 | // OpenMP [2.8.1, simd construct, Description] |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 4413 | // OpenMP [2.9.6, distribute construct, Description] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 4414 | // The parameter of the collapse clause must be a constant |
| 4415 | // positive integer expression. |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 4416 | ExprResult NumForLoopsResult = |
| 4417 | VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); |
| 4418 | if (NumForLoopsResult.isInvalid()) |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 4419 | return nullptr; |
| 4420 | return new (Context) |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 4421 | OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4424 | OMPClause *Sema::ActOnOpenMPSimpleClause( |
| 4425 | OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, |
| 4426 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 4427 | OMPClause *Res = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4428 | switch (Kind) { |
| 4429 | case OMPC_default: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4430 | Res = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4431 | ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), |
| 4432 | ArgumentLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4433 | break; |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 4434 | case OMPC_proc_bind: |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4435 | Res = ActOnOpenMPProcBindClause( |
| 4436 | static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, |
| 4437 | LParenLoc, EndLoc); |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 4438 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4439 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 4440 | case OMPC_final: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4441 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 4442 | case OMPC_safelen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 4443 | case OMPC_collapse: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4444 | case OMPC_schedule: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4445 | case OMPC_private: |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 4446 | case OMPC_firstprivate: |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 4447 | case OMPC_lastprivate: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4448 | case OMPC_shared: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 4449 | case OMPC_reduction: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 4450 | case OMPC_linear: |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 4451 | case OMPC_aligned: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 4452 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 4453 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4454 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 4455 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 4456 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 4457 | case OMPC_mergeable: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4458 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 4459 | case OMPC_flush: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 4460 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 4461 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 4462 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 4463 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 4464 | case OMPC_seq_cst: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4465 | case OMPC_unknown: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4466 | llvm_unreachable("Clause is not allowed."); |
| 4467 | } |
| 4468 | return Res; |
| 4469 | } |
| 4470 | |
| 4471 | OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, |
| 4472 | SourceLocation KindKwLoc, |
| 4473 | SourceLocation StartLoc, |
| 4474 | SourceLocation LParenLoc, |
| 4475 | SourceLocation EndLoc) { |
| 4476 | if (Kind == OMPC_DEFAULT_unknown) { |
| 4477 | std::string Values; |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 4478 | static_assert(OMPC_DEFAULT_unknown > 0, |
| 4479 | "OMPC_DEFAULT_unknown not greater than 0"); |
Ted Kremenek | 725a097 | 2014-03-21 17:34:28 +0000 | [diff] [blame] | 4480 | std::string Sep(", "); |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 4481 | for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4482 | Values += "'"; |
| 4483 | Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); |
| 4484 | Values += "'"; |
| 4485 | switch (i) { |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 4486 | case OMPC_DEFAULT_unknown - 2: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4487 | Values += " or "; |
| 4488 | break; |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 4489 | case OMPC_DEFAULT_unknown - 1: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4490 | break; |
| 4491 | default: |
| 4492 | Values += Sep; |
| 4493 | break; |
| 4494 | } |
| 4495 | } |
| 4496 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4497 | << Values << getOpenMPClauseName(OMPC_default); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 4498 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4499 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4500 | switch (Kind) { |
| 4501 | case OMPC_DEFAULT_none: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 4502 | DSAStack->setDefaultDSANone(KindKwLoc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4503 | break; |
| 4504 | case OMPC_DEFAULT_shared: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 4505 | DSAStack->setDefaultDSAShared(KindKwLoc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4506 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4507 | case OMPC_DEFAULT_unknown: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4508 | llvm_unreachable("Clause kind is not allowed."); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4509 | break; |
| 4510 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4511 | return new (Context) |
| 4512 | OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 4515 | OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, |
| 4516 | SourceLocation KindKwLoc, |
| 4517 | SourceLocation StartLoc, |
| 4518 | SourceLocation LParenLoc, |
| 4519 | SourceLocation EndLoc) { |
| 4520 | if (Kind == OMPC_PROC_BIND_unknown) { |
| 4521 | std::string Values; |
| 4522 | std::string Sep(", "); |
| 4523 | for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) { |
| 4524 | Values += "'"; |
| 4525 | Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i); |
| 4526 | Values += "'"; |
| 4527 | switch (i) { |
| 4528 | case OMPC_PROC_BIND_unknown - 2: |
| 4529 | Values += " or "; |
| 4530 | break; |
| 4531 | case OMPC_PROC_BIND_unknown - 1: |
| 4532 | break; |
| 4533 | default: |
| 4534 | Values += Sep; |
| 4535 | break; |
| 4536 | } |
| 4537 | } |
| 4538 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4539 | << Values << getOpenMPClauseName(OMPC_proc_bind); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 4540 | return nullptr; |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 4541 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4542 | return new (Context) |
| 4543 | OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4546 | OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( |
| 4547 | OpenMPClauseKind Kind, unsigned Argument, Expr *Expr, |
| 4548 | SourceLocation StartLoc, SourceLocation LParenLoc, |
| 4549 | SourceLocation ArgumentLoc, SourceLocation CommaLoc, |
| 4550 | SourceLocation EndLoc) { |
| 4551 | OMPClause *Res = nullptr; |
| 4552 | switch (Kind) { |
| 4553 | case OMPC_schedule: |
| 4554 | Res = ActOnOpenMPScheduleClause( |
| 4555 | static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc, |
| 4556 | LParenLoc, ArgumentLoc, CommaLoc, EndLoc); |
| 4557 | break; |
| 4558 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 4559 | case OMPC_final: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4560 | case OMPC_num_threads: |
| 4561 | case OMPC_safelen: |
| 4562 | case OMPC_collapse: |
| 4563 | case OMPC_default: |
| 4564 | case OMPC_proc_bind: |
| 4565 | case OMPC_private: |
| 4566 | case OMPC_firstprivate: |
| 4567 | case OMPC_lastprivate: |
| 4568 | case OMPC_shared: |
| 4569 | case OMPC_reduction: |
| 4570 | case OMPC_linear: |
| 4571 | case OMPC_aligned: |
| 4572 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 4573 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4574 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 4575 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 4576 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 4577 | case OMPC_mergeable: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4578 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 4579 | case OMPC_flush: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 4580 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 4581 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 4582 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 4583 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 4584 | case OMPC_seq_cst: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4585 | case OMPC_unknown: |
| 4586 | llvm_unreachable("Clause is not allowed."); |
| 4587 | } |
| 4588 | return Res; |
| 4589 | } |
| 4590 | |
| 4591 | OMPClause *Sema::ActOnOpenMPScheduleClause( |
| 4592 | OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, |
| 4593 | SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, |
| 4594 | SourceLocation EndLoc) { |
| 4595 | if (Kind == OMPC_SCHEDULE_unknown) { |
| 4596 | std::string Values; |
| 4597 | std::string Sep(", "); |
| 4598 | for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) { |
| 4599 | Values += "'"; |
| 4600 | Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i); |
| 4601 | Values += "'"; |
| 4602 | switch (i) { |
| 4603 | case OMPC_SCHEDULE_unknown - 2: |
| 4604 | Values += " or "; |
| 4605 | break; |
| 4606 | case OMPC_SCHEDULE_unknown - 1: |
| 4607 | break; |
| 4608 | default: |
| 4609 | Values += Sep; |
| 4610 | break; |
| 4611 | } |
| 4612 | } |
| 4613 | Diag(KindLoc, diag::err_omp_unexpected_clause_value) |
| 4614 | << Values << getOpenMPClauseName(OMPC_schedule); |
| 4615 | return nullptr; |
| 4616 | } |
| 4617 | Expr *ValExpr = ChunkSize; |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 4618 | Expr *HelperValExpr = nullptr; |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4619 | if (ChunkSize) { |
| 4620 | if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && |
| 4621 | !ChunkSize->isInstantiationDependent() && |
| 4622 | !ChunkSize->containsUnexpandedParameterPack()) { |
| 4623 | SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); |
| 4624 | ExprResult Val = |
| 4625 | PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); |
| 4626 | if (Val.isInvalid()) |
| 4627 | return nullptr; |
| 4628 | |
| 4629 | ValExpr = Val.get(); |
| 4630 | |
| 4631 | // OpenMP [2.7.1, Restrictions] |
| 4632 | // chunk_size must be a loop invariant integer expression with a positive |
| 4633 | // value. |
| 4634 | llvm::APSInt Result; |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 4635 | if (ValExpr->isIntegerConstantExpr(Result, Context)) { |
| 4636 | if (Result.isSigned() && !Result.isStrictlyPositive()) { |
| 4637 | Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) |
| 4638 | << "schedule" << ChunkSize->getSourceRange(); |
| 4639 | return nullptr; |
| 4640 | } |
| 4641 | } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { |
| 4642 | auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(), |
| 4643 | ChunkSize->getType(), ".chunk."); |
| 4644 | auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(), |
| 4645 | ChunkSize->getExprLoc(), |
| 4646 | /*RefersToCapture=*/true); |
| 4647 | HelperValExpr = ImpVarRef; |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4648 | } |
| 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 4653 | EndLoc, Kind, ValExpr, HelperValExpr); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4656 | OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, |
| 4657 | SourceLocation StartLoc, |
| 4658 | SourceLocation EndLoc) { |
| 4659 | OMPClause *Res = nullptr; |
| 4660 | switch (Kind) { |
| 4661 | case OMPC_ordered: |
| 4662 | Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); |
| 4663 | break; |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 4664 | case OMPC_nowait: |
| 4665 | Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); |
| 4666 | break; |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 4667 | case OMPC_untied: |
| 4668 | Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); |
| 4669 | break; |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 4670 | case OMPC_mergeable: |
| 4671 | Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); |
| 4672 | break; |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 4673 | case OMPC_read: |
| 4674 | Res = ActOnOpenMPReadClause(StartLoc, EndLoc); |
| 4675 | break; |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 4676 | case OMPC_write: |
| 4677 | Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); |
| 4678 | break; |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 4679 | case OMPC_update: |
| 4680 | Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); |
| 4681 | break; |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 4682 | case OMPC_capture: |
| 4683 | Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); |
| 4684 | break; |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 4685 | case OMPC_seq_cst: |
| 4686 | Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); |
| 4687 | break; |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4688 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 4689 | case OMPC_final: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4690 | case OMPC_num_threads: |
| 4691 | case OMPC_safelen: |
| 4692 | case OMPC_collapse: |
| 4693 | case OMPC_schedule: |
| 4694 | case OMPC_private: |
| 4695 | case OMPC_firstprivate: |
| 4696 | case OMPC_lastprivate: |
| 4697 | case OMPC_shared: |
| 4698 | case OMPC_reduction: |
| 4699 | case OMPC_linear: |
| 4700 | case OMPC_aligned: |
| 4701 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 4702 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4703 | case OMPC_default: |
| 4704 | case OMPC_proc_bind: |
| 4705 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 4706 | case OMPC_flush: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4707 | case OMPC_unknown: |
| 4708 | llvm_unreachable("Clause is not allowed."); |
| 4709 | } |
| 4710 | return Res; |
| 4711 | } |
| 4712 | |
| 4713 | OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, |
| 4714 | SourceLocation EndLoc) { |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 4715 | DSAStack->setOrderedRegion(); |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4716 | return new (Context) OMPOrderedClause(StartLoc, EndLoc); |
| 4717 | } |
| 4718 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 4719 | OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, |
| 4720 | SourceLocation EndLoc) { |
| 4721 | return new (Context) OMPNowaitClause(StartLoc, EndLoc); |
| 4722 | } |
| 4723 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 4724 | OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, |
| 4725 | SourceLocation EndLoc) { |
| 4726 | return new (Context) OMPUntiedClause(StartLoc, EndLoc); |
| 4727 | } |
| 4728 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 4729 | OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, |
| 4730 | SourceLocation EndLoc) { |
| 4731 | return new (Context) OMPMergeableClause(StartLoc, EndLoc); |
| 4732 | } |
| 4733 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 4734 | OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, |
| 4735 | SourceLocation EndLoc) { |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 4736 | return new (Context) OMPReadClause(StartLoc, EndLoc); |
| 4737 | } |
| 4738 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 4739 | OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, |
| 4740 | SourceLocation EndLoc) { |
| 4741 | return new (Context) OMPWriteClause(StartLoc, EndLoc); |
| 4742 | } |
| 4743 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 4744 | OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, |
| 4745 | SourceLocation EndLoc) { |
| 4746 | return new (Context) OMPUpdateClause(StartLoc, EndLoc); |
| 4747 | } |
| 4748 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 4749 | OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, |
| 4750 | SourceLocation EndLoc) { |
| 4751 | return new (Context) OMPCaptureClause(StartLoc, EndLoc); |
| 4752 | } |
| 4753 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 4754 | OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, |
| 4755 | SourceLocation EndLoc) { |
| 4756 | return new (Context) OMPSeqCstClause(StartLoc, EndLoc); |
| 4757 | } |
| 4758 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 4759 | OMPClause *Sema::ActOnOpenMPVarListClause( |
| 4760 | OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, |
| 4761 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, |
| 4762 | SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, |
| 4763 | const DeclarationNameInfo &ReductionId) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 4764 | OMPClause *Res = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4765 | switch (Kind) { |
| 4766 | case OMPC_private: |
| 4767 | Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 4768 | break; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 4769 | case OMPC_firstprivate: |
| 4770 | Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 4771 | break; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 4772 | case OMPC_lastprivate: |
| 4773 | Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 4774 | break; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4775 | case OMPC_shared: |
| 4776 | Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 4777 | break; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 4778 | case OMPC_reduction: |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 4779 | Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, |
| 4780 | EndLoc, ReductionIdScopeSpec, ReductionId); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 4781 | break; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 4782 | case OMPC_linear: |
| 4783 | Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, |
| 4784 | ColonLoc, EndLoc); |
| 4785 | break; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 4786 | case OMPC_aligned: |
| 4787 | Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, |
| 4788 | ColonLoc, EndLoc); |
| 4789 | break; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 4790 | case OMPC_copyin: |
| 4791 | Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 4792 | break; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 4793 | case OMPC_copyprivate: |
| 4794 | Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 4795 | break; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 4796 | case OMPC_flush: |
| 4797 | Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 4798 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 4799 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 4800 | case OMPC_final: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 4801 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 4802 | case OMPC_safelen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 4803 | case OMPC_collapse: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4804 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 4805 | case OMPC_proc_bind: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 4806 | case OMPC_schedule: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 4807 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 4808 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 4809 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 4810 | case OMPC_mergeable: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4811 | case OMPC_threadprivate: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 4812 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 4813 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 4814 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 4815 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 4816 | case OMPC_seq_cst: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4817 | case OMPC_unknown: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4818 | llvm_unreachable("Clause is not allowed."); |
| 4819 | } |
| 4820 | return Res; |
| 4821 | } |
| 4822 | |
| 4823 | OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, |
| 4824 | SourceLocation StartLoc, |
| 4825 | SourceLocation LParenLoc, |
| 4826 | SourceLocation EndLoc) { |
| 4827 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 4828 | SmallVector<Expr *, 8> PrivateCopies; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4829 | for (auto &RefExpr : VarList) { |
| 4830 | assert(RefExpr && "NULL expr in OpenMP private clause."); |
| 4831 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4832 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4833 | Vars.push_back(RefExpr); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 4834 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4835 | continue; |
| 4836 | } |
| 4837 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4838 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4839 | // OpenMP [2.1, C/C++] |
| 4840 | // A list item is a variable name. |
| 4841 | // OpenMP [2.9.3.3, Restrictions, p.1] |
| 4842 | // A variable that is part of another variable (as an array or |
| 4843 | // structure element) cannot appear in a private clause. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4844 | DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4845 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4846 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4847 | continue; |
| 4848 | } |
| 4849 | Decl *D = DE->getDecl(); |
| 4850 | VarDecl *VD = cast<VarDecl>(D); |
| 4851 | |
| 4852 | QualType Type = VD->getType(); |
| 4853 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 4854 | // It will be analyzed later. |
| 4855 | Vars.push_back(DE); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 4856 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4857 | continue; |
| 4858 | } |
| 4859 | |
| 4860 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 4861 | // A variable that appears in a private clause must not have an incomplete |
| 4862 | // type or a reference type. |
| 4863 | if (RequireCompleteType(ELoc, Type, |
| 4864 | diag::err_omp_private_incomplete_type)) { |
| 4865 | continue; |
| 4866 | } |
| 4867 | if (Type->isReferenceType()) { |
| 4868 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4869 | << getOpenMPClauseName(OMPC_private) << Type; |
| 4870 | bool IsDecl = |
| 4871 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 4872 | Diag(VD->getLocation(), |
| 4873 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 4874 | << VD; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4875 | continue; |
| 4876 | } |
| 4877 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4878 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 4879 | // in a Construct] |
| 4880 | // Variables with the predetermined data-sharing attributes may not be |
| 4881 | // listed in data-sharing attributes clauses, except for the cases |
| 4882 | // listed below. For these exceptions only, listing a predetermined |
| 4883 | // variable in a data-sharing attribute clause is allowed and overrides |
| 4884 | // the variable's predetermined data-sharing attributes. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 4885 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4886 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4887 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 4888 | << getOpenMPClauseName(OMPC_private); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 4889 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4890 | continue; |
| 4891 | } |
| 4892 | |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 4893 | // Variably modified types are not supported for tasks. |
Alexey Bataev | 5129d3a | 2015-05-21 09:47:46 +0000 | [diff] [blame] | 4894 | if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 4895 | DSAStack->getCurrentDirective() == OMPD_task) { |
| 4896 | Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) |
| 4897 | << getOpenMPClauseName(OMPC_private) << Type |
| 4898 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 4899 | bool IsDecl = |
| 4900 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 4901 | Diag(VD->getLocation(), |
| 4902 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 4903 | << VD; |
| 4904 | continue; |
| 4905 | } |
| 4906 | |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 4907 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] |
| 4908 | // A variable of class type (or array thereof) that appears in a private |
| 4909 | // clause requires an accessible, unambiguous default constructor for the |
| 4910 | // class type. |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 4911 | // Generate helper private variable and initialize it with the default |
| 4912 | // value. The address of the original variable is replaced by the address of |
| 4913 | // the new private variable in CodeGen. This new variable is not added to |
| 4914 | // IdResolver, so the code in the OpenMP region uses original variable for |
| 4915 | // proper diagnostics. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 4916 | Type = Type.getUnqualifiedType(); |
| 4917 | auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName()); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 4918 | ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 4919 | if (VDPrivate->isInvalidDecl()) |
| 4920 | continue; |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 4921 | auto VDPrivateRefExpr = buildDeclRefExpr( |
| 4922 | *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc()); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 4923 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 4924 | DSAStack->addDSA(VD, DE, OMPC_private); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4925 | Vars.push_back(DE); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 4926 | PrivateCopies.push_back(VDPrivateRefExpr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4929 | if (Vars.empty()) |
| 4930 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4931 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 4932 | return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, |
| 4933 | PrivateCopies); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 4936 | namespace { |
| 4937 | class DiagsUninitializedSeveretyRAII { |
| 4938 | private: |
| 4939 | DiagnosticsEngine &Diags; |
| 4940 | SourceLocation SavedLoc; |
| 4941 | bool IsIgnored; |
| 4942 | |
| 4943 | public: |
| 4944 | DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, |
| 4945 | bool IsIgnored) |
| 4946 | : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { |
| 4947 | if (!IsIgnored) { |
| 4948 | Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, |
| 4949 | /*Map*/ diag::Severity::Ignored, Loc); |
| 4950 | } |
| 4951 | } |
| 4952 | ~DiagsUninitializedSeveretyRAII() { |
| 4953 | if (!IsIgnored) |
| 4954 | Diags.popMappings(SavedLoc); |
| 4955 | } |
| 4956 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4957 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 4958 | |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 4959 | OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, |
| 4960 | SourceLocation StartLoc, |
| 4961 | SourceLocation LParenLoc, |
| 4962 | SourceLocation EndLoc) { |
| 4963 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 4964 | SmallVector<Expr *, 8> PrivateCopies; |
| 4965 | SmallVector<Expr *, 8> Inits; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 4966 | bool IsImplicitClause = |
| 4967 | StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); |
| 4968 | auto ImplicitClauseLoc = DSAStack->getConstructLoc(); |
| 4969 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4970 | for (auto &RefExpr : VarList) { |
| 4971 | assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); |
| 4972 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 4973 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4974 | Vars.push_back(RefExpr); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 4975 | PrivateCopies.push_back(nullptr); |
| 4976 | Inits.push_back(nullptr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 4977 | continue; |
| 4978 | } |
| 4979 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 4980 | SourceLocation ELoc = |
| 4981 | IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 4982 | // OpenMP [2.1, C/C++] |
| 4983 | // A list item is a variable name. |
| 4984 | // OpenMP [2.9.3.3, Restrictions, p.1] |
| 4985 | // A variable that is part of another variable (as an array or |
| 4986 | // structure element) cannot appear in a private clause. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4987 | DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 4988 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 4989 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 4990 | continue; |
| 4991 | } |
| 4992 | Decl *D = DE->getDecl(); |
| 4993 | VarDecl *VD = cast<VarDecl>(D); |
| 4994 | |
| 4995 | QualType Type = VD->getType(); |
| 4996 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 4997 | // It will be analyzed later. |
| 4998 | Vars.push_back(DE); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 4999 | PrivateCopies.push_back(nullptr); |
| 5000 | Inits.push_back(nullptr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5001 | continue; |
| 5002 | } |
| 5003 | |
| 5004 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 5005 | // A variable that appears in a private clause must not have an incomplete |
| 5006 | // type or a reference type. |
| 5007 | if (RequireCompleteType(ELoc, Type, |
| 5008 | diag::err_omp_firstprivate_incomplete_type)) { |
| 5009 | continue; |
| 5010 | } |
| 5011 | if (Type->isReferenceType()) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5012 | if (IsImplicitClause) { |
| 5013 | Diag(ImplicitClauseLoc, |
| 5014 | diag::err_omp_task_predetermined_firstprivate_ref_type_arg) |
| 5015 | << Type; |
| 5016 | Diag(RefExpr->getExprLoc(), diag::note_used_here); |
| 5017 | } else { |
| 5018 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
| 5019 | << getOpenMPClauseName(OMPC_firstprivate) << Type; |
| 5020 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5021 | bool IsDecl = |
| 5022 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5023 | Diag(VD->getLocation(), |
| 5024 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5025 | << VD; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5026 | continue; |
| 5027 | } |
| 5028 | |
| 5029 | // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] |
| 5030 | // A variable of class type (or array thereof) that appears in a private |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 5031 | // clause requires an accessible, unambiguous copy constructor for the |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5032 | // class type. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 5033 | auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5034 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5035 | // If an implicit firstprivate variable found it was checked already. |
| 5036 | if (!IsImplicitClause) { |
| 5037 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 5038 | bool IsConstant = ElemType.isConstant(Context); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5039 | // OpenMP [2.4.13, Data-sharing Attribute Clauses] |
| 5040 | // A list item that specifies a given variable may not appear in more |
| 5041 | // than one clause on the same directive, except that a variable may be |
| 5042 | // specified in both firstprivate and lastprivate clauses. |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5043 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5044 | DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5045 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5046 | << getOpenMPClauseName(DVar.CKind) |
| 5047 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 5048 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5049 | continue; |
| 5050 | } |
| 5051 | |
| 5052 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 5053 | // in a Construct] |
| 5054 | // Variables with the predetermined data-sharing attributes may not be |
| 5055 | // listed in data-sharing attributes clauses, except for the cases |
| 5056 | // listed below. For these exceptions only, listing a predetermined |
| 5057 | // variable in a data-sharing attribute clause is allowed and overrides |
| 5058 | // the variable's predetermined data-sharing attributes. |
| 5059 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 5060 | // in a Construct, C/C++, p.2] |
| 5061 | // Variables with const-qualified type having no mutable member may be |
| 5062 | // listed in a firstprivate clause, even if they are static data members. |
| 5063 | if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr && |
| 5064 | DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { |
| 5065 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5066 | << getOpenMPClauseName(DVar.CKind) |
| 5067 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 5068 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5069 | continue; |
| 5070 | } |
| 5071 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5072 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5073 | // OpenMP [2.9.3.4, Restrictions, p.2] |
| 5074 | // A list item that is private within a parallel region must not appear |
| 5075 | // in a firstprivate clause on a worksharing construct if any of the |
| 5076 | // worksharing regions arising from the worksharing construct ever bind |
| 5077 | // to any of the parallel regions arising from the parallel construct. |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 5078 | if (isOpenMPWorksharingDirective(CurrDir) && |
| 5079 | !isOpenMPParallelDirective(CurrDir)) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5080 | DVar = DSAStack->getImplicitDSA(VD, true); |
| 5081 | if (DVar.CKind != OMPC_shared && |
| 5082 | (isOpenMPParallelDirective(DVar.DKind) || |
| 5083 | DVar.DKind == OMPD_unknown)) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5084 | Diag(ELoc, diag::err_omp_required_access) |
| 5085 | << getOpenMPClauseName(OMPC_firstprivate) |
| 5086 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 5087 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5088 | continue; |
| 5089 | } |
| 5090 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5091 | // OpenMP [2.9.3.4, Restrictions, p.3] |
| 5092 | // A list item that appears in a reduction clause of a parallel construct |
| 5093 | // must not appear in a firstprivate clause on a worksharing or task |
| 5094 | // construct if any of the worksharing or task regions arising from the |
| 5095 | // worksharing or task construct ever bind to any of the parallel regions |
| 5096 | // arising from the parallel construct. |
| 5097 | // OpenMP [2.9.3.4, Restrictions, p.4] |
| 5098 | // A list item that appears in a reduction clause in worksharing |
| 5099 | // construct must not appear in a firstprivate clause in a task construct |
| 5100 | // encountered during execution of any of the worksharing regions arising |
| 5101 | // from the worksharing construct. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5102 | if (CurrDir == OMPD_task) { |
| 5103 | DVar = |
| 5104 | DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), |
| 5105 | [](OpenMPDirectiveKind K) -> bool { |
| 5106 | return isOpenMPParallelDirective(K) || |
| 5107 | isOpenMPWorksharingDirective(K); |
| 5108 | }, |
| 5109 | false); |
| 5110 | if (DVar.CKind == OMPC_reduction && |
| 5111 | (isOpenMPParallelDirective(DVar.DKind) || |
| 5112 | isOpenMPWorksharingDirective(DVar.DKind))) { |
| 5113 | Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) |
| 5114 | << getOpenMPDirectiveName(DVar.DKind); |
| 5115 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
| 5116 | continue; |
| 5117 | } |
| 5118 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 5121 | // Variably modified types are not supported for tasks. |
Alexey Bataev | 5129d3a | 2015-05-21 09:47:46 +0000 | [diff] [blame] | 5122 | if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 5123 | DSAStack->getCurrentDirective() == OMPD_task) { |
| 5124 | Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) |
| 5125 | << getOpenMPClauseName(OMPC_firstprivate) << Type |
| 5126 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 5127 | bool IsDecl = |
| 5128 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5129 | Diag(VD->getLocation(), |
| 5130 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5131 | << VD; |
| 5132 | continue; |
| 5133 | } |
| 5134 | |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 5135 | Type = Type.getUnqualifiedType(); |
| 5136 | auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName()); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 5137 | // Generate helper private variable and initialize it with the value of the |
| 5138 | // original variable. The address of the original variable is replaced by |
| 5139 | // the address of the new private variable in the CodeGen. This new variable |
| 5140 | // is not added to IdResolver, so the code in the OpenMP region uses |
| 5141 | // original variable for proper diagnostics and variable capturing. |
| 5142 | Expr *VDInitRefExpr = nullptr; |
| 5143 | // For arrays generate initializer for single element and replace it by the |
| 5144 | // original array element in CodeGen. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 5145 | if (Type->isArrayType()) { |
| 5146 | auto VDInit = |
| 5147 | buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName()); |
| 5148 | VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 5149 | auto Init = DefaultLvalueConversion(VDInitRefExpr).get(); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 5150 | ElemType = ElemType.getUnqualifiedType(); |
| 5151 | auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType, |
| 5152 | ".firstprivate.temp"); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 5153 | InitializedEntity Entity = |
| 5154 | InitializedEntity::InitializeVariable(VDInitTemp); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 5155 | InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); |
| 5156 | |
| 5157 | InitializationSequence InitSeq(*this, Entity, Kind, Init); |
| 5158 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); |
| 5159 | if (Result.isInvalid()) |
| 5160 | VDPrivate->setInvalidDecl(); |
| 5161 | else |
| 5162 | VDPrivate->setInit(Result.getAs<Expr>()); |
| 5163 | } else { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 5164 | auto *VDInit = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5165 | buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp"); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 5166 | VDInitRefExpr = |
| 5167 | buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc()); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 5168 | AddInitializerToDecl(VDPrivate, |
| 5169 | DefaultLvalueConversion(VDInitRefExpr).get(), |
| 5170 | /*DirectInit=*/false, /*TypeMayContainAuto=*/false); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 5171 | } |
| 5172 | if (VDPrivate->isInvalidDecl()) { |
| 5173 | if (IsImplicitClause) { |
| 5174 | Diag(DE->getExprLoc(), |
| 5175 | diag::note_omp_task_predetermined_firstprivate_here); |
| 5176 | } |
| 5177 | continue; |
| 5178 | } |
| 5179 | CurContext->addDecl(VDPrivate); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5180 | auto VDPrivateRefExpr = buildDeclRefExpr( |
| 5181 | *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc()); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5182 | DSAStack->addDSA(VD, DE, OMPC_firstprivate); |
| 5183 | Vars.push_back(DE); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 5184 | PrivateCopies.push_back(VDPrivateRefExpr); |
| 5185 | Inits.push_back(VDInitRefExpr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5188 | if (Vars.empty()) |
| 5189 | return nullptr; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5190 | |
| 5191 | return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 5192 | Vars, PrivateCopies, Inits); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 5193 | } |
| 5194 | |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5195 | OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, |
| 5196 | SourceLocation StartLoc, |
| 5197 | SourceLocation LParenLoc, |
| 5198 | SourceLocation EndLoc) { |
| 5199 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5200 | SmallVector<Expr *, 8> SrcExprs; |
| 5201 | SmallVector<Expr *, 8> DstExprs; |
| 5202 | SmallVector<Expr *, 8> AssignmentOps; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5203 | for (auto &RefExpr : VarList) { |
| 5204 | assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); |
| 5205 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
| 5206 | // It will be analyzed later. |
| 5207 | Vars.push_back(RefExpr); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5208 | SrcExprs.push_back(nullptr); |
| 5209 | DstExprs.push_back(nullptr); |
| 5210 | AssignmentOps.push_back(nullptr); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5211 | continue; |
| 5212 | } |
| 5213 | |
| 5214 | SourceLocation ELoc = RefExpr->getExprLoc(); |
| 5215 | // OpenMP [2.1, C/C++] |
| 5216 | // A list item is a variable name. |
| 5217 | // OpenMP [2.14.3.5, Restrictions, p.1] |
| 5218 | // A variable that is part of another variable (as an array or structure |
| 5219 | // element) cannot appear in a lastprivate clause. |
| 5220 | DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
| 5221 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
| 5222 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
| 5223 | continue; |
| 5224 | } |
| 5225 | Decl *D = DE->getDecl(); |
| 5226 | VarDecl *VD = cast<VarDecl>(D); |
| 5227 | |
| 5228 | QualType Type = VD->getType(); |
| 5229 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 5230 | // It will be analyzed later. |
| 5231 | Vars.push_back(DE); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5232 | SrcExprs.push_back(nullptr); |
| 5233 | DstExprs.push_back(nullptr); |
| 5234 | AssignmentOps.push_back(nullptr); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5235 | continue; |
| 5236 | } |
| 5237 | |
| 5238 | // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] |
| 5239 | // A variable that appears in a lastprivate clause must not have an |
| 5240 | // incomplete type or a reference type. |
| 5241 | if (RequireCompleteType(ELoc, Type, |
| 5242 | diag::err_omp_lastprivate_incomplete_type)) { |
| 5243 | continue; |
| 5244 | } |
| 5245 | if (Type->isReferenceType()) { |
| 5246 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
| 5247 | << getOpenMPClauseName(OMPC_lastprivate) << Type; |
| 5248 | bool IsDecl = |
| 5249 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5250 | Diag(VD->getLocation(), |
| 5251 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5252 | << VD; |
| 5253 | continue; |
| 5254 | } |
| 5255 | |
| 5256 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 5257 | // in a Construct] |
| 5258 | // Variables with the predetermined data-sharing attributes may not be |
| 5259 | // listed in data-sharing attributes clauses, except for the cases |
| 5260 | // listed below. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5261 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5262 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && |
| 5263 | DVar.CKind != OMPC_firstprivate && |
| 5264 | (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { |
| 5265 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 5266 | << getOpenMPClauseName(DVar.CKind) |
| 5267 | << getOpenMPClauseName(OMPC_lastprivate); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 5268 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5269 | continue; |
| 5270 | } |
| 5271 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5272 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
| 5273 | // OpenMP [2.14.3.5, Restrictions, p.2] |
| 5274 | // A list item that is private within a parallel region, or that appears in |
| 5275 | // the reduction clause of a parallel construct, must not appear in a |
| 5276 | // lastprivate clause on a worksharing construct if any of the corresponding |
| 5277 | // worksharing regions ever binds to any of the corresponding parallel |
| 5278 | // regions. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5279 | DSAStackTy::DSAVarData TopDVar = DVar; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 5280 | if (isOpenMPWorksharingDirective(CurrDir) && |
| 5281 | !isOpenMPParallelDirective(CurrDir)) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5282 | DVar = DSAStack->getImplicitDSA(VD, true); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5283 | if (DVar.CKind != OMPC_shared) { |
| 5284 | Diag(ELoc, diag::err_omp_required_access) |
| 5285 | << getOpenMPClauseName(OMPC_lastprivate) |
| 5286 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 5287 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5288 | continue; |
| 5289 | } |
| 5290 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5291 | // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5292 | // A variable of class type (or array thereof) that appears in a |
| 5293 | // lastprivate clause requires an accessible, unambiguous default |
| 5294 | // constructor for the class type, unless the list item is also specified |
| 5295 | // in a firstprivate clause. |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5296 | // A variable of class type (or array thereof) that appears in a |
| 5297 | // lastprivate clause requires an accessible, unambiguous copy assignment |
| 5298 | // operator for the class type. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5299 | Type = Context.getBaseElementType(Type).getNonReferenceType(); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5300 | auto *SrcVD = buildVarDecl(*this, DE->getLocStart(), |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5301 | Type.getUnqualifiedType(), ".lastprivate.src"); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5302 | auto *PseudoSrcExpr = buildDeclRefExpr( |
| 5303 | *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc()); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5304 | auto *DstVD = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5305 | buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst"); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5306 | auto *PseudoDstExpr = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5307 | buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc()); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5308 | // For arrays generate assignment operation for single element and replace |
| 5309 | // it by the original array element in CodeGen. |
| 5310 | auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, |
| 5311 | PseudoDstExpr, PseudoSrcExpr); |
| 5312 | if (AssignmentOp.isInvalid()) |
| 5313 | continue; |
| 5314 | AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), |
| 5315 | /*DiscardedValue=*/true); |
| 5316 | if (AssignmentOp.isInvalid()) |
| 5317 | continue; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5318 | |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5319 | if (TopDVar.CKind != OMPC_firstprivate) |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5320 | DSAStack->addDSA(VD, DE, OMPC_lastprivate); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5321 | Vars.push_back(DE); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5322 | SrcExprs.push_back(PseudoSrcExpr); |
| 5323 | DstExprs.push_back(PseudoDstExpr); |
| 5324 | AssignmentOps.push_back(AssignmentOp.get()); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5325 | } |
| 5326 | |
| 5327 | if (Vars.empty()) |
| 5328 | return nullptr; |
| 5329 | |
| 5330 | return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 5331 | Vars, SrcExprs, DstExprs, AssignmentOps); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 5332 | } |
| 5333 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 5334 | OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, |
| 5335 | SourceLocation StartLoc, |
| 5336 | SourceLocation LParenLoc, |
| 5337 | SourceLocation EndLoc) { |
| 5338 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5339 | for (auto &RefExpr : VarList) { |
| 5340 | assert(RefExpr && "NULL expr in OpenMP shared clause."); |
| 5341 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 5342 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5343 | Vars.push_back(RefExpr); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 5344 | continue; |
| 5345 | } |
| 5346 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5347 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 5348 | // OpenMP [2.1, C/C++] |
| 5349 | // A list item is a variable name. |
Alexey Bataev | d4dbdf5 | 2014-03-06 12:27:56 +0000 | [diff] [blame] | 5350 | // OpenMP [2.14.3.2, Restrictions, p.1] |
| 5351 | // A variable that is part of another variable (as an array or structure |
| 5352 | // element) cannot appear in a shared unless it is a static data member |
| 5353 | // of a C++ class. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5354 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 5355 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5356 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 5357 | continue; |
| 5358 | } |
| 5359 | Decl *D = DE->getDecl(); |
| 5360 | VarDecl *VD = cast<VarDecl>(D); |
| 5361 | |
| 5362 | QualType Type = VD->getType(); |
| 5363 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 5364 | // It will be analyzed later. |
| 5365 | Vars.push_back(DE); |
| 5366 | continue; |
| 5367 | } |
| 5368 | |
| 5369 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 5370 | // in a Construct] |
| 5371 | // Variables with the predetermined data-sharing attributes may not be |
| 5372 | // listed in data-sharing attributes clauses, except for the cases |
| 5373 | // listed below. For these exceptions only, listing a predetermined |
| 5374 | // variable in a data-sharing attribute clause is allowed and overrides |
| 5375 | // the variable's predetermined data-sharing attributes. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5376 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5377 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && |
| 5378 | DVar.RefExpr) { |
| 5379 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 5380 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 5381 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 5382 | continue; |
| 5383 | } |
| 5384 | |
| 5385 | DSAStack->addDSA(VD, DE, OMPC_shared); |
| 5386 | Vars.push_back(DE); |
| 5387 | } |
| 5388 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5389 | if (Vars.empty()) |
| 5390 | return nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 5391 | |
| 5392 | return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 5393 | } |
| 5394 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5395 | namespace { |
| 5396 | class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { |
| 5397 | DSAStackTy *Stack; |
| 5398 | |
| 5399 | public: |
| 5400 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 5401 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5402 | DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5403 | if (DVar.CKind == OMPC_shared && !DVar.RefExpr) |
| 5404 | return false; |
| 5405 | if (DVar.CKind != OMPC_unknown) |
| 5406 | return true; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5407 | DSAStackTy::DSAVarData DVarPrivate = |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5408 | Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5409 | if (DVarPrivate.CKind != OMPC_unknown) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5410 | return true; |
| 5411 | return false; |
| 5412 | } |
| 5413 | return false; |
| 5414 | } |
| 5415 | bool VisitStmt(Stmt *S) { |
| 5416 | for (auto Child : S->children()) { |
| 5417 | if (Child && Visit(Child)) |
| 5418 | return true; |
| 5419 | } |
| 5420 | return false; |
| 5421 | } |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 5422 | explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5423 | }; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 5424 | } // namespace |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5425 | |
| 5426 | OMPClause *Sema::ActOnOpenMPReductionClause( |
| 5427 | ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, |
| 5428 | SourceLocation ColonLoc, SourceLocation EndLoc, |
| 5429 | CXXScopeSpec &ReductionIdScopeSpec, |
| 5430 | const DeclarationNameInfo &ReductionId) { |
| 5431 | // TODO: Allow scope specification search when 'declare reduction' is |
| 5432 | // supported. |
| 5433 | assert(ReductionIdScopeSpec.isEmpty() && |
| 5434 | "No support for scoped reduction identifiers yet."); |
| 5435 | |
| 5436 | auto DN = ReductionId.getName(); |
| 5437 | auto OOK = DN.getCXXOverloadedOperator(); |
| 5438 | BinaryOperatorKind BOK = BO_Comma; |
| 5439 | |
| 5440 | // OpenMP [2.14.3.6, reduction clause] |
| 5441 | // C |
| 5442 | // reduction-identifier is either an identifier or one of the following |
| 5443 | // operators: +, -, *, &, |, ^, && and || |
| 5444 | // C++ |
| 5445 | // reduction-identifier is either an id-expression or one of the following |
| 5446 | // operators: +, -, *, &, |, ^, && and || |
| 5447 | // FIXME: Only 'min' and 'max' identifiers are supported for now. |
| 5448 | switch (OOK) { |
| 5449 | case OO_Plus: |
| 5450 | case OO_Minus: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5451 | BOK = BO_Add; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5452 | break; |
| 5453 | case OO_Star: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5454 | BOK = BO_Mul; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5455 | break; |
| 5456 | case OO_Amp: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5457 | BOK = BO_And; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5458 | break; |
| 5459 | case OO_Pipe: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5460 | BOK = BO_Or; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5461 | break; |
| 5462 | case OO_Caret: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5463 | BOK = BO_Xor; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5464 | break; |
| 5465 | case OO_AmpAmp: |
| 5466 | BOK = BO_LAnd; |
| 5467 | break; |
| 5468 | case OO_PipePipe: |
| 5469 | BOK = BO_LOr; |
| 5470 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5471 | case OO_New: |
| 5472 | case OO_Delete: |
| 5473 | case OO_Array_New: |
| 5474 | case OO_Array_Delete: |
| 5475 | case OO_Slash: |
| 5476 | case OO_Percent: |
| 5477 | case OO_Tilde: |
| 5478 | case OO_Exclaim: |
| 5479 | case OO_Equal: |
| 5480 | case OO_Less: |
| 5481 | case OO_Greater: |
| 5482 | case OO_LessEqual: |
| 5483 | case OO_GreaterEqual: |
| 5484 | case OO_PlusEqual: |
| 5485 | case OO_MinusEqual: |
| 5486 | case OO_StarEqual: |
| 5487 | case OO_SlashEqual: |
| 5488 | case OO_PercentEqual: |
| 5489 | case OO_CaretEqual: |
| 5490 | case OO_AmpEqual: |
| 5491 | case OO_PipeEqual: |
| 5492 | case OO_LessLess: |
| 5493 | case OO_GreaterGreater: |
| 5494 | case OO_LessLessEqual: |
| 5495 | case OO_GreaterGreaterEqual: |
| 5496 | case OO_EqualEqual: |
| 5497 | case OO_ExclaimEqual: |
| 5498 | case OO_PlusPlus: |
| 5499 | case OO_MinusMinus: |
| 5500 | case OO_Comma: |
| 5501 | case OO_ArrowStar: |
| 5502 | case OO_Arrow: |
| 5503 | case OO_Call: |
| 5504 | case OO_Subscript: |
| 5505 | case OO_Conditional: |
| 5506 | case NUM_OVERLOADED_OPERATORS: |
| 5507 | llvm_unreachable("Unexpected reduction identifier"); |
| 5508 | case OO_None: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5509 | if (auto II = DN.getAsIdentifierInfo()) { |
| 5510 | if (II->isStr("max")) |
| 5511 | BOK = BO_GT; |
| 5512 | else if (II->isStr("min")) |
| 5513 | BOK = BO_LT; |
| 5514 | } |
| 5515 | break; |
| 5516 | } |
| 5517 | SourceRange ReductionIdRange; |
| 5518 | if (ReductionIdScopeSpec.isValid()) { |
| 5519 | ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); |
| 5520 | } |
| 5521 | ReductionIdRange.setEnd(ReductionId.getEndLoc()); |
| 5522 | if (BOK == BO_Comma) { |
| 5523 | // Not allowed reduction identifier is found. |
| 5524 | Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier) |
| 5525 | << ReductionIdRange; |
| 5526 | return nullptr; |
| 5527 | } |
| 5528 | |
| 5529 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5530 | SmallVector<Expr *, 8> LHSs; |
| 5531 | SmallVector<Expr *, 8> RHSs; |
| 5532 | SmallVector<Expr *, 8> ReductionOps; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5533 | for (auto RefExpr : VarList) { |
| 5534 | assert(RefExpr && "nullptr expr in OpenMP reduction clause."); |
| 5535 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
| 5536 | // It will be analyzed later. |
| 5537 | Vars.push_back(RefExpr); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5538 | LHSs.push_back(nullptr); |
| 5539 | RHSs.push_back(nullptr); |
| 5540 | ReductionOps.push_back(nullptr); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5541 | continue; |
| 5542 | } |
| 5543 | |
| 5544 | if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || |
| 5545 | RefExpr->isInstantiationDependent() || |
| 5546 | RefExpr->containsUnexpandedParameterPack()) { |
| 5547 | // It will be analyzed later. |
| 5548 | Vars.push_back(RefExpr); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5549 | LHSs.push_back(nullptr); |
| 5550 | RHSs.push_back(nullptr); |
| 5551 | ReductionOps.push_back(nullptr); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5552 | continue; |
| 5553 | } |
| 5554 | |
| 5555 | auto ELoc = RefExpr->getExprLoc(); |
| 5556 | auto ERange = RefExpr->getSourceRange(); |
| 5557 | // OpenMP [2.1, C/C++] |
| 5558 | // A list item is a variable or array section, subject to the restrictions |
| 5559 | // specified in Section 2.4 on page 42 and in each of the sections |
| 5560 | // describing clauses and directives for which a list appears. |
| 5561 | // OpenMP [2.14.3.3, Restrictions, p.1] |
| 5562 | // A variable that is part of another variable (as an array or |
| 5563 | // structure element) cannot appear in a private clause. |
| 5564 | auto DE = dyn_cast<DeclRefExpr>(RefExpr); |
| 5565 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
| 5566 | Diag(ELoc, diag::err_omp_expected_var_name) << ERange; |
| 5567 | continue; |
| 5568 | } |
| 5569 | auto D = DE->getDecl(); |
| 5570 | auto VD = cast<VarDecl>(D); |
| 5571 | auto Type = VD->getType(); |
| 5572 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 5573 | // A variable that appears in a private clause must not have an incomplete |
| 5574 | // type or a reference type. |
| 5575 | if (RequireCompleteType(ELoc, Type, |
| 5576 | diag::err_omp_reduction_incomplete_type)) |
| 5577 | continue; |
| 5578 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
| 5579 | // Arrays may not appear in a reduction clause. |
| 5580 | if (Type.getNonReferenceType()->isArrayType()) { |
| 5581 | Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange; |
| 5582 | bool IsDecl = |
| 5583 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5584 | Diag(VD->getLocation(), |
| 5585 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5586 | << VD; |
| 5587 | continue; |
| 5588 | } |
| 5589 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
| 5590 | // A list item that appears in a reduction clause must not be |
| 5591 | // const-qualified. |
| 5592 | if (Type.getNonReferenceType().isConstant(Context)) { |
| 5593 | Diag(ELoc, diag::err_omp_const_variable) |
| 5594 | << getOpenMPClauseName(OMPC_reduction) << Type << ERange; |
| 5595 | bool IsDecl = |
| 5596 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5597 | Diag(VD->getLocation(), |
| 5598 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5599 | << VD; |
| 5600 | continue; |
| 5601 | } |
| 5602 | // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] |
| 5603 | // If a list-item is a reference type then it must bind to the same object |
| 5604 | // for all threads of the team. |
| 5605 | VarDecl *VDDef = VD->getDefinition(); |
| 5606 | if (Type->isReferenceType() && VDDef) { |
| 5607 | DSARefChecker Check(DSAStack); |
| 5608 | if (Check.Visit(VDDef->getInit())) { |
| 5609 | Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; |
| 5610 | Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; |
| 5611 | continue; |
| 5612 | } |
| 5613 | } |
| 5614 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
| 5615 | // The type of a list item that appears in a reduction clause must be valid |
| 5616 | // for the reduction-identifier. For a max or min reduction in C, the type |
| 5617 | // of the list item must be an allowed arithmetic data type: char, int, |
| 5618 | // float, double, or _Bool, possibly modified with long, short, signed, or |
| 5619 | // unsigned. For a max or min reduction in C++, the type of the list item |
| 5620 | // must be an allowed arithmetic data type: char, wchar_t, int, float, |
| 5621 | // double, or bool, possibly modified with long, short, signed, or unsigned. |
| 5622 | if ((BOK == BO_GT || BOK == BO_LT) && |
| 5623 | !(Type->isScalarType() || |
| 5624 | (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { |
| 5625 | Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) |
| 5626 | << getLangOpts().CPlusPlus; |
| 5627 | bool IsDecl = |
| 5628 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5629 | Diag(VD->getLocation(), |
| 5630 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5631 | << VD; |
| 5632 | continue; |
| 5633 | } |
| 5634 | if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && |
| 5635 | !getLangOpts().CPlusPlus && Type->isFloatingType()) { |
| 5636 | Diag(ELoc, diag::err_omp_clause_floating_type_arg); |
| 5637 | bool IsDecl = |
| 5638 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5639 | Diag(VD->getLocation(), |
| 5640 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5641 | << VD; |
| 5642 | continue; |
| 5643 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5644 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 5645 | // in a Construct] |
| 5646 | // Variables with the predetermined data-sharing attributes may not be |
| 5647 | // listed in data-sharing attributes clauses, except for the cases |
| 5648 | // listed below. For these exceptions only, listing a predetermined |
| 5649 | // variable in a data-sharing attribute clause is allowed and overrides |
| 5650 | // the variable's predetermined data-sharing attributes. |
| 5651 | // OpenMP [2.14.3.6, Restrictions, p.3] |
| 5652 | // Any number of reduction clauses can be specified on the directive, |
| 5653 | // but a list item can appear only once in the reduction clauses for that |
| 5654 | // directive. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5655 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5656 | if (DVar.CKind == OMPC_reduction) { |
| 5657 | Diag(ELoc, diag::err_omp_once_referenced) |
| 5658 | << getOpenMPClauseName(OMPC_reduction); |
| 5659 | if (DVar.RefExpr) { |
| 5660 | Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); |
| 5661 | } |
| 5662 | } else if (DVar.CKind != OMPC_unknown) { |
| 5663 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 5664 | << getOpenMPClauseName(DVar.CKind) |
| 5665 | << getOpenMPClauseName(OMPC_reduction); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 5666 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5667 | continue; |
| 5668 | } |
| 5669 | |
| 5670 | // OpenMP [2.14.3.6, Restrictions, p.1] |
| 5671 | // A list item that appears in a reduction clause of a worksharing |
| 5672 | // construct must be shared in the parallel regions to which any of the |
| 5673 | // worksharing regions arising from the worksharing construct bind. |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5674 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 5675 | if (isOpenMPWorksharingDirective(CurrDir) && |
| 5676 | !isOpenMPParallelDirective(CurrDir)) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5677 | DVar = DSAStack->getImplicitDSA(VD, true); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5678 | if (DVar.CKind != OMPC_shared) { |
| 5679 | Diag(ELoc, diag::err_omp_required_access) |
| 5680 | << getOpenMPClauseName(OMPC_reduction) |
| 5681 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 5682 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5683 | continue; |
| 5684 | } |
| 5685 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5686 | Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5687 | auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs"); |
| 5688 | auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5689 | // Add initializer for private variable. |
| 5690 | Expr *Init = nullptr; |
| 5691 | switch (BOK) { |
| 5692 | case BO_Add: |
| 5693 | case BO_Xor: |
| 5694 | case BO_Or: |
| 5695 | case BO_LOr: |
| 5696 | // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. |
| 5697 | if (Type->isScalarType() || Type->isAnyComplexType()) { |
| 5698 | Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get(); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5699 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5700 | break; |
| 5701 | case BO_Mul: |
| 5702 | case BO_LAnd: |
| 5703 | if (Type->isScalarType() || Type->isAnyComplexType()) { |
| 5704 | // '*' and '&&' reduction ops - initializer is '1'. |
| 5705 | Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get(); |
| 5706 | } |
| 5707 | break; |
| 5708 | case BO_And: { |
| 5709 | // '&' reduction op - initializer is '~0'. |
| 5710 | QualType OrigType = Type; |
| 5711 | if (auto *ComplexTy = OrigType->getAs<ComplexType>()) { |
| 5712 | Type = ComplexTy->getElementType(); |
| 5713 | } |
| 5714 | if (Type->isRealFloatingType()) { |
| 5715 | llvm::APFloat InitValue = |
| 5716 | llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), |
| 5717 | /*isIEEE=*/true); |
| 5718 | Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, |
| 5719 | Type, ELoc); |
| 5720 | } else if (Type->isScalarType()) { |
| 5721 | auto Size = Context.getTypeSize(Type); |
| 5722 | QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); |
| 5723 | llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); |
| 5724 | Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); |
| 5725 | } |
| 5726 | if (Init && OrigType->isAnyComplexType()) { |
| 5727 | // Init = 0xFFFF + 0xFFFFi; |
| 5728 | auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); |
| 5729 | Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); |
| 5730 | } |
| 5731 | Type = OrigType; |
| 5732 | break; |
| 5733 | } |
| 5734 | case BO_LT: |
| 5735 | case BO_GT: { |
| 5736 | // 'min' reduction op - initializer is 'Largest representable number in |
| 5737 | // the reduction list item type'. |
| 5738 | // 'max' reduction op - initializer is 'Least representable number in |
| 5739 | // the reduction list item type'. |
| 5740 | if (Type->isIntegerType() || Type->isPointerType()) { |
| 5741 | bool IsSigned = Type->hasSignedIntegerRepresentation(); |
| 5742 | auto Size = Context.getTypeSize(Type); |
| 5743 | QualType IntTy = |
| 5744 | Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); |
| 5745 | llvm::APInt InitValue = |
| 5746 | (BOK != BO_LT) |
| 5747 | ? IsSigned ? llvm::APInt::getSignedMinValue(Size) |
| 5748 | : llvm::APInt::getMinValue(Size) |
| 5749 | : IsSigned ? llvm::APInt::getSignedMaxValue(Size) |
| 5750 | : llvm::APInt::getMaxValue(Size); |
| 5751 | Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); |
| 5752 | if (Type->isPointerType()) { |
| 5753 | // Cast to pointer type. |
| 5754 | auto CastExpr = BuildCStyleCastExpr( |
| 5755 | SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc), |
| 5756 | SourceLocation(), Init); |
| 5757 | if (CastExpr.isInvalid()) |
| 5758 | continue; |
| 5759 | Init = CastExpr.get(); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5760 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5761 | } else if (Type->isRealFloatingType()) { |
| 5762 | llvm::APFloat InitValue = llvm::APFloat::getLargest( |
| 5763 | Context.getFloatTypeSemantics(Type), BOK != BO_LT); |
| 5764 | Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, |
| 5765 | Type, ELoc); |
| 5766 | } |
| 5767 | break; |
| 5768 | } |
| 5769 | case BO_PtrMemD: |
| 5770 | case BO_PtrMemI: |
| 5771 | case BO_MulAssign: |
| 5772 | case BO_Div: |
| 5773 | case BO_Rem: |
| 5774 | case BO_Sub: |
| 5775 | case BO_Shl: |
| 5776 | case BO_Shr: |
| 5777 | case BO_LE: |
| 5778 | case BO_GE: |
| 5779 | case BO_EQ: |
| 5780 | case BO_NE: |
| 5781 | case BO_AndAssign: |
| 5782 | case BO_XorAssign: |
| 5783 | case BO_OrAssign: |
| 5784 | case BO_Assign: |
| 5785 | case BO_AddAssign: |
| 5786 | case BO_SubAssign: |
| 5787 | case BO_DivAssign: |
| 5788 | case BO_RemAssign: |
| 5789 | case BO_ShlAssign: |
| 5790 | case BO_ShrAssign: |
| 5791 | case BO_Comma: |
| 5792 | llvm_unreachable("Unexpected reduction operation"); |
| 5793 | } |
| 5794 | if (Init) { |
| 5795 | AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false, |
| 5796 | /*TypeMayContainAuto=*/false); |
| 5797 | } else { |
| 5798 | ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false); |
| 5799 | } |
| 5800 | if (!RHSVD->hasInit()) { |
| 5801 | Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type |
| 5802 | << ReductionIdRange; |
| 5803 | bool IsDecl = |
| 5804 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5805 | Diag(VD->getLocation(), |
| 5806 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5807 | << VD; |
| 5808 | continue; |
| 5809 | } |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5810 | auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc); |
| 5811 | auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5812 | ExprResult ReductionOp = |
| 5813 | BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK, |
| 5814 | LHSDRE, RHSDRE); |
| 5815 | if (ReductionOp.isUsable()) { |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5816 | if (BOK != BO_LT && BOK != BO_GT) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5817 | ReductionOp = |
| 5818 | BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), |
| 5819 | BO_Assign, LHSDRE, ReductionOp.get()); |
| 5820 | } else { |
| 5821 | auto *ConditionalOp = new (Context) ConditionalOperator( |
| 5822 | ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(), |
| 5823 | RHSDRE, Type, VK_LValue, OK_Ordinary); |
| 5824 | ReductionOp = |
| 5825 | BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), |
| 5826 | BO_Assign, LHSDRE, ConditionalOp); |
| 5827 | } |
| 5828 | if (ReductionOp.isUsable()) { |
| 5829 | ReductionOp = ActOnFinishFullExpr(ReductionOp.get()); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5830 | } |
| 5831 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5832 | if (ReductionOp.isInvalid()) |
| 5833 | continue; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5834 | |
| 5835 | DSAStack->addDSA(VD, DE, OMPC_reduction); |
| 5836 | Vars.push_back(DE); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5837 | LHSs.push_back(LHSDRE); |
| 5838 | RHSs.push_back(RHSDRE); |
| 5839 | ReductionOps.push_back(ReductionOp.get()); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | if (Vars.empty()) |
| 5843 | return nullptr; |
| 5844 | |
| 5845 | return OMPReductionClause::Create( |
| 5846 | Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5847 | ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs, |
| 5848 | RHSs, ReductionOps); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 5849 | } |
| 5850 | |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5851 | OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step, |
| 5852 | SourceLocation StartLoc, |
| 5853 | SourceLocation LParenLoc, |
| 5854 | SourceLocation ColonLoc, |
| 5855 | SourceLocation EndLoc) { |
| 5856 | SmallVector<Expr *, 8> Vars; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5857 | SmallVector<Expr *, 8> Inits; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5858 | for (auto &RefExpr : VarList) { |
| 5859 | assert(RefExpr && "NULL expr in OpenMP linear clause."); |
| 5860 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5861 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5862 | Vars.push_back(RefExpr); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5863 | Inits.push_back(nullptr); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5864 | continue; |
| 5865 | } |
| 5866 | |
| 5867 | // OpenMP [2.14.3.7, linear clause] |
| 5868 | // A list item that appears in a linear clause is subject to the private |
| 5869 | // clause semantics described in Section 2.14.3.3 on page 159 except as |
| 5870 | // noted. In addition, the value of the new list item on each iteration |
| 5871 | // of the associated loop(s) corresponds to the value of the original |
| 5872 | // list item before entering the construct plus the logical number of |
| 5873 | // the iteration times linear-step. |
| 5874 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5875 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5876 | // OpenMP [2.1, C/C++] |
| 5877 | // A list item is a variable name. |
| 5878 | // OpenMP [2.14.3.3, Restrictions, p.1] |
| 5879 | // A variable that is part of another variable (as an array or |
| 5880 | // structure element) cannot appear in a private clause. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5881 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5882 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 5883 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5884 | continue; |
| 5885 | } |
| 5886 | |
| 5887 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 5888 | |
| 5889 | // OpenMP [2.14.3.7, linear clause] |
| 5890 | // A list-item cannot appear in more than one linear clause. |
| 5891 | // A list-item that appears in a linear clause cannot appear in any |
| 5892 | // other data-sharing attribute clause. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5893 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5894 | if (DVar.RefExpr) { |
| 5895 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 5896 | << getOpenMPClauseName(OMPC_linear); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 5897 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5898 | continue; |
| 5899 | } |
| 5900 | |
| 5901 | QualType QType = VD->getType(); |
| 5902 | if (QType->isDependentType() || QType->isInstantiationDependentType()) { |
| 5903 | // It will be analyzed later. |
| 5904 | Vars.push_back(DE); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5905 | Inits.push_back(nullptr); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5906 | continue; |
| 5907 | } |
| 5908 | |
| 5909 | // A variable must not have an incomplete type or a reference type. |
| 5910 | if (RequireCompleteType(ELoc, QType, |
| 5911 | diag::err_omp_linear_incomplete_type)) { |
| 5912 | continue; |
| 5913 | } |
| 5914 | if (QType->isReferenceType()) { |
| 5915 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
| 5916 | << getOpenMPClauseName(OMPC_linear) << QType; |
| 5917 | bool IsDecl = |
| 5918 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5919 | Diag(VD->getLocation(), |
| 5920 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5921 | << VD; |
| 5922 | continue; |
| 5923 | } |
| 5924 | |
| 5925 | // A list item must not be const-qualified. |
| 5926 | if (QType.isConstant(Context)) { |
| 5927 | Diag(ELoc, diag::err_omp_const_variable) |
| 5928 | << getOpenMPClauseName(OMPC_linear); |
| 5929 | bool IsDecl = |
| 5930 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5931 | Diag(VD->getLocation(), |
| 5932 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5933 | << VD; |
| 5934 | continue; |
| 5935 | } |
| 5936 | |
| 5937 | // A list item must be of integral or pointer type. |
| 5938 | QType = QType.getUnqualifiedType().getCanonicalType(); |
| 5939 | const Type *Ty = QType.getTypePtrOrNull(); |
| 5940 | if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && |
| 5941 | !Ty->isPointerType())) { |
| 5942 | Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType; |
| 5943 | bool IsDecl = |
| 5944 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 5945 | Diag(VD->getLocation(), |
| 5946 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 5947 | << VD; |
| 5948 | continue; |
| 5949 | } |
| 5950 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5951 | // Build var to save initial value. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 5952 | VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start"); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5953 | AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(), |
| 5954 | /*DirectInit*/ false, /*TypeMayContainAuto*/ false); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 5955 | auto InitRef = buildDeclRefExpr( |
| 5956 | *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc()); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5957 | DSAStack->addDSA(VD, DE, OMPC_linear); |
| 5958 | Vars.push_back(DE); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5959 | Inits.push_back(InitRef); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5960 | } |
| 5961 | |
| 5962 | if (Vars.empty()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 5963 | return nullptr; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5964 | |
| 5965 | Expr *StepExpr = Step; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5966 | Expr *CalcStepExpr = nullptr; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5967 | if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && |
| 5968 | !Step->isInstantiationDependent() && |
| 5969 | !Step->containsUnexpandedParameterPack()) { |
| 5970 | SourceLocation StepLoc = Step->getLocStart(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 5971 | ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5972 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 5973 | return nullptr; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5974 | StepExpr = Val.get(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5975 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5976 | // Build var to save the step value. |
| 5977 | VarDecl *SaveVar = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5978 | buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5979 | ExprResult SaveRef = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 5980 | buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5981 | ExprResult CalcStep = |
| 5982 | BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); |
| 5983 | |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5984 | // Warn about zero linear step (it would be probably better specified as |
| 5985 | // making corresponding variables 'const'). |
| 5986 | llvm::APSInt Result; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5987 | bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); |
| 5988 | if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5989 | Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] |
| 5990 | << (Vars.size() > 1); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5991 | if (!IsConstant && CalcStep.isUsable()) { |
| 5992 | // Calculate the step beforehand instead of doing this on each iteration. |
| 5993 | // (This is not used if the number of iterations may be kfold-ed). |
| 5994 | CalcStepExpr = CalcStep.get(); |
| 5995 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
| 5998 | return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc, |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 5999 | Vars, Inits, StepExpr, CalcStepExpr); |
| 6000 | } |
| 6001 | |
| 6002 | static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, |
| 6003 | Expr *NumIterations, Sema &SemaRef, |
| 6004 | Scope *S) { |
| 6005 | // Walk the vars and build update/final expressions for the CodeGen. |
| 6006 | SmallVector<Expr *, 8> Updates; |
| 6007 | SmallVector<Expr *, 8> Finals; |
| 6008 | Expr *Step = Clause.getStep(); |
| 6009 | Expr *CalcStep = Clause.getCalcStep(); |
| 6010 | // OpenMP [2.14.3.7, linear clause] |
| 6011 | // If linear-step is not specified it is assumed to be 1. |
| 6012 | if (Step == nullptr) |
| 6013 | Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); |
| 6014 | else if (CalcStep) |
| 6015 | Step = cast<BinaryOperator>(CalcStep)->getLHS(); |
| 6016 | bool HasErrors = false; |
| 6017 | auto CurInit = Clause.inits().begin(); |
| 6018 | for (auto &RefExpr : Clause.varlists()) { |
| 6019 | Expr *InitExpr = *CurInit; |
| 6020 | |
| 6021 | // Build privatized reference to the current linear var. |
| 6022 | auto DE = cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 6023 | auto PrivateRef = |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 6024 | buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), |
| 6025 | DE->getType().getUnqualifiedType(), DE->getExprLoc(), |
| 6026 | /*RefersToCapture=*/true); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 6027 | |
| 6028 | // Build update: Var = InitExpr + IV * Step |
| 6029 | ExprResult Update = |
| 6030 | BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef, |
| 6031 | InitExpr, IV, Step, /* Subtract */ false); |
| 6032 | Update = SemaRef.ActOnFinishFullExpr(Update.get()); |
| 6033 | |
| 6034 | // Build final: Var = InitExpr + NumIterations * Step |
| 6035 | ExprResult Final = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 6036 | BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef, |
| 6037 | InitExpr, NumIterations, Step, /* Subtract */ false); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 6038 | Final = SemaRef.ActOnFinishFullExpr(Final.get()); |
| 6039 | if (!Update.isUsable() || !Final.isUsable()) { |
| 6040 | Updates.push_back(nullptr); |
| 6041 | Finals.push_back(nullptr); |
| 6042 | HasErrors = true; |
| 6043 | } else { |
| 6044 | Updates.push_back(Update.get()); |
| 6045 | Finals.push_back(Final.get()); |
| 6046 | } |
| 6047 | ++CurInit; |
| 6048 | } |
| 6049 | Clause.setUpdates(Updates); |
| 6050 | Clause.setFinals(Finals); |
| 6051 | return HasErrors; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 6054 | OMPClause *Sema::ActOnOpenMPAlignedClause( |
| 6055 | ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, |
| 6056 | SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { |
| 6057 | |
| 6058 | SmallVector<Expr *, 8> Vars; |
| 6059 | for (auto &RefExpr : VarList) { |
| 6060 | assert(RefExpr && "NULL expr in OpenMP aligned clause."); |
| 6061 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
| 6062 | // It will be analyzed later. |
| 6063 | Vars.push_back(RefExpr); |
| 6064 | continue; |
| 6065 | } |
| 6066 | |
| 6067 | SourceLocation ELoc = RefExpr->getExprLoc(); |
| 6068 | // OpenMP [2.1, C/C++] |
| 6069 | // A list item is a variable name. |
| 6070 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
| 6071 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
| 6072 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
| 6073 | continue; |
| 6074 | } |
| 6075 | |
| 6076 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 6077 | |
| 6078 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 6079 | // The type of list items appearing in the aligned clause must be |
| 6080 | // array, pointer, reference to array, or reference to pointer. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 6081 | QualType QType = VD->getType(); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 6082 | QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 6083 | const Type *Ty = QType.getTypePtrOrNull(); |
| 6084 | if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() && |
| 6085 | !Ty->isPointerType())) { |
| 6086 | Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) |
| 6087 | << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange(); |
| 6088 | bool IsDecl = |
| 6089 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 6090 | Diag(VD->getLocation(), |
| 6091 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 6092 | << VD; |
| 6093 | continue; |
| 6094 | } |
| 6095 | |
| 6096 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 6097 | // A list-item cannot appear in more than one aligned clause. |
| 6098 | if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) { |
| 6099 | Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange(); |
| 6100 | Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) |
| 6101 | << getOpenMPClauseName(OMPC_aligned); |
| 6102 | continue; |
| 6103 | } |
| 6104 | |
| 6105 | Vars.push_back(DE); |
| 6106 | } |
| 6107 | |
| 6108 | // OpenMP [2.8.1, simd construct, Description] |
| 6109 | // The parameter of the aligned clause, alignment, must be a constant |
| 6110 | // positive integer expression. |
| 6111 | // If no optional parameter is specified, implementation-defined default |
| 6112 | // alignments for SIMD instructions on the target platforms are assumed. |
| 6113 | if (Alignment != nullptr) { |
| 6114 | ExprResult AlignResult = |
| 6115 | VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); |
| 6116 | if (AlignResult.isInvalid()) |
| 6117 | return nullptr; |
| 6118 | Alignment = AlignResult.get(); |
| 6119 | } |
| 6120 | if (Vars.empty()) |
| 6121 | return nullptr; |
| 6122 | |
| 6123 | return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, |
| 6124 | EndLoc, Vars, Alignment); |
| 6125 | } |
| 6126 | |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6127 | OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, |
| 6128 | SourceLocation StartLoc, |
| 6129 | SourceLocation LParenLoc, |
| 6130 | SourceLocation EndLoc) { |
| 6131 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 6132 | SmallVector<Expr *, 8> SrcExprs; |
| 6133 | SmallVector<Expr *, 8> DstExprs; |
| 6134 | SmallVector<Expr *, 8> AssignmentOps; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6135 | for (auto &RefExpr : VarList) { |
| 6136 | assert(RefExpr && "NULL expr in OpenMP copyin clause."); |
| 6137 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6138 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6139 | Vars.push_back(RefExpr); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 6140 | SrcExprs.push_back(nullptr); |
| 6141 | DstExprs.push_back(nullptr); |
| 6142 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6143 | continue; |
| 6144 | } |
| 6145 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6146 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6147 | // OpenMP [2.1, C/C++] |
| 6148 | // A list item is a variable name. |
| 6149 | // OpenMP [2.14.4.1, Restrictions, p.1] |
| 6150 | // A list item that appears in a copyin clause must be threadprivate. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6151 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6152 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6153 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6154 | continue; |
| 6155 | } |
| 6156 | |
| 6157 | Decl *D = DE->getDecl(); |
| 6158 | VarDecl *VD = cast<VarDecl>(D); |
| 6159 | |
| 6160 | QualType Type = VD->getType(); |
| 6161 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 6162 | // It will be analyzed later. |
| 6163 | Vars.push_back(DE); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 6164 | SrcExprs.push_back(nullptr); |
| 6165 | DstExprs.push_back(nullptr); |
| 6166 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6167 | continue; |
| 6168 | } |
| 6169 | |
| 6170 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] |
| 6171 | // A list item that appears in a copyin clause must be threadprivate. |
| 6172 | if (!DSAStack->isThreadPrivate(VD)) { |
| 6173 | Diag(ELoc, diag::err_omp_required_access) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6174 | << getOpenMPClauseName(OMPC_copyin) |
| 6175 | << getOpenMPDirectiveName(OMPD_threadprivate); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6176 | continue; |
| 6177 | } |
| 6178 | |
| 6179 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] |
| 6180 | // A variable of class type (or array thereof) that appears in a |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 6181 | // copyin clause requires an accessible, unambiguous copy assignment |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6182 | // operator for the class type. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 6183 | auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 6184 | auto *SrcVD = buildVarDecl(*this, DE->getLocStart(), |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 6185 | ElemType.getUnqualifiedType(), ".copyin.src"); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 6186 | auto *PseudoSrcExpr = buildDeclRefExpr( |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 6187 | *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); |
| 6188 | auto *DstVD = |
| 6189 | buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst"); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 6190 | auto *PseudoDstExpr = |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 6191 | buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 6192 | // For arrays generate assignment operation for single element and replace |
| 6193 | // it by the original array element in CodeGen. |
| 6194 | auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, |
| 6195 | PseudoDstExpr, PseudoSrcExpr); |
| 6196 | if (AssignmentOp.isInvalid()) |
| 6197 | continue; |
| 6198 | AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), |
| 6199 | /*DiscardedValue=*/true); |
| 6200 | if (AssignmentOp.isInvalid()) |
| 6201 | continue; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6202 | |
| 6203 | DSAStack->addDSA(VD, DE, OMPC_copyin); |
| 6204 | Vars.push_back(DE); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 6205 | SrcExprs.push_back(PseudoSrcExpr); |
| 6206 | DstExprs.push_back(PseudoDstExpr); |
| 6207 | AssignmentOps.push_back(AssignmentOp.get()); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6208 | } |
| 6209 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6210 | if (Vars.empty()) |
| 6211 | return nullptr; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6212 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 6213 | return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, |
| 6214 | SrcExprs, DstExprs, AssignmentOps); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6215 | } |
| 6216 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6217 | OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, |
| 6218 | SourceLocation StartLoc, |
| 6219 | SourceLocation LParenLoc, |
| 6220 | SourceLocation EndLoc) { |
| 6221 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 6222 | SmallVector<Expr *, 8> SrcExprs; |
| 6223 | SmallVector<Expr *, 8> DstExprs; |
| 6224 | SmallVector<Expr *, 8> AssignmentOps; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6225 | for (auto &RefExpr : VarList) { |
| 6226 | assert(RefExpr && "NULL expr in OpenMP copyprivate clause."); |
| 6227 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
| 6228 | // It will be analyzed later. |
| 6229 | Vars.push_back(RefExpr); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 6230 | SrcExprs.push_back(nullptr); |
| 6231 | DstExprs.push_back(nullptr); |
| 6232 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6233 | continue; |
| 6234 | } |
| 6235 | |
| 6236 | SourceLocation ELoc = RefExpr->getExprLoc(); |
| 6237 | // OpenMP [2.1, C/C++] |
| 6238 | // A list item is a variable name. |
| 6239 | // OpenMP [2.14.4.1, Restrictions, p.1] |
| 6240 | // A list item that appears in a copyin clause must be threadprivate. |
| 6241 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
| 6242 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
| 6243 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
| 6244 | continue; |
| 6245 | } |
| 6246 | |
| 6247 | Decl *D = DE->getDecl(); |
| 6248 | VarDecl *VD = cast<VarDecl>(D); |
| 6249 | |
| 6250 | QualType Type = VD->getType(); |
| 6251 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 6252 | // It will be analyzed later. |
| 6253 | Vars.push_back(DE); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 6254 | SrcExprs.push_back(nullptr); |
| 6255 | DstExprs.push_back(nullptr); |
| 6256 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6257 | continue; |
| 6258 | } |
| 6259 | |
| 6260 | // OpenMP [2.14.4.2, Restrictions, p.2] |
| 6261 | // A list item that appears in a copyprivate clause may not appear in a |
| 6262 | // private or firstprivate clause on the single construct. |
| 6263 | if (!DSAStack->isThreadPrivate(VD)) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 6264 | auto DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 6265 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && |
| 6266 | DVar.RefExpr) { |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6267 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 6268 | << getOpenMPClauseName(DVar.CKind) |
| 6269 | << getOpenMPClauseName(OMPC_copyprivate); |
| 6270 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
| 6271 | continue; |
| 6272 | } |
| 6273 | |
| 6274 | // OpenMP [2.11.4.2, Restrictions, p.1] |
| 6275 | // All list items that appear in a copyprivate clause must be either |
| 6276 | // threadprivate or private in the enclosing context. |
| 6277 | if (DVar.CKind == OMPC_unknown) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 6278 | DVar = DSAStack->getImplicitDSA(VD, false); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6279 | if (DVar.CKind == OMPC_shared) { |
| 6280 | Diag(ELoc, diag::err_omp_required_access) |
| 6281 | << getOpenMPClauseName(OMPC_copyprivate) |
| 6282 | << "threadprivate or private in the enclosing context"; |
| 6283 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
| 6284 | continue; |
| 6285 | } |
| 6286 | } |
| 6287 | } |
| 6288 | |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 6289 | // Variably modified types are not supported. |
Alexey Bataev | 5129d3a | 2015-05-21 09:47:46 +0000 | [diff] [blame] | 6290 | if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 6291 | Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 6292 | << getOpenMPClauseName(OMPC_copyprivate) << Type |
| 6293 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 6294 | bool IsDecl = |
| 6295 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 6296 | Diag(VD->getLocation(), |
| 6297 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 6298 | << VD; |
| 6299 | continue; |
| 6300 | } |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 6301 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6302 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] |
| 6303 | // A variable of class type (or array thereof) that appears in a |
| 6304 | // copyin clause requires an accessible, unambiguous copy assignment |
| 6305 | // operator for the class type. |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 6306 | Type = Context.getBaseElementType(Type).getUnqualifiedType(); |
| 6307 | auto *SrcVD = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 6308 | buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 6309 | auto *PseudoSrcExpr = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 6310 | buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 6311 | auto *DstVD = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 6312 | buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 6313 | auto *PseudoDstExpr = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 6314 | buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 6315 | auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, |
| 6316 | PseudoDstExpr, PseudoSrcExpr); |
| 6317 | if (AssignmentOp.isInvalid()) |
| 6318 | continue; |
| 6319 | AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), |
| 6320 | /*DiscardedValue=*/true); |
| 6321 | if (AssignmentOp.isInvalid()) |
| 6322 | continue; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6323 | |
| 6324 | // No need to mark vars as copyprivate, they are already threadprivate or |
| 6325 | // implicitly private. |
| 6326 | Vars.push_back(DE); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 6327 | SrcExprs.push_back(PseudoSrcExpr); |
| 6328 | DstExprs.push_back(PseudoDstExpr); |
| 6329 | AssignmentOps.push_back(AssignmentOp.get()); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | if (Vars.empty()) |
| 6333 | return nullptr; |
| 6334 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 6335 | return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 6336 | Vars, SrcExprs, DstExprs, AssignmentOps); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6337 | } |
| 6338 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 6339 | OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, |
| 6340 | SourceLocation StartLoc, |
| 6341 | SourceLocation LParenLoc, |
| 6342 | SourceLocation EndLoc) { |
| 6343 | if (VarList.empty()) |
| 6344 | return nullptr; |
| 6345 | |
| 6346 | return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); |
| 6347 | } |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 6348 | |