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 | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 15 | #include "TreeTransform.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclCXX.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclOpenMP.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtCXX.h" |
| 23 | #include "clang/AST/StmtOpenMP.h" |
| 24 | #include "clang/AST/StmtVisitor.h" |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeOrdering.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 26 | #include "clang/Basic/OpenMPKinds.h" |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Initialization.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Lookup.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 31 | #include "clang/Sema/Scope.h" |
| 32 | #include "clang/Sema/ScopeInfo.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 33 | #include "clang/Sema/SemaInternal.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | // Stack of data-sharing attributes for variables |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | namespace { |
| 41 | /// \brief Default data sharing attributes, which can be applied to directive. |
| 42 | enum DefaultDataSharingAttributes { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 43 | DSA_unspecified = 0, /// \brief Data sharing attribute not specified. |
| 44 | DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'. |
| 45 | DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'. |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 46 | }; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 47 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 48 | /// \brief Stack for tracking declarations used in OpenMP directives and |
| 49 | /// clauses and their data-sharing attributes. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 50 | class DSAStackTy final { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 51 | public: |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 52 | struct DSAVarData final { |
| 53 | OpenMPDirectiveKind DKind = OMPD_unknown; |
| 54 | OpenMPClauseKind CKind = OMPC_unknown; |
| 55 | Expr *RefExpr = nullptr; |
| 56 | DeclRefExpr *PrivateCopy = nullptr; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 57 | SourceLocation ImplicitDSALoc; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 58 | DSAVarData() {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 59 | }; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 60 | typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4> |
| 61 | OperatorOffsetTy; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 62 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 63 | private: |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 64 | struct DSAInfo final { |
| 65 | OpenMPClauseKind Attributes = OMPC_unknown; |
| 66 | /// Pointer to a reference expression and a flag which shows that the |
| 67 | /// variable is marked as lastprivate(true) or not (false). |
| 68 | llvm::PointerIntPair<Expr *, 1, bool> RefExpr; |
| 69 | DeclRefExpr *PrivateCopy = nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 70 | }; |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 71 | typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy; |
| 72 | typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy; |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 73 | typedef std::pair<unsigned, VarDecl *> LCDeclInfo; |
| 74 | typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy; |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 75 | /// Struct that associates a component with the clause kind where they are |
| 76 | /// found. |
| 77 | struct MappedExprComponentTy { |
| 78 | OMPClauseMappableExprCommon::MappableExprComponentLists Components; |
| 79 | OpenMPClauseKind Kind = OMPC_unknown; |
| 80 | }; |
| 81 | typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy> |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 82 | MappedExprComponentsTy; |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 83 | typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>> |
| 84 | CriticalsWithHintsTy; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 85 | typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy> |
| 86 | DoacrossDependMapTy; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 87 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 88 | struct SharingMapTy final { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 89 | DeclSAMapTy SharingMap; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 90 | AlignedMapTy AlignedMap; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 91 | MappedExprComponentsTy MappedExprComponents; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 92 | LoopControlVariablesMapTy LCVMap; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 93 | DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 94 | SourceLocation DefaultAttrLoc; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 95 | OpenMPDirectiveKind Directive = OMPD_unknown; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 96 | DeclarationNameInfo DirectiveName; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 97 | Scope *CurScope = nullptr; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 98 | SourceLocation ConstructLoc; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 99 | /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to |
| 100 | /// get the data (loop counters etc.) about enclosing loop-based construct. |
| 101 | /// This data is required during codegen. |
| 102 | DoacrossDependMapTy DoacrossDepends; |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 103 | /// \brief first argument (Expr *) contains optional argument of the |
| 104 | /// 'ordered' clause, the second one is true if the regions has 'ordered' |
| 105 | /// clause, false otherwise. |
| 106 | llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 107 | bool NowaitRegion = false; |
| 108 | bool CancelRegion = false; |
| 109 | unsigned AssociatedLoops = 1; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 110 | SourceLocation InnerTeamsRegionLoc; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 111 | SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 112 | Scope *CurScope, SourceLocation Loc) |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 113 | : Directive(DKind), DirectiveName(Name), CurScope(CurScope), |
| 114 | ConstructLoc(Loc) {} |
| 115 | SharingMapTy() {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Axel Naumann | 323862e | 2016-02-03 10:45:22 +0000 | [diff] [blame] | 118 | typedef SmallVector<SharingMapTy, 4> StackTy; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 119 | |
| 120 | /// \brief Stack of used declaration and their data-sharing attributes. |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 121 | DeclSAMapTy Threadprivates; |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 122 | const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; |
| 123 | SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 124 | /// \brief true, if check for DSA must be from parent directive, false, if |
| 125 | /// from current directive. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 126 | OpenMPClauseKind ClauseKindMode = OMPC_unknown; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 127 | Sema &SemaRef; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 128 | bool ForceCapturing = false; |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 129 | CriticalsWithHintsTy Criticals; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 130 | |
| 131 | typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; |
| 132 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 133 | DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 134 | |
| 135 | /// \brief Checks if the variable is a local for OpenMP region. |
| 136 | bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 137 | |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 138 | bool isStackEmpty() const { |
| 139 | return Stack.empty() || |
| 140 | Stack.back().second != CurrentNonCapturingFunctionScope || |
| 141 | Stack.back().first.empty(); |
| 142 | } |
| 143 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 144 | public: |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 145 | explicit DSAStackTy(Sema &S) : SemaRef(S) {} |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 146 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 147 | bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } |
| 148 | void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 149 | |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 150 | bool isForceVarCapturing() const { return ForceCapturing; } |
| 151 | void setForceVarCapturing(bool V) { ForceCapturing = V; } |
| 152 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 153 | void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 154 | Scope *CurScope, SourceLocation Loc) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 155 | if (Stack.empty() || |
| 156 | Stack.back().second != CurrentNonCapturingFunctionScope) |
| 157 | Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); |
| 158 | Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); |
| 159 | Stack.back().first.back().DefaultAttrLoc = Loc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void pop() { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 163 | assert(!Stack.back().first.empty() && |
| 164 | "Data-sharing attributes stack is empty!"); |
| 165 | Stack.back().first.pop_back(); |
| 166 | } |
| 167 | |
| 168 | /// Start new OpenMP region stack in new non-capturing function. |
| 169 | void pushFunction() { |
| 170 | const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); |
| 171 | assert(!isa<CapturingScopeInfo>(CurFnScope)); |
| 172 | CurrentNonCapturingFunctionScope = CurFnScope; |
| 173 | } |
| 174 | /// Pop region stack for non-capturing function. |
| 175 | void popFunction(const FunctionScopeInfo *OldFSI) { |
| 176 | if (!Stack.empty() && Stack.back().second == OldFSI) { |
| 177 | assert(Stack.back().first.empty()); |
| 178 | Stack.pop_back(); |
| 179 | } |
| 180 | CurrentNonCapturingFunctionScope = nullptr; |
| 181 | for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { |
| 182 | if (!isa<CapturingScopeInfo>(FSI)) { |
| 183 | CurrentNonCapturingFunctionScope = FSI; |
| 184 | break; |
| 185 | } |
| 186 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 189 | void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) { |
| 190 | Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint); |
| 191 | } |
| 192 | const std::pair<OMPCriticalDirective *, llvm::APSInt> |
| 193 | getCriticalWithHint(const DeclarationNameInfo &Name) const { |
| 194 | auto I = Criticals.find(Name.getAsString()); |
| 195 | if (I != Criticals.end()) |
| 196 | return I->second; |
| 197 | return std::make_pair(nullptr, llvm::APSInt()); |
| 198 | } |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 199 | /// \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] | 200 | /// add it and return NULL; otherwise return previous occurrence's expression |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 201 | /// for diagnostics. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 202 | Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 203 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 204 | /// \brief Register specified variable as loop control variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 205 | void addLoopControlVariable(ValueDecl *D, VarDecl *Capture); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 206 | /// \brief Check if the specified variable is a loop control variable for |
| 207 | /// current region. |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 208 | /// \return The index of the loop control variable in the list of associated |
| 209 | /// for-loops (from outer to inner). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 210 | LCDeclInfo isLoopControlVariable(ValueDecl *D); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 211 | /// \brief Check if the specified variable is a loop control variable for |
| 212 | /// parent region. |
| 213 | /// \return The index of the loop control variable in the list of associated |
| 214 | /// for-loops (from outer to inner). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 215 | LCDeclInfo isParentLoopControlVariable(ValueDecl *D); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 216 | /// \brief Get the loop control variable for the I-th loop (or nullptr) in |
| 217 | /// parent directive. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 218 | ValueDecl *getParentLoopControlVariable(unsigned I); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 219 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 220 | /// \brief Adds explicit data sharing attribute to the specified declaration. |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 221 | void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, |
| 222 | DeclRefExpr *PrivateCopy = nullptr); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 223 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 224 | /// \brief Returns data sharing attributes from top of the stack for the |
| 225 | /// specified declaration. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 226 | DSAVarData getTopDSA(ValueDecl *D, bool FromParent); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 227 | /// \brief Returns data-sharing attributes for the specified declaration. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 228 | DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 229 | /// \brief Checks if the specified variables has data-sharing attributes which |
| 230 | /// match specified \a CPred predicate in any directive which matches \a DPred |
| 231 | /// predicate. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 232 | DSAVarData hasDSA(ValueDecl *D, |
| 233 | const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 234 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 235 | bool FromParent); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 236 | /// \brief Checks if the specified variables has data-sharing attributes which |
| 237 | /// match specified \a CPred predicate in any innermost directive which |
| 238 | /// matches \a DPred predicate. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 239 | DSAVarData |
| 240 | hasInnermostDSA(ValueDecl *D, |
| 241 | const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 242 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 243 | bool FromParent); |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 244 | /// \brief Checks if the specified variables has explicit data-sharing |
| 245 | /// attributes which match specified \a CPred predicate at the specified |
| 246 | /// OpenMP region. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 247 | bool hasExplicitDSA(ValueDecl *D, |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 248 | const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 249 | unsigned Level, bool NotLastprivate = false); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 250 | |
| 251 | /// \brief Returns true if the directive at level \Level matches in the |
| 252 | /// specified \a DPred predicate. |
| 253 | bool hasExplicitDirective( |
| 254 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 255 | unsigned Level); |
| 256 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 257 | /// \brief Finds a directive which matches specified \a DPred predicate. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 258 | bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind, |
| 259 | const DeclarationNameInfo &, |
| 260 | SourceLocation)> &DPred, |
| 261 | bool FromParent); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 262 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 263 | /// \brief Returns currently analyzed directive. |
| 264 | OpenMPDirectiveKind getCurrentDirective() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 265 | return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 266 | } |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 267 | /// \brief Returns parent directive. |
| 268 | OpenMPDirectiveKind getParentDirective() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 269 | if (isStackEmpty() || Stack.back().first.size() == 1) |
| 270 | return OMPD_unknown; |
| 271 | return std::next(Stack.back().first.rbegin())->Directive; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 272 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 273 | |
| 274 | /// \brief Set default data sharing attribute to none. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 275 | void setDefaultDSANone(SourceLocation Loc) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 276 | assert(!isStackEmpty()); |
| 277 | Stack.back().first.back().DefaultAttr = DSA_none; |
| 278 | Stack.back().first.back().DefaultAttrLoc = Loc; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 279 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 280 | /// \brief Set default data sharing attribute to shared. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 281 | void setDefaultDSAShared(SourceLocation Loc) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 282 | assert(!isStackEmpty()); |
| 283 | Stack.back().first.back().DefaultAttr = DSA_shared; |
| 284 | Stack.back().first.back().DefaultAttrLoc = Loc; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 285 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 286 | |
| 287 | DefaultDataSharingAttributes getDefaultDSA() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 288 | return isStackEmpty() ? DSA_unspecified |
| 289 | : Stack.back().first.back().DefaultAttr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 290 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 291 | SourceLocation getDefaultDSALocation() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 292 | return isStackEmpty() ? SourceLocation() |
| 293 | : Stack.back().first.back().DefaultAttrLoc; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 294 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 295 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 296 | /// \brief Checks if the specified variable is a threadprivate. |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 297 | bool isThreadPrivate(VarDecl *D) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 298 | DSAVarData DVar = getTopDSA(D, false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 299 | return isOpenMPThreadPrivate(DVar.CKind); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 302 | /// \brief Marks current region as ordered (it has an 'ordered' clause). |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 303 | void setOrderedRegion(bool IsOrdered, Expr *Param) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 304 | assert(!isStackEmpty()); |
| 305 | Stack.back().first.back().OrderedRegion.setInt(IsOrdered); |
| 306 | Stack.back().first.back().OrderedRegion.setPointer(Param); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 307 | } |
| 308 | /// \brief Returns true, if parent region is ordered (has associated |
| 309 | /// 'ordered' clause), false - otherwise. |
| 310 | bool isParentOrderedRegion() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 311 | if (isStackEmpty() || Stack.back().first.size() == 1) |
| 312 | return false; |
| 313 | return std::next(Stack.back().first.rbegin())->OrderedRegion.getInt(); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 314 | } |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 315 | /// \brief Returns optional parameter for the ordered region. |
| 316 | Expr *getParentOrderedRegionParam() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 317 | if (isStackEmpty() || Stack.back().first.size() == 1) |
| 318 | return nullptr; |
| 319 | return std::next(Stack.back().first.rbegin())->OrderedRegion.getPointer(); |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 320 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 321 | /// \brief Marks current region as nowait (it has a 'nowait' clause). |
| 322 | void setNowaitRegion(bool IsNowait = true) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 323 | assert(!isStackEmpty()); |
| 324 | Stack.back().first.back().NowaitRegion = IsNowait; |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 325 | } |
| 326 | /// \brief Returns true, if parent region is nowait (has associated |
| 327 | /// 'nowait' clause), false - otherwise. |
| 328 | bool isParentNowaitRegion() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 329 | if (isStackEmpty() || Stack.back().first.size() == 1) |
| 330 | return false; |
| 331 | return std::next(Stack.back().first.rbegin())->NowaitRegion; |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 332 | } |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 333 | /// \brief Marks parent region as cancel region. |
| 334 | void setParentCancelRegion(bool Cancel = true) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 335 | if (!isStackEmpty() && Stack.back().first.size() > 1) { |
| 336 | auto &StackElemRef = *std::next(Stack.back().first.rbegin()); |
| 337 | StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel; |
| 338 | } |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 339 | } |
| 340 | /// \brief Return true if current region has inner cancel construct. |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 341 | bool isCancelRegion() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 342 | return isStackEmpty() ? false : Stack.back().first.back().CancelRegion; |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 343 | } |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 344 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 345 | /// \brief Set collapse value for the region. |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 346 | void setAssociatedLoops(unsigned Val) { |
| 347 | assert(!isStackEmpty()); |
| 348 | Stack.back().first.back().AssociatedLoops = Val; |
| 349 | } |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 350 | /// \brief Return collapse value for region. |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 351 | unsigned getAssociatedLoops() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 352 | return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops; |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 353 | } |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 354 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 355 | /// \brief Marks current target region as one with closely nested teams |
| 356 | /// region. |
| 357 | void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 358 | if (!isStackEmpty() && Stack.back().first.size() > 1) { |
| 359 | std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc = |
| 360 | TeamsRegionLoc; |
| 361 | } |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 362 | } |
| 363 | /// \brief Returns true, if current region has closely nested teams region. |
| 364 | bool hasInnerTeamsRegion() const { |
| 365 | return getInnerTeamsRegionLoc().isValid(); |
| 366 | } |
| 367 | /// \brief Returns location of the nested teams region (if any). |
| 368 | SourceLocation getInnerTeamsRegionLoc() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 369 | return isStackEmpty() ? SourceLocation() |
| 370 | : Stack.back().first.back().InnerTeamsRegionLoc; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 373 | Scope *getCurScope() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 374 | return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 375 | } |
| 376 | Scope *getCurScope() { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 377 | return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 378 | } |
| 379 | SourceLocation getConstructLoc() { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 380 | return isStackEmpty() ? SourceLocation() |
| 381 | : Stack.back().first.back().ConstructLoc; |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 382 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 383 | |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 384 | /// Do the check specified in \a Check to all component lists and return true |
| 385 | /// if any issue is found. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 386 | bool checkMappableExprComponentListsForDecl( |
| 387 | ValueDecl *VD, bool CurrentRegionOnly, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 388 | const llvm::function_ref< |
| 389 | bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| 390 | OpenMPClauseKind)> &Check) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 391 | if (isStackEmpty()) |
| 392 | return false; |
| 393 | auto SI = Stack.back().first.rbegin(); |
| 394 | auto SE = Stack.back().first.rend(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 395 | |
| 396 | if (SI == SE) |
| 397 | return false; |
| 398 | |
| 399 | if (CurrentRegionOnly) { |
| 400 | SE = std::next(SI); |
| 401 | } else { |
| 402 | ++SI; |
| 403 | } |
| 404 | |
| 405 | for (; SI != SE; ++SI) { |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 406 | auto MI = SI->MappedExprComponents.find(VD); |
| 407 | if (MI != SI->MappedExprComponents.end()) |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 408 | for (auto &L : MI->second.Components) |
| 409 | if (Check(L, MI->second.Kind)) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 410 | return true; |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 411 | } |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 412 | return false; |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 415 | /// Create a new mappable expression component list associated with a given |
| 416 | /// declaration and initialize it with the provided list of components. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 417 | void addMappableExpressionComponents( |
| 418 | ValueDecl *VD, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 419 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
| 420 | OpenMPClauseKind WhereFoundClauseKind) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 421 | assert(!isStackEmpty() && |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 422 | "Not expecting to retrieve components from a empty stack!"); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 423 | auto &MEC = Stack.back().first.back().MappedExprComponents[VD]; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 424 | // Create new entry and append the new components there. |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 425 | MEC.Components.resize(MEC.Components.size() + 1); |
| 426 | MEC.Components.back().append(Components.begin(), Components.end()); |
| 427 | MEC.Kind = WhereFoundClauseKind; |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 428 | } |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 429 | |
| 430 | unsigned getNestingLevel() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 431 | assert(!isStackEmpty()); |
| 432 | return Stack.back().first.size() - 1; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 433 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 434 | void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 435 | assert(!isStackEmpty() && Stack.back().first.size() > 1); |
| 436 | auto &StackElem = *std::next(Stack.back().first.rbegin()); |
| 437 | assert(isOpenMPWorksharingDirective(StackElem.Directive)); |
| 438 | StackElem.DoacrossDepends.insert({C, OpsOffs}); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 439 | } |
| 440 | llvm::iterator_range<DoacrossDependMapTy::const_iterator> |
| 441 | getDoacrossDependClauses() const { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 442 | assert(!isStackEmpty()); |
| 443 | auto &StackElem = Stack.back().first.back(); |
| 444 | if (isOpenMPWorksharingDirective(StackElem.Directive)) { |
| 445 | auto &Ref = StackElem.DoacrossDepends; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 446 | return llvm::make_range(Ref.begin(), Ref.end()); |
| 447 | } |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 448 | return llvm::make_range(StackElem.DoacrossDepends.end(), |
| 449 | StackElem.DoacrossDepends.end()); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 450 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 451 | }; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 452 | bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 453 | return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) || |
| 454 | isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 455 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 456 | } // namespace |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 457 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 458 | static ValueDecl *getCanonicalDecl(ValueDecl *D) { |
| 459 | auto *VD = dyn_cast<VarDecl>(D); |
| 460 | auto *FD = dyn_cast<FieldDecl>(D); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 461 | if (VD != nullptr) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 462 | VD = VD->getCanonicalDecl(); |
| 463 | D = VD; |
| 464 | } else { |
| 465 | assert(FD); |
| 466 | FD = FD->getCanonicalDecl(); |
| 467 | D = FD; |
| 468 | } |
| 469 | return D; |
| 470 | } |
| 471 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 472 | DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 473 | ValueDecl *D) { |
| 474 | D = getCanonicalDecl(D); |
| 475 | auto *VD = dyn_cast<VarDecl>(D); |
| 476 | auto *FD = dyn_cast<FieldDecl>(D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 477 | DSAVarData DVar; |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 478 | if (isStackEmpty() || Iter == Stack.back().first.rend()) { |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 479 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 480 | // in a region but not in construct] |
| 481 | // File-scope or namespace-scope variables referenced in called routines |
| 482 | // in the region are shared unless they appear in a threadprivate |
| 483 | // directive. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 484 | if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D)) |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 485 | DVar.CKind = OMPC_shared; |
| 486 | |
| 487 | // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced |
| 488 | // in a region but not in construct] |
| 489 | // Variables with static storage duration that are declared in called |
| 490 | // routines in the region are shared. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 491 | if (VD && VD->hasGlobalStorage()) |
| 492 | DVar.CKind = OMPC_shared; |
| 493 | |
| 494 | // Non-static data members are shared by default. |
| 495 | if (FD) |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 496 | DVar.CKind = OMPC_shared; |
| 497 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 498 | return DVar; |
| 499 | } |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 500 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 501 | DVar.DKind = Iter->Directive; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 502 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 503 | // in a Construct, C/C++, predetermined, p.1] |
| 504 | // Variables with automatic storage duration that are declared in a scope |
| 505 | // inside the construct are private. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 506 | if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && |
| 507 | (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 508 | DVar.CKind = OMPC_private; |
| 509 | return DVar; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 512 | // Explicitly specified attributes and local variables with predetermined |
| 513 | // attributes. |
| 514 | if (Iter->SharingMap.count(D)) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 515 | DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer(); |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 516 | DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 517 | DVar.CKind = Iter->SharingMap[D].Attributes; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 518 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 519 | return DVar; |
| 520 | } |
| 521 | |
| 522 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 523 | // in a Construct, C/C++, implicitly determined, p.1] |
| 524 | // In a parallel or task construct, the data-sharing attributes of these |
| 525 | // variables are determined by the default clause, if present. |
| 526 | switch (Iter->DefaultAttr) { |
| 527 | case DSA_shared: |
| 528 | DVar.CKind = OMPC_shared; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 529 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 530 | return DVar; |
| 531 | case DSA_none: |
| 532 | return DVar; |
| 533 | case DSA_unspecified: |
| 534 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 535 | // in a Construct, implicitly determined, p.2] |
| 536 | // In a parallel construct, if no default clause is present, these |
| 537 | // variables are shared. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 538 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 539 | if (isOpenMPParallelDirective(DVar.DKind) || |
| 540 | isOpenMPTeamsDirective(DVar.DKind)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 541 | DVar.CKind = OMPC_shared; |
| 542 | return DVar; |
| 543 | } |
| 544 | |
| 545 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 546 | // in a Construct, implicitly determined, p.4] |
| 547 | // In a task construct, if no default clause is present, a variable that in |
| 548 | // the enclosing context is determined to be shared by all implicit tasks |
| 549 | // bound to the current team is shared. |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 550 | if (isOpenMPTaskingDirective(DVar.DKind)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 551 | DSAVarData DVarTemp; |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 552 | auto I = Iter, E = Stack.back().first.rend(); |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 553 | do { |
| 554 | ++I; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 555 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 556 | // Referenced in a Construct, implicitly determined, p.6] |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 557 | // In a task construct, if no default clause is present, a variable |
| 558 | // whose data-sharing attribute is not determined by the rules above is |
| 559 | // firstprivate. |
| 560 | DVarTemp = getDSA(I, D); |
| 561 | if (DVarTemp.CKind != OMPC_shared) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 562 | DVar.RefExpr = nullptr; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 563 | DVar.CKind = OMPC_firstprivate; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 564 | return DVar; |
| 565 | } |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 566 | } while (I != E && !isParallelOrTaskRegion(I->Directive)); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 567 | DVar.CKind = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 568 | (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 569 | return DVar; |
| 570 | } |
| 571 | } |
| 572 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 573 | // in a Construct, implicitly determined, p.3] |
| 574 | // For constructs other than task, if no default clause is present, these |
| 575 | // variables inherit their data-sharing attributes from the enclosing |
| 576 | // context. |
Dmitry Polukhin | dc78bc82 | 2016-04-01 09:52:30 +0000 | [diff] [blame] | 577 | return getDSA(++Iter, D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 580 | Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 581 | assert(!isStackEmpty() && "Data sharing attributes stack is empty"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 582 | D = getCanonicalDecl(D); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 583 | auto &StackElem = Stack.back().first.back(); |
| 584 | auto It = StackElem.AlignedMap.find(D); |
| 585 | if (It == StackElem.AlignedMap.end()) { |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 586 | assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 587 | StackElem.AlignedMap[D] = NewDE; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 588 | return nullptr; |
| 589 | } else { |
| 590 | assert(It->second && "Unexpected nullptr expr in the aligned map"); |
| 591 | return It->second; |
| 592 | } |
| 593 | return nullptr; |
| 594 | } |
| 595 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 596 | void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 597 | assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 598 | D = getCanonicalDecl(D); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 599 | auto &StackElem = Stack.back().first.back(); |
| 600 | StackElem.LCVMap.insert( |
| 601 | {D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)}); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 604 | DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 605 | assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 606 | D = getCanonicalDecl(D); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 607 | auto &StackElem = Stack.back().first.back(); |
| 608 | auto It = StackElem.LCVMap.find(D); |
| 609 | if (It != StackElem.LCVMap.end()) |
| 610 | return It->second; |
| 611 | return {0, nullptr}; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 614 | DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 615 | assert(!isStackEmpty() && Stack.back().first.size() > 1 && |
| 616 | "Data-sharing attributes stack is empty"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 617 | D = getCanonicalDecl(D); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 618 | auto &StackElem = *std::next(Stack.back().first.rbegin()); |
| 619 | auto It = StackElem.LCVMap.find(D); |
| 620 | if (It != StackElem.LCVMap.end()) |
| 621 | return It->second; |
| 622 | return {0, nullptr}; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 625 | ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 626 | assert(!isStackEmpty() && Stack.back().first.size() > 1 && |
| 627 | "Data-sharing attributes stack is empty"); |
| 628 | auto &StackElem = *std::next(Stack.back().first.rbegin()); |
| 629 | if (StackElem.LCVMap.size() < I) |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 630 | return nullptr; |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 631 | for (auto &Pair : StackElem.LCVMap) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 632 | if (Pair.second.first == I) |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 633 | return Pair.first; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 634 | return nullptr; |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 637 | void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, |
| 638 | DeclRefExpr *PrivateCopy) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 639 | D = getCanonicalDecl(D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 640 | if (A == OMPC_threadprivate) { |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 641 | auto &Data = Threadprivates[D]; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 642 | Data.Attributes = A; |
| 643 | Data.RefExpr.setPointer(E); |
| 644 | Data.PrivateCopy = nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 645 | } else { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 646 | assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); |
| 647 | auto &Data = Stack.back().first.back().SharingMap[D]; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 648 | assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || |
| 649 | (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || |
| 650 | (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || |
| 651 | (isLoopControlVariable(D).first && A == OMPC_private)); |
| 652 | if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { |
| 653 | Data.RefExpr.setInt(/*IntVal=*/true); |
| 654 | return; |
| 655 | } |
| 656 | const bool IsLastprivate = |
| 657 | A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; |
| 658 | Data.Attributes = A; |
| 659 | Data.RefExpr.setPointerAndInt(E, IsLastprivate); |
| 660 | Data.PrivateCopy = PrivateCopy; |
| 661 | if (PrivateCopy) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 662 | auto &Data = Stack.back().first.back().SharingMap[PrivateCopy->getDecl()]; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 663 | Data.Attributes = A; |
| 664 | Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); |
| 665 | Data.PrivateCopy = nullptr; |
| 666 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 670 | bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 671 | D = D->getCanonicalDecl(); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 672 | if (!isStackEmpty() && Stack.back().first.size() > 1) { |
| 673 | reverse_iterator I = Iter, E = Stack.back().first.rend(); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 674 | Scope *TopScope = nullptr; |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 675 | while (I != E && !isParallelOrTaskRegion(I->Directive)) |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 676 | ++I; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 677 | if (I == E) |
| 678 | return false; |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 679 | TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 680 | Scope *CurScope = getCurScope(); |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 681 | while (CurScope != TopScope && !CurScope->isDeclScope(D)) |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 682 | CurScope = CurScope->getParent(); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 683 | return CurScope != TopScope; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 684 | } |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 685 | return false; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 688 | /// \brief Build a variable declaration for OpenMP loop iteration variable. |
| 689 | static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 690 | StringRef Name, const AttrVec *Attrs = nullptr) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 691 | DeclContext *DC = SemaRef.CurContext; |
| 692 | IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); |
| 693 | TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); |
| 694 | VarDecl *Decl = |
| 695 | VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 696 | if (Attrs) { |
| 697 | for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); |
| 698 | I != E; ++I) |
| 699 | Decl->addAttr(*I); |
| 700 | } |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 701 | Decl->setImplicit(); |
| 702 | return Decl; |
| 703 | } |
| 704 | |
| 705 | static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, |
| 706 | SourceLocation Loc, |
| 707 | bool RefersToCapture = false) { |
| 708 | D->setReferenced(); |
| 709 | D->markUsed(S.Context); |
| 710 | return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), |
| 711 | SourceLocation(), D, RefersToCapture, Loc, Ty, |
| 712 | VK_LValue); |
| 713 | } |
| 714 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 715 | DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) { |
| 716 | D = getCanonicalDecl(D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 717 | DSAVarData DVar; |
| 718 | |
| 719 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 720 | // in a Construct, C/C++, predetermined, p.1] |
| 721 | // Variables appearing in threadprivate directives are threadprivate. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 722 | auto *VD = dyn_cast<VarDecl>(D); |
| 723 | if ((VD && VD->getTLSKind() != VarDecl::TLS_None && |
| 724 | !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 725 | SemaRef.getLangOpts().OpenMPUseTLS && |
| 726 | SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 727 | (VD && VD->getStorageClass() == SC_Register && |
| 728 | VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { |
| 729 | addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 730 | D->getLocation()), |
Alexey Bataev | f2453a0 | 2015-05-06 07:25:08 +0000 | [diff] [blame] | 731 | OMPC_threadprivate); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 732 | } |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 733 | auto TI = Threadprivates.find(D); |
| 734 | if (TI != Threadprivates.end()) { |
| 735 | DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 736 | DVar.CKind = OMPC_threadprivate; |
| 737 | return DVar; |
| 738 | } |
| 739 | |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 740 | if (isStackEmpty()) |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 741 | // Not in OpenMP execution region and top scope was already checked. |
| 742 | return DVar; |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 743 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 744 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
Alexey Bataev | dffa93a | 2015-12-10 08:20:58 +0000 | [diff] [blame] | 745 | // in a Construct, C/C++, predetermined, p.4] |
| 746 | // Static data members are shared. |
| 747 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 748 | // in a Construct, C/C++, predetermined, p.7] |
| 749 | // Variables with static storage duration that are declared in a scope |
| 750 | // inside the construct are shared. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 751 | auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; }; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 752 | if (VD && VD->isStaticDataMember()) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 753 | DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); |
Alexey Bataev | dffa93a | 2015-12-10 08:20:58 +0000 | [diff] [blame] | 754 | if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 755 | return DVar; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 756 | |
Alexey Bataev | dffa93a | 2015-12-10 08:20:58 +0000 | [diff] [blame] | 757 | DVar.CKind = OMPC_shared; |
| 758 | return DVar; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | QualType Type = D->getType().getNonReferenceType().getCanonicalType(); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 762 | bool IsConstant = Type.isConstant(SemaRef.getASTContext()); |
| 763 | Type = SemaRef.getASTContext().getBaseElementType(Type); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 764 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 765 | // in a Construct, C/C++, predetermined, p.6] |
| 766 | // Variables with const qualified type having no mutable member are |
| 767 | // shared. |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 768 | CXXRecordDecl *RD = |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 769 | SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; |
Alexey Bataev | c9bd03d | 2015-12-17 06:55:08 +0000 | [diff] [blame] | 770 | if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) |
| 771 | if (auto *CTD = CTSD->getSpecializedTemplate()) |
| 772 | RD = CTD->getTemplatedDecl(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 773 | if (IsConstant && |
Alexey Bataev | 4bcad7f | 2016-02-10 10:50:12 +0000 | [diff] [blame] | 774 | !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() && |
| 775 | RD->hasMutableFields())) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 776 | // Variables with const-qualified type having no mutable member may be |
| 777 | // listed in a firstprivate clause, even if they are static data members. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 778 | DSAVarData DVarTemp = hasDSA( |
| 779 | D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; }, |
| 780 | MatchesAlways, FromParent); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 781 | if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) |
| 782 | return DVar; |
| 783 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 784 | DVar.CKind = OMPC_shared; |
| 785 | return DVar; |
| 786 | } |
| 787 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 788 | // Explicitly specified attributes and local variables with predetermined |
| 789 | // attributes. |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 790 | auto StartI = std::next(Stack.back().first.rbegin()); |
| 791 | auto EndI = Stack.back().first.rend(); |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 792 | if (FromParent && StartI != EndI) |
Alexey Bataev | dffa93a | 2015-12-10 08:20:58 +0000 | [diff] [blame] | 793 | StartI = std::next(StartI); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 794 | auto I = std::prev(StartI); |
| 795 | if (I->SharingMap.count(D)) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 796 | DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer(); |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 797 | DVar.PrivateCopy = I->SharingMap[D].PrivateCopy; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 798 | DVar.CKind = I->SharingMap[D].Attributes; |
| 799 | DVar.ImplicitDSALoc = I->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | return DVar; |
| 803 | } |
| 804 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 805 | DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, |
| 806 | bool FromParent) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 807 | if (isStackEmpty()) { |
| 808 | StackTy::reverse_iterator I; |
| 809 | return getDSA(I, D); |
| 810 | } |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 811 | D = getCanonicalDecl(D); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 812 | auto StartI = Stack.back().first.rbegin(); |
| 813 | auto EndI = Stack.back().first.rend(); |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 814 | if (FromParent && StartI != EndI) |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 815 | StartI = std::next(StartI); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 816 | return getDSA(StartI, D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 819 | DSAStackTy::DSAVarData |
| 820 | DSAStackTy::hasDSA(ValueDecl *D, |
| 821 | const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 822 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 823 | bool FromParent) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 824 | if (isStackEmpty()) |
| 825 | return {}; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 826 | D = getCanonicalDecl(D); |
Alexey Bataev | 0e6fc1c | 2017-04-27 14:46:26 +0000 | [diff] [blame^] | 827 | auto I = (FromParent && Stack.back().first.size() > 1) |
| 828 | ? std::next(Stack.back().first.rbegin()) |
| 829 | : Stack.back().first.rbegin(); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 830 | auto EndI = Stack.back().first.rend(); |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 831 | do { |
Alexey Bataev | 0e6fc1c | 2017-04-27 14:46:26 +0000 | [diff] [blame^] | 832 | std::advance(I, 1); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 833 | if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 834 | continue; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 835 | DSAVarData DVar = getDSA(I, D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 836 | if (CPred(DVar.CKind)) |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 837 | return DVar; |
Alexey Bataev | 0e6fc1c | 2017-04-27 14:46:26 +0000 | [diff] [blame^] | 838 | } while (std::distance(I, EndI) > 1); |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 839 | return {}; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 842 | DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( |
| 843 | ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 844 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 845 | bool FromParent) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 846 | if (isStackEmpty()) |
| 847 | return {}; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 848 | D = getCanonicalDecl(D); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 849 | auto StartI = std::next(Stack.back().first.rbegin()); |
| 850 | auto EndI = Stack.back().first.rend(); |
Alexey Bataev | e397812 | 2016-07-19 05:06:39 +0000 | [diff] [blame] | 851 | if (FromParent && StartI != EndI) |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 852 | StartI = std::next(StartI); |
Alexey Bataev | e397812 | 2016-07-19 05:06:39 +0000 | [diff] [blame] | 853 | if (StartI == EndI || !DPred(StartI->Directive)) |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 854 | return {}; |
Alexey Bataev | e397812 | 2016-07-19 05:06:39 +0000 | [diff] [blame] | 855 | DSAVarData DVar = getDSA(StartI, D); |
| 856 | return CPred(DVar.CKind) ? DVar : DSAVarData(); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 859 | bool DSAStackTy::hasExplicitDSA( |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 860 | ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 861 | unsigned Level, bool NotLastprivate) { |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 862 | if (CPred(ClauseKindMode)) |
| 863 | return true; |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 864 | if (isStackEmpty()) |
| 865 | return false; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 866 | D = getCanonicalDecl(D); |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 867 | auto StartI = Stack.back().first.begin(); |
| 868 | auto EndI = Stack.back().first.end(); |
NAKAMURA Takumi | 0332eda | 2015-06-23 10:01:20 +0000 | [diff] [blame] | 869 | if (std::distance(StartI, EndI) <= (int)Level) |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 870 | return false; |
| 871 | std::advance(StartI, Level); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 872 | return (StartI->SharingMap.count(D) > 0) && |
| 873 | StartI->SharingMap[D].RefExpr.getPointer() && |
| 874 | CPred(StartI->SharingMap[D].Attributes) && |
| 875 | (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt()); |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 878 | bool DSAStackTy::hasExplicitDirective( |
| 879 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 880 | unsigned Level) { |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 881 | if (isStackEmpty()) |
| 882 | return false; |
| 883 | auto StartI = Stack.back().first.begin(); |
| 884 | auto EndI = Stack.back().first.end(); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 885 | if (std::distance(StartI, EndI) <= (int)Level) |
| 886 | return false; |
| 887 | std::advance(StartI, Level); |
| 888 | return DPred(StartI->Directive); |
| 889 | } |
| 890 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 891 | bool DSAStackTy::hasDirective( |
| 892 | const llvm::function_ref<bool(OpenMPDirectiveKind, |
| 893 | const DeclarationNameInfo &, SourceLocation)> |
| 894 | &DPred, |
| 895 | bool FromParent) { |
Samuel Antao | f0d7975 | 2016-05-27 15:21:27 +0000 | [diff] [blame] | 896 | // We look only in the enclosing region. |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 897 | if (isStackEmpty()) |
Samuel Antao | f0d7975 | 2016-05-27 15:21:27 +0000 | [diff] [blame] | 898 | return false; |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 899 | auto StartI = std::next(Stack.back().first.rbegin()); |
| 900 | auto EndI = Stack.back().first.rend(); |
Alexey Bataev | ccaddfb | 2017-04-26 14:24:21 +0000 | [diff] [blame] | 901 | if (FromParent && StartI != EndI) |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 902 | StartI = std::next(StartI); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 903 | for (auto I = StartI, EE = EndI; I != EE; ++I) { |
| 904 | if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) |
| 905 | return true; |
| 906 | } |
| 907 | return false; |
| 908 | } |
| 909 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 910 | void Sema::InitDataSharingAttributesStack() { |
| 911 | VarDataSharingAttributesStack = new DSAStackTy(*this); |
| 912 | } |
| 913 | |
| 914 | #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) |
| 915 | |
Alexey Bataev | 4b46539 | 2017-04-26 15:06:24 +0000 | [diff] [blame] | 916 | void Sema::pushOpenMPFunctionRegion() { |
| 917 | DSAStack->pushFunction(); |
| 918 | } |
| 919 | |
| 920 | void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { |
| 921 | DSAStack->popFunction(OldFSI); |
| 922 | } |
| 923 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 924 | bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 925 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
| 926 | |
| 927 | auto &Ctx = getASTContext(); |
| 928 | bool IsByRef = true; |
| 929 | |
| 930 | // Find the directive that is associated with the provided scope. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 931 | auto Ty = D->getType(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 932 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 933 | if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 934 | // This table summarizes how a given variable should be passed to the device |
| 935 | // given its type and the clauses where it appears. This table is based on |
| 936 | // the description in OpenMP 4.5 [2.10.4, target Construct] and |
| 937 | // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. |
| 938 | // |
| 939 | // ========================================================================= |
| 940 | // | type | defaultmap | pvt | first | is_device_ptr | map | res. | |
| 941 | // | |(tofrom:scalar)| | pvt | | | | |
| 942 | // ========================================================================= |
| 943 | // | scl | | | | - | | bycopy| |
| 944 | // | scl | | - | x | - | - | bycopy| |
| 945 | // | scl | | x | - | - | - | null | |
| 946 | // | scl | x | | | - | | byref | |
| 947 | // | scl | x | - | x | - | - | bycopy| |
| 948 | // | scl | x | x | - | - | - | null | |
| 949 | // | scl | | - | - | - | x | byref | |
| 950 | // | scl | x | - | - | - | x | byref | |
| 951 | // |
| 952 | // | agg | n.a. | | | - | | byref | |
| 953 | // | agg | n.a. | - | x | - | - | byref | |
| 954 | // | agg | n.a. | x | - | - | - | null | |
| 955 | // | agg | n.a. | - | - | - | x | byref | |
| 956 | // | agg | n.a. | - | - | - | x[] | byref | |
| 957 | // |
| 958 | // | ptr | n.a. | | | - | | bycopy| |
| 959 | // | ptr | n.a. | - | x | - | - | bycopy| |
| 960 | // | ptr | n.a. | x | - | - | - | null | |
| 961 | // | ptr | n.a. | - | - | - | x | byref | |
| 962 | // | ptr | n.a. | - | - | - | x[] | bycopy| |
| 963 | // | ptr | n.a. | - | - | x | | bycopy| |
| 964 | // | ptr | n.a. | - | - | x | x | bycopy| |
| 965 | // | ptr | n.a. | - | - | x | x[] | bycopy| |
| 966 | // ========================================================================= |
| 967 | // Legend: |
| 968 | // scl - scalar |
| 969 | // ptr - pointer |
| 970 | // agg - aggregate |
| 971 | // x - applies |
| 972 | // - - invalid in this combination |
| 973 | // [] - mapped with an array section |
| 974 | // byref - should be mapped by reference |
| 975 | // byval - should be mapped by value |
| 976 | // null - initialize a local variable to null on the device |
| 977 | // |
| 978 | // Observations: |
| 979 | // - All scalar declarations that show up in a map clause have to be passed |
| 980 | // by reference, because they may have been mapped in the enclosing data |
| 981 | // environment. |
| 982 | // - If the scalar value does not fit the size of uintptr, it has to be |
| 983 | // passed by reference, regardless the result in the table above. |
| 984 | // - For pointers mapped by value that have either an implicit map or an |
| 985 | // array section, the runtime library may pass the NULL value to the |
| 986 | // device instead of the value passed to it by the compiler. |
| 987 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 988 | if (Ty->isReferenceType()) |
| 989 | Ty = Ty->castAs<ReferenceType>()->getPointeeType(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 990 | |
| 991 | // Locate map clauses and see if the variable being captured is referred to |
| 992 | // in any of those clauses. Here we only care about variables, not fields, |
| 993 | // because fields are part of aggregates. |
| 994 | bool IsVariableUsedInMapClause = false; |
| 995 | bool IsVariableAssociatedWithSection = false; |
| 996 | |
| 997 | DSAStack->checkMappableExprComponentListsForDecl( |
| 998 | D, /*CurrentRegionOnly=*/true, |
| 999 | [&](OMPClauseMappableExprCommon::MappableExprComponentListRef |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 1000 | MapExprComponents, |
| 1001 | OpenMPClauseKind WhereFoundClauseKind) { |
| 1002 | // Only the map clause information influences how a variable is |
| 1003 | // captured. E.g. is_device_ptr does not require changing the default |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 1004 | // behavior. |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 1005 | if (WhereFoundClauseKind != OMPC_map) |
| 1006 | return false; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 1007 | |
| 1008 | auto EI = MapExprComponents.rbegin(); |
| 1009 | auto EE = MapExprComponents.rend(); |
| 1010 | |
| 1011 | assert(EI != EE && "Invalid map expression!"); |
| 1012 | |
| 1013 | if (isa<DeclRefExpr>(EI->getAssociatedExpression())) |
| 1014 | IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; |
| 1015 | |
| 1016 | ++EI; |
| 1017 | if (EI == EE) |
| 1018 | return false; |
| 1019 | |
| 1020 | if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || |
| 1021 | isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || |
| 1022 | isa<MemberExpr>(EI->getAssociatedExpression())) { |
| 1023 | IsVariableAssociatedWithSection = true; |
| 1024 | // There is nothing more we need to know about this variable. |
| 1025 | return true; |
| 1026 | } |
| 1027 | |
| 1028 | // Keep looking for more map info. |
| 1029 | return false; |
| 1030 | }); |
| 1031 | |
| 1032 | if (IsVariableUsedInMapClause) { |
| 1033 | // If variable is identified in a map clause it is always captured by |
| 1034 | // reference except if it is a pointer that is dereferenced somehow. |
| 1035 | IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); |
| 1036 | } else { |
| 1037 | // By default, all the data that has a scalar type is mapped by copy. |
| 1038 | IsByRef = !Ty->isScalarType(); |
| 1039 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1042 | if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { |
| 1043 | IsByRef = !DSAStack->hasExplicitDSA( |
| 1044 | D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, |
| 1045 | Level, /*NotLastprivate=*/true); |
| 1046 | } |
| 1047 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 1048 | // When passing data by copy, we need to make sure it fits the uintptr size |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 1049 | // and alignment, because the runtime library only deals with uintptr types. |
| 1050 | // If it does not fit the uintptr size, we need to pass the data by reference |
| 1051 | // instead. |
| 1052 | if (!IsByRef && |
| 1053 | (Ctx.getTypeSizeInChars(Ty) > |
| 1054 | Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1055 | Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 1056 | IsByRef = true; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1057 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 1058 | |
| 1059 | return IsByRef; |
| 1060 | } |
| 1061 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1062 | unsigned Sema::getOpenMPNestingLevel() const { |
| 1063 | assert(getLangOpts().OpenMP); |
| 1064 | return DSAStack->getNestingLevel(); |
| 1065 | } |
| 1066 | |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1067 | VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) { |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 1068 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1069 | D = getCanonicalDecl(D); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1070 | |
| 1071 | // If we are attempting to capture a global variable in a directive with |
| 1072 | // 'target' we return true so that this global is also mapped to the device. |
| 1073 | // |
| 1074 | // FIXME: If the declaration is enclosed in a 'declare target' directive, |
| 1075 | // then it should not be captured. Therefore, an extra check has to be |
| 1076 | // inserted here once support for 'declare target' is added. |
| 1077 | // |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1078 | auto *VD = dyn_cast<VarDecl>(D); |
| 1079 | if (VD && !VD->hasLocalStorage()) { |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1080 | if (DSAStack->getCurrentDirective() == OMPD_target && |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1081 | !DSAStack->isClauseParsingMode()) |
| 1082 | return VD; |
Samuel Antao | f0d7975 | 2016-05-27 15:21:27 +0000 | [diff] [blame] | 1083 | if (DSAStack->hasDirective( |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1084 | [](OpenMPDirectiveKind K, const DeclarationNameInfo &, |
| 1085 | SourceLocation) -> bool { |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 1086 | return isOpenMPTargetExecutionDirective(K); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1087 | }, |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1088 | false)) |
| 1089 | return VD; |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Alexey Bataev | 48977c3 | 2015-08-04 08:10:48 +0000 | [diff] [blame] | 1092 | if (DSAStack->getCurrentDirective() != OMPD_unknown && |
| 1093 | (!DSAStack->isClauseParsingMode() || |
| 1094 | DSAStack->getParentDirective() != OMPD_unknown)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 1095 | auto &&Info = DSAStack->isLoopControlVariable(D); |
| 1096 | if (Info.first || |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1097 | (VD && VD->hasLocalStorage() && |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 1098 | isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1099 | (VD && DSAStack->isForceVarCapturing())) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 1100 | return VD ? VD : Info.second; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1101 | auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 1102 | if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1103 | return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1104 | DVarPrivate = DSAStack->hasDSA( |
| 1105 | D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, |
| 1106 | DSAStack->isClauseParsingMode()); |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1107 | if (DVarPrivate.CKind != OMPC_unknown) |
| 1108 | return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 1109 | } |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1110 | return nullptr; |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1113 | bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) { |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 1114 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
| 1115 | return DSAStack->hasExplicitDSA( |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1116 | D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level); |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1119 | bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) { |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1120 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
| 1121 | // Return true if the current level is no longer enclosed in a target region. |
| 1122 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1123 | auto *VD = dyn_cast<VarDecl>(D); |
| 1124 | return VD && !VD->hasLocalStorage() && |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 1125 | DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, |
| 1126 | Level); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1129 | void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1130 | |
| 1131 | void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, |
| 1132 | const DeclarationNameInfo &DirName, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1133 | Scope *CurScope, SourceLocation Loc) { |
| 1134 | DSAStack->push(DKind, DirName, CurScope, Loc); |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1135 | PushExpressionEvaluationContext( |
| 1136 | ExpressionEvaluationContext::PotentiallyEvaluated); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 1139 | void Sema::StartOpenMPClause(OpenMPClauseKind K) { |
| 1140 | DSAStack->setClauseParsingMode(K); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 1143 | void Sema::EndOpenMPClause() { |
| 1144 | DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1147 | void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1148 | // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] |
| 1149 | // A variable of class type (or array thereof) that appears in a lastprivate |
| 1150 | // clause requires an accessible, unambiguous default constructor for the |
| 1151 | // class type, unless the list item is also specified in a firstprivate |
| 1152 | // clause. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1153 | if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1154 | for (auto *C : D->clauses()) { |
| 1155 | if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { |
| 1156 | SmallVector<Expr *, 8> PrivateCopies; |
| 1157 | for (auto *DE : Clause->varlists()) { |
| 1158 | if (DE->isValueDependent() || DE->isTypeDependent()) { |
| 1159 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1160 | continue; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1161 | } |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 1162 | auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1163 | VarDecl *VD = cast<VarDecl>(DRE->getDecl()); |
| 1164 | QualType Type = VD->getType().getNonReferenceType(); |
| 1165 | auto DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1166 | if (DVar.CKind == OMPC_lastprivate) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1167 | // Generate helper private variable and initialize it with the |
| 1168 | // default value. The address of the original variable is replaced |
| 1169 | // by the address of the new private variable in CodeGen. This new |
| 1170 | // variable is not added to IdResolver, so the code in the OpenMP |
| 1171 | // region uses original variable for proper diagnostics. |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 1172 | auto *VDPrivate = buildVarDecl( |
| 1173 | *this, DE->getExprLoc(), Type.getUnqualifiedType(), |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1174 | VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr); |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1175 | ActOnUninitializedDecl(VDPrivate); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1176 | if (VDPrivate->isInvalidDecl()) |
| 1177 | continue; |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1178 | PrivateCopies.push_back(buildDeclRefExpr( |
| 1179 | *this, VDPrivate, DE->getType(), DE->getExprLoc())); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1180 | } else { |
| 1181 | // The variable is also a firstprivate, so initialization sequence |
| 1182 | // for private copy is generated already. |
| 1183 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1184 | } |
| 1185 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1186 | // Set initializers to private copies if no errors were found. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1187 | if (PrivateCopies.size() == Clause->varlist_size()) |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1188 | Clause->setPrivateCopies(PrivateCopies); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1193 | DSAStack->pop(); |
| 1194 | DiscardCleanupsInEvaluationContext(); |
| 1195 | PopExpressionEvaluationContext(); |
| 1196 | } |
| 1197 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1198 | static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, |
| 1199 | Expr *NumIterations, Sema &SemaRef, |
| 1200 | Scope *S, DSAStackTy *Stack); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1201 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1202 | namespace { |
| 1203 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1204 | class VarDeclFilterCCC : public CorrectionCandidateCallback { |
| 1205 | private: |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1206 | Sema &SemaRef; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1207 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1208 | public: |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1209 | explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1210 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1211 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1212 | if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1213 | return VD->hasGlobalStorage() && |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1214 | SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), |
| 1215 | SemaRef.getCurScope()); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1216 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1217 | return false; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1218 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1219 | }; |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 1220 | |
| 1221 | class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback { |
| 1222 | private: |
| 1223 | Sema &SemaRef; |
| 1224 | |
| 1225 | public: |
| 1226 | explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} |
| 1227 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| 1228 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 1229 | if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { |
| 1230 | return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), |
| 1231 | SemaRef.getCurScope()); |
| 1232 | } |
| 1233 | return false; |
| 1234 | } |
| 1235 | }; |
| 1236 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1237 | } // namespace |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1238 | |
| 1239 | ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, |
| 1240 | CXXScopeSpec &ScopeSpec, |
| 1241 | const DeclarationNameInfo &Id) { |
| 1242 | LookupResult Lookup(*this, Id, LookupOrdinaryName); |
| 1243 | LookupParsedName(Lookup, CurScope, &ScopeSpec, true); |
| 1244 | |
| 1245 | if (Lookup.isAmbiguous()) |
| 1246 | return ExprError(); |
| 1247 | |
| 1248 | VarDecl *VD; |
| 1249 | if (!Lookup.isSingleResult()) { |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1250 | if (TypoCorrection Corrected = CorrectTypo( |
| 1251 | Id, LookupOrdinaryName, CurScope, nullptr, |
| 1252 | llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1253 | diagnoseTypo(Corrected, |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 1254 | PDiag(Lookup.empty() |
| 1255 | ? diag::err_undeclared_var_use_suggest |
| 1256 | : diag::err_omp_expected_var_arg_suggest) |
| 1257 | << Id.getName()); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1258 | VD = Corrected.getCorrectionDeclAs<VarDecl>(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1259 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1260 | Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use |
| 1261 | : diag::err_omp_expected_var_arg) |
| 1262 | << Id.getName(); |
| 1263 | return ExprError(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1264 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1265 | } else { |
| 1266 | if (!(VD = Lookup.getAsSingle<VarDecl>())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1267 | Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1268 | Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); |
| 1269 | return ExprError(); |
| 1270 | } |
| 1271 | } |
| 1272 | Lookup.suppressDiagnostics(); |
| 1273 | |
| 1274 | // OpenMP [2.9.2, Syntax, C/C++] |
| 1275 | // Variables must be file-scope, namespace-scope, or static block-scope. |
| 1276 | if (!VD->hasGlobalStorage()) { |
| 1277 | Diag(Id.getLoc(), diag::err_omp_global_var_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1278 | << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); |
| 1279 | bool IsDecl = |
| 1280 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1281 | Diag(VD->getLocation(), |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1282 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1283 | << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1284 | return ExprError(); |
| 1285 | } |
| 1286 | |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1287 | VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 1288 | NamedDecl *ND = cast<NamedDecl>(CanonicalVD); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1289 | // OpenMP [2.9.2, Restrictions, C/C++, p.2] |
| 1290 | // A threadprivate directive for file-scope variables must appear outside |
| 1291 | // any definition or declaration. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1292 | if (CanonicalVD->getDeclContext()->isTranslationUnit() && |
| 1293 | !getCurLexicalContext()->isTranslationUnit()) { |
| 1294 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1295 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 1296 | bool IsDecl = |
| 1297 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1298 | Diag(VD->getLocation(), |
| 1299 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1300 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1301 | return ExprError(); |
| 1302 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1303 | // OpenMP [2.9.2, Restrictions, C/C++, p.3] |
| 1304 | // A threadprivate directive for static class member variables must appear |
| 1305 | // in the class definition, in the same scope in which the member |
| 1306 | // variables are declared. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1307 | if (CanonicalVD->isStaticDataMember() && |
| 1308 | !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { |
| 1309 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1310 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 1311 | bool IsDecl = |
| 1312 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1313 | Diag(VD->getLocation(), |
| 1314 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1315 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1316 | return ExprError(); |
| 1317 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1318 | // OpenMP [2.9.2, Restrictions, C/C++, p.4] |
| 1319 | // A threadprivate directive for namespace-scope variables must appear |
| 1320 | // outside any definition or declaration other than the namespace |
| 1321 | // definition itself. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1322 | if (CanonicalVD->getDeclContext()->isNamespace() && |
| 1323 | (!getCurLexicalContext()->isFileContext() || |
| 1324 | !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { |
| 1325 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1326 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 1327 | bool IsDecl = |
| 1328 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1329 | Diag(VD->getLocation(), |
| 1330 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1331 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1332 | return ExprError(); |
| 1333 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1334 | // OpenMP [2.9.2, Restrictions, C/C++, p.6] |
| 1335 | // A threadprivate directive for static block-scope variables must appear |
| 1336 | // in the scope of the variable and not in a nested scope. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1337 | if (CanonicalVD->isStaticLocal() && CurScope && |
| 1338 | !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1339 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1340 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 1341 | bool IsDecl = |
| 1342 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1343 | Diag(VD->getLocation(), |
| 1344 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1345 | << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1346 | return ExprError(); |
| 1347 | } |
| 1348 | |
| 1349 | // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] |
| 1350 | // A threadprivate directive must lexically precede all references to any |
| 1351 | // of the variables in its list. |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 1352 | if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1353 | Diag(Id.getLoc(), diag::err_omp_var_used) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1354 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1355 | return ExprError(); |
| 1356 | } |
| 1357 | |
| 1358 | QualType ExprType = VD->getType().getNonReferenceType(); |
Alexey Bataev | 376b4a4 | 2016-02-09 09:41:09 +0000 | [diff] [blame] | 1359 | return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), |
| 1360 | SourceLocation(), VD, |
| 1361 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1362 | Id.getLoc(), ExprType, VK_LValue); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1365 | Sema::DeclGroupPtrTy |
| 1366 | Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, |
| 1367 | ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1368 | if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1369 | CurContext->addDecl(D); |
| 1370 | return DeclGroupPtrTy::make(DeclGroupRef(D)); |
| 1371 | } |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1372 | return nullptr; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1375 | namespace { |
| 1376 | class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { |
| 1377 | Sema &SemaRef; |
| 1378 | |
| 1379 | public: |
| 1380 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1381 | if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1382 | if (VD->hasLocalStorage()) { |
| 1383 | SemaRef.Diag(E->getLocStart(), |
| 1384 | diag::err_omp_local_var_in_threadprivate_init) |
| 1385 | << E->getSourceRange(); |
| 1386 | SemaRef.Diag(VD->getLocation(), diag::note_defined_here) |
| 1387 | << VD << VD->getSourceRange(); |
| 1388 | return true; |
| 1389 | } |
| 1390 | } |
| 1391 | return false; |
| 1392 | } |
| 1393 | bool VisitStmt(const Stmt *S) { |
| 1394 | for (auto Child : S->children()) { |
| 1395 | if (Child && Visit(Child)) |
| 1396 | return true; |
| 1397 | } |
| 1398 | return false; |
| 1399 | } |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1400 | explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1401 | }; |
| 1402 | } // namespace |
| 1403 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1404 | OMPThreadPrivateDecl * |
| 1405 | Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1406 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1407 | for (auto &RefExpr : VarList) { |
| 1408 | DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1409 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 1410 | SourceLocation ILoc = DE->getExprLoc(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1411 | |
Alexey Bataev | 376b4a4 | 2016-02-09 09:41:09 +0000 | [diff] [blame] | 1412 | // Mark variable as used. |
| 1413 | VD->setReferenced(); |
| 1414 | VD->markUsed(Context); |
| 1415 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 1416 | QualType QType = VD->getType(); |
| 1417 | if (QType->isDependentType() || QType->isInstantiationDependentType()) { |
| 1418 | // It will be analyzed later. |
| 1419 | Vars.push_back(DE); |
| 1420 | continue; |
| 1421 | } |
| 1422 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1423 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 1424 | // A threadprivate variable must not have an incomplete type. |
| 1425 | if (RequireCompleteType(ILoc, VD->getType(), |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1426 | diag::err_omp_threadprivate_incomplete_type)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1427 | continue; |
| 1428 | } |
| 1429 | |
| 1430 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 1431 | // A threadprivate variable must not have a reference type. |
| 1432 | if (VD->getType()->isReferenceType()) { |
| 1433 | Diag(ILoc, diag::err_omp_ref_type_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1434 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); |
| 1435 | bool IsDecl = |
| 1436 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1437 | Diag(VD->getLocation(), |
| 1438 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1439 | << VD; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1440 | continue; |
| 1441 | } |
| 1442 | |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1443 | // Check if this is a TLS variable. If TLS is not being supported, produce |
| 1444 | // the corresponding diagnostic. |
| 1445 | if ((VD->getTLSKind() != VarDecl::TLS_None && |
| 1446 | !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && |
| 1447 | getLangOpts().OpenMPUseTLS && |
| 1448 | getASTContext().getTargetInfo().isTLSSupported())) || |
Alexey Bataev | 1a8b3f1 | 2015-05-06 06:34:55 +0000 | [diff] [blame] | 1449 | (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && |
| 1450 | !VD->isLocalVarDecl())) { |
Alexey Bataev | 26a3924 | 2015-01-13 03:35:30 +0000 | [diff] [blame] | 1451 | Diag(ILoc, diag::err_omp_var_thread_local) |
| 1452 | << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1453 | bool IsDecl = |
| 1454 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1455 | Diag(VD->getLocation(), |
| 1456 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1457 | << VD; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1458 | continue; |
| 1459 | } |
| 1460 | |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1461 | // Check if initial value of threadprivate variable reference variable with |
| 1462 | // local storage (it is not supported by runtime). |
| 1463 | if (auto Init = VD->getAnyInitializer()) { |
| 1464 | LocalVarRefChecker Checker(*this); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1465 | if (Checker.Visit(Init)) |
| 1466 | continue; |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1469 | Vars.push_back(RefExpr); |
Alexey Bataev | d178ad4 | 2014-03-07 08:03:37 +0000 | [diff] [blame] | 1470 | DSAStack->addDSA(VD, DE, OMPC_threadprivate); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1471 | VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( |
| 1472 | Context, SourceRange(Loc, Loc))); |
| 1473 | if (auto *ML = Context.getASTMutationListener()) |
| 1474 | ML->DeclarationMarkedOpenMPThreadPrivate(VD); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1475 | } |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 1476 | OMPThreadPrivateDecl *D = nullptr; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 1477 | if (!Vars.empty()) { |
| 1478 | D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, |
| 1479 | Vars); |
| 1480 | D->setAccess(AS_public); |
| 1481 | } |
| 1482 | return D; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1483 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1484 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1485 | static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1486 | const ValueDecl *D, DSAStackTy::DSAVarData DVar, |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1487 | bool IsLoopIterVar = false) { |
| 1488 | if (DVar.RefExpr) { |
| 1489 | SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) |
| 1490 | << getOpenMPClauseName(DVar.CKind); |
| 1491 | return; |
| 1492 | } |
| 1493 | enum { |
| 1494 | PDSA_StaticMemberShared, |
| 1495 | PDSA_StaticLocalVarShared, |
| 1496 | PDSA_LoopIterVarPrivate, |
| 1497 | PDSA_LoopIterVarLinear, |
| 1498 | PDSA_LoopIterVarLastprivate, |
| 1499 | PDSA_ConstVarShared, |
| 1500 | PDSA_GlobalVarShared, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1501 | PDSA_TaskVarFirstprivate, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1502 | PDSA_LocalVarPrivate, |
| 1503 | PDSA_Implicit |
| 1504 | } Reason = PDSA_Implicit; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1505 | bool ReportHint = false; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1506 | auto ReportLoc = D->getLocation(); |
| 1507 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1508 | if (IsLoopIterVar) { |
| 1509 | if (DVar.CKind == OMPC_private) |
| 1510 | Reason = PDSA_LoopIterVarPrivate; |
| 1511 | else if (DVar.CKind == OMPC_lastprivate) |
| 1512 | Reason = PDSA_LoopIterVarLastprivate; |
| 1513 | else |
| 1514 | Reason = PDSA_LoopIterVarLinear; |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1515 | } else if (isOpenMPTaskingDirective(DVar.DKind) && |
| 1516 | DVar.CKind == OMPC_firstprivate) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1517 | Reason = PDSA_TaskVarFirstprivate; |
| 1518 | ReportLoc = DVar.ImplicitDSALoc; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1519 | } else if (VD && VD->isStaticLocal()) |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1520 | Reason = PDSA_StaticLocalVarShared; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1521 | else if (VD && VD->isStaticDataMember()) |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1522 | Reason = PDSA_StaticMemberShared; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1523 | else if (VD && VD->isFileVarDecl()) |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1524 | Reason = PDSA_GlobalVarShared; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1525 | else if (D->getType().isConstant(SemaRef.getASTContext())) |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1526 | Reason = PDSA_ConstVarShared; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1527 | else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1528 | ReportHint = true; |
| 1529 | Reason = PDSA_LocalVarPrivate; |
| 1530 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1531 | if (Reason != PDSA_Implicit) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1532 | SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1533 | << Reason << ReportHint |
| 1534 | << getOpenMPDirectiveName(Stack->getCurrentDirective()); |
| 1535 | } else if (DVar.ImplicitDSALoc.isValid()) { |
| 1536 | SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) |
| 1537 | << getOpenMPClauseName(DVar.CKind); |
| 1538 | } |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1541 | namespace { |
| 1542 | class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { |
| 1543 | DSAStackTy *Stack; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1544 | Sema &SemaRef; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1545 | bool ErrorFound; |
| 1546 | CapturedStmt *CS; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1547 | llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1548 | llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1549 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1550 | public: |
| 1551 | void VisitDeclRefExpr(DeclRefExpr *E) { |
Alexey Bataev | 07b79c2 | 2016-04-29 09:56:11 +0000 | [diff] [blame] | 1552 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1553 | E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) |
| 1554 | return; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1555 | if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1556 | // Skip internally declared variables. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1557 | if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) |
| 1558 | return; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1559 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1560 | auto DVar = Stack->getTopDSA(VD, false); |
| 1561 | // Check if the variable has explicit DSA set and stop analysis if it so. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1562 | if (DVar.RefExpr) |
| 1563 | return; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1564 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1565 | auto ELoc = E->getExprLoc(); |
| 1566 | auto DKind = Stack->getCurrentDirective(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1567 | // The default(none) clause requires that each variable that is referenced |
| 1568 | // in the construct, and does not have a predetermined data-sharing |
| 1569 | // attribute, must have its data-sharing attribute explicitly determined |
| 1570 | // by being listed in a data-sharing attribute clause. |
| 1571 | if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1572 | isParallelOrTaskRegion(DKind) && |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1573 | VarsWithInheritedDSA.count(VD) == 0) { |
| 1574 | VarsWithInheritedDSA[VD] = E; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1575 | return; |
| 1576 | } |
| 1577 | |
| 1578 | // OpenMP [2.9.3.6, Restrictions, p.2] |
| 1579 | // A list item that appears in a reduction clause of the innermost |
| 1580 | // enclosing worksharing or parallel construct may not be accessed in an |
| 1581 | // explicit task. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1582 | DVar = Stack->hasInnermostDSA( |
| 1583 | VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, |
| 1584 | [](OpenMPDirectiveKind K) -> bool { |
| 1585 | return isOpenMPParallelDirective(K) || |
| 1586 | isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); |
| 1587 | }, |
| 1588 | false); |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1589 | if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 1590 | ErrorFound = true; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1591 | SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); |
| 1592 | ReportOriginalDSA(SemaRef, Stack, VD, DVar); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 1593 | return; |
| 1594 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1595 | |
| 1596 | // Define implicit data-sharing attributes for task. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1597 | DVar = Stack->getImplicitDSA(VD, false); |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1598 | if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && |
| 1599 | !Stack->isLoopControlVariable(VD).first) |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1600 | ImplicitFirstprivate.push_back(E); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1601 | } |
| 1602 | } |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1603 | void VisitMemberExpr(MemberExpr *E) { |
Alexey Bataev | 07b79c2 | 2016-04-29 09:56:11 +0000 | [diff] [blame] | 1604 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1605 | E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) |
| 1606 | return; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1607 | if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { |
| 1608 | if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 1609 | auto DVar = Stack->getTopDSA(FD, false); |
| 1610 | // Check if the variable has explicit DSA set and stop analysis if it |
| 1611 | // so. |
| 1612 | if (DVar.RefExpr) |
| 1613 | return; |
| 1614 | |
| 1615 | auto ELoc = E->getExprLoc(); |
| 1616 | auto DKind = Stack->getCurrentDirective(); |
| 1617 | // OpenMP [2.9.3.6, Restrictions, p.2] |
| 1618 | // A list item that appears in a reduction clause of the innermost |
| 1619 | // enclosing worksharing or parallel construct may not be accessed in |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 1620 | // an explicit task. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1621 | DVar = Stack->hasInnermostDSA( |
| 1622 | FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, |
| 1623 | [](OpenMPDirectiveKind K) -> bool { |
| 1624 | return isOpenMPParallelDirective(K) || |
| 1625 | isOpenMPWorksharingDirective(K) || |
| 1626 | isOpenMPTeamsDirective(K); |
| 1627 | }, |
| 1628 | false); |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1629 | if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1630 | ErrorFound = true; |
| 1631 | SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); |
| 1632 | ReportOriginalDSA(SemaRef, Stack, FD, DVar); |
| 1633 | return; |
| 1634 | } |
| 1635 | |
| 1636 | // Define implicit data-sharing attributes for task. |
| 1637 | DVar = Stack->getImplicitDSA(FD, false); |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1638 | if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && |
| 1639 | !Stack->isLoopControlVariable(FD).first) |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1640 | ImplicitFirstprivate.push_back(E); |
| 1641 | } |
Alexey Bataev | 7fcacd8 | 2016-11-28 15:55:15 +0000 | [diff] [blame] | 1642 | } else |
| 1643 | Visit(E->getBase()); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1644 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1645 | void VisitOMPExecutableDirective(OMPExecutableDirective *S) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1646 | for (auto *C : S->clauses()) { |
| 1647 | // Skip analysis of arguments of implicitly defined firstprivate clause |
| 1648 | // for task directives. |
| 1649 | if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid())) |
| 1650 | for (auto *CC : C->children()) { |
| 1651 | if (CC) |
| 1652 | Visit(CC); |
| 1653 | } |
| 1654 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1655 | } |
| 1656 | void VisitStmt(Stmt *S) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1657 | for (auto *C : S->children()) { |
| 1658 | if (C && !isa<OMPExecutableDirective>(C)) |
| 1659 | Visit(C); |
| 1660 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1661 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1662 | |
| 1663 | bool isErrorFound() { return ErrorFound; } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1664 | ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1665 | llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() { |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1666 | return VarsWithInheritedDSA; |
| 1667 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1668 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1669 | DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) |
| 1670 | : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1671 | }; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1672 | } // namespace |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1673 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1674 | void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1675 | switch (DKind) { |
Kelvin Li | 70a12c5 | 2016-07-13 21:51:49 +0000 | [diff] [blame] | 1676 | case OMPD_parallel: |
| 1677 | case OMPD_parallel_for: |
| 1678 | case OMPD_parallel_for_simd: |
| 1679 | case OMPD_parallel_sections: |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 1680 | case OMPD_teams: { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1681 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1682 | QualType KmpInt32PtrTy = |
| 1683 | Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1684 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1685 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1686 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1687 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1688 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1689 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1690 | Params); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1691 | break; |
| 1692 | } |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 1693 | case OMPD_target_teams: |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1694 | case OMPD_target_parallel: { |
| 1695 | Sema::CapturedParamNameType ParamsTarget[] = { |
| 1696 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1697 | }; |
| 1698 | // Start a captured region for 'target' with no implicit parameters. |
| 1699 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1700 | ParamsTarget); |
| 1701 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 1702 | QualType KmpInt32PtrTy = |
| 1703 | Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 1704 | Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1705 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1706 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1707 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1708 | }; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 1709 | // Start a captured region for 'teams' or 'parallel'. Both regions have |
| 1710 | // the same implicit parameters. |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1711 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 1712 | ParamsTeamsOrParallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1713 | break; |
| 1714 | } |
Kelvin Li | 70a12c5 | 2016-07-13 21:51:49 +0000 | [diff] [blame] | 1715 | case OMPD_simd: |
| 1716 | case OMPD_for: |
| 1717 | case OMPD_for_simd: |
| 1718 | case OMPD_sections: |
| 1719 | case OMPD_section: |
| 1720 | case OMPD_single: |
| 1721 | case OMPD_master: |
| 1722 | case OMPD_critical: |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 1723 | case OMPD_taskgroup: |
| 1724 | case OMPD_distribute: |
Kelvin Li | 70a12c5 | 2016-07-13 21:51:49 +0000 | [diff] [blame] | 1725 | case OMPD_ordered: |
| 1726 | case OMPD_atomic: |
| 1727 | case OMPD_target_data: |
| 1728 | case OMPD_target: |
Kelvin Li | 70a12c5 | 2016-07-13 21:51:49 +0000 | [diff] [blame] | 1729 | case OMPD_target_parallel_for: |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 1730 | case OMPD_target_parallel_for_simd: |
| 1731 | case OMPD_target_simd: { |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1732 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1733 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1734 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1735 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1736 | Params); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1737 | break; |
| 1738 | } |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1739 | case OMPD_task: { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1740 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1741 | QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; |
| 1742 | FunctionProtoType::ExtProtoInfo EPI; |
| 1743 | EPI.Variadic = true; |
| 1744 | QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1745 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1746 | std::make_pair(".global_tid.", KmpInt32Ty), |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1747 | std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), |
| 1748 | std::make_pair(".privates.", Context.VoidPtrTy.withConst()), |
| 1749 | std::make_pair(".copy_fn.", |
| 1750 | Context.getPointerType(CopyFnType).withConst()), |
| 1751 | std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1752 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1753 | }; |
| 1754 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1755 | Params); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1756 | // Mark this captured region as inlined, because we don't use outlined |
| 1757 | // function directly. |
| 1758 | getCurCapturedRegion()->TheCapturedDecl->addAttr( |
| 1759 | AlwaysInlineAttr::CreateImplicit( |
| 1760 | Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1761 | break; |
| 1762 | } |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 1763 | case OMPD_taskloop: |
| 1764 | case OMPD_taskloop_simd: { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1765 | QualType KmpInt32Ty = |
| 1766 | Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1767 | QualType KmpUInt64Ty = |
| 1768 | Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 1769 | QualType KmpInt64Ty = |
| 1770 | Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 1771 | QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; |
| 1772 | FunctionProtoType::ExtProtoInfo EPI; |
| 1773 | EPI.Variadic = true; |
| 1774 | QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 1775 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1776 | std::make_pair(".global_tid.", KmpInt32Ty), |
| 1777 | std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), |
| 1778 | std::make_pair(".privates.", |
| 1779 | Context.VoidPtrTy.withConst().withRestrict()), |
| 1780 | std::make_pair( |
| 1781 | ".copy_fn.", |
| 1782 | Context.getPointerType(CopyFnType).withConst().withRestrict()), |
| 1783 | std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), |
| 1784 | std::make_pair(".lb.", KmpUInt64Ty), |
| 1785 | std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty), |
| 1786 | std::make_pair(".liter.", KmpInt32Ty), |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 1787 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1788 | }; |
| 1789 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1790 | Params); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1791 | // Mark this captured region as inlined, because we don't use outlined |
| 1792 | // function directly. |
| 1793 | getCurCapturedRegion()->TheCapturedDecl->addAttr( |
| 1794 | AlwaysInlineAttr::CreateImplicit( |
| 1795 | Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 1796 | break; |
| 1797 | } |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 1798 | case OMPD_distribute_parallel_for_simd: |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 1799 | case OMPD_distribute_simd: |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 1800 | case OMPD_distribute_parallel_for: |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 1801 | case OMPD_teams_distribute: |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 1802 | case OMPD_teams_distribute_simd: |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 1803 | case OMPD_teams_distribute_parallel_for_simd: |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 1804 | case OMPD_teams_distribute_parallel_for: |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 1805 | case OMPD_target_teams_distribute: |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 1806 | case OMPD_target_teams_distribute_parallel_for: |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 1807 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 1808 | case OMPD_target_teams_distribute_simd: { |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 1809 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 1810 | QualType KmpInt32PtrTy = |
| 1811 | Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); |
| 1812 | Sema::CapturedParamNameType Params[] = { |
| 1813 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1814 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1815 | std::make_pair(".previous.lb.", Context.getSizeType()), |
| 1816 | std::make_pair(".previous.ub.", Context.getSizeType()), |
| 1817 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1818 | }; |
| 1819 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1820 | Params); |
| 1821 | break; |
| 1822 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1823 | case OMPD_threadprivate: |
Alexey Bataev | ee9af45 | 2014-11-21 11:33:46 +0000 | [diff] [blame] | 1824 | case OMPD_taskyield: |
| 1825 | case OMPD_barrier: |
| 1826 | case OMPD_taskwait: |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1827 | case OMPD_cancellation_point: |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 1828 | case OMPD_cancel: |
Alexey Bataev | ee9af45 | 2014-11-21 11:33:46 +0000 | [diff] [blame] | 1829 | case OMPD_flush: |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 1830 | case OMPD_target_enter_data: |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 1831 | case OMPD_target_exit_data: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1832 | case OMPD_declare_reduction: |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 1833 | case OMPD_declare_simd: |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 1834 | case OMPD_declare_target: |
| 1835 | case OMPD_end_declare_target: |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 1836 | case OMPD_target_update: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1837 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 1838 | case OMPD_unknown: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1839 | llvm_unreachable("Unknown OpenMP directive"); |
| 1840 | } |
| 1841 | } |
| 1842 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1843 | int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { |
| 1844 | SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 1845 | getOpenMPCaptureRegions(CaptureRegions, DKind); |
| 1846 | return CaptureRegions.size(); |
| 1847 | } |
| 1848 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1849 | static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 1850 | Expr *CaptureExpr, bool WithInit, |
| 1851 | bool AsExpression) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 1852 | assert(CaptureExpr); |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1853 | ASTContext &C = S.getASTContext(); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 1854 | Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1855 | QualType Ty = Init->getType(); |
| 1856 | if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { |
| 1857 | if (S.getLangOpts().CPlusPlus) |
| 1858 | Ty = C.getLValueReferenceType(Ty); |
| 1859 | else { |
| 1860 | Ty = C.getPointerType(Ty); |
| 1861 | ExprResult Res = |
| 1862 | S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); |
| 1863 | if (!Res.isUsable()) |
| 1864 | return nullptr; |
| 1865 | Init = Res.get(); |
| 1866 | } |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1867 | WithInit = true; |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1868 | } |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 1869 | auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, |
| 1870 | CaptureExpr->getLocStart()); |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 1871 | if (!WithInit) |
| 1872 | CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange())); |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1873 | S.CurContext->addHiddenDecl(CED); |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1874 | S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1875 | return CED; |
| 1876 | } |
| 1877 | |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1878 | static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, |
| 1879 | bool WithInit) { |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 1880 | OMPCapturedExprDecl *CD; |
| 1881 | if (auto *VD = S.IsOpenMPCapturedDecl(D)) |
| 1882 | CD = cast<OMPCapturedExprDecl>(VD); |
| 1883 | else |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 1884 | CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, |
| 1885 | /*AsExpression=*/false); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1886 | return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 1887 | CaptureExpr->getExprLoc()); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 1890 | static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { |
| 1891 | if (!Ref) { |
| 1892 | auto *CD = |
| 1893 | buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."), |
| 1894 | CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true); |
| 1895 | Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), |
| 1896 | CaptureExpr->getExprLoc()); |
| 1897 | } |
| 1898 | ExprResult Res = Ref; |
| 1899 | if (!S.getLangOpts().CPlusPlus && |
| 1900 | CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && |
| 1901 | Ref->getType()->isPointerType()) |
| 1902 | Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); |
| 1903 | if (!Res.isUsable()) |
| 1904 | return ExprError(); |
| 1905 | return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get()); |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1908 | namespace { |
| 1909 | // OpenMP directives parsed in this section are represented as a |
| 1910 | // CapturedStatement with an associated statement. If a syntax error |
| 1911 | // is detected during the parsing of the associated statement, the |
| 1912 | // compiler must abort processing and close the CapturedStatement. |
| 1913 | // |
| 1914 | // Combined directives such as 'target parallel' have more than one |
| 1915 | // nested CapturedStatements. This RAII ensures that we unwind out |
| 1916 | // of all the nested CapturedStatements when an error is found. |
| 1917 | class CaptureRegionUnwinderRAII { |
| 1918 | private: |
| 1919 | Sema &S; |
| 1920 | bool &ErrorFound; |
| 1921 | OpenMPDirectiveKind DKind; |
| 1922 | |
| 1923 | public: |
| 1924 | CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, |
| 1925 | OpenMPDirectiveKind DKind) |
| 1926 | : S(S), ErrorFound(ErrorFound), DKind(DKind) {} |
| 1927 | ~CaptureRegionUnwinderRAII() { |
| 1928 | if (ErrorFound) { |
| 1929 | int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); |
| 1930 | while (--ThisCaptureLevel >= 0) |
| 1931 | S.ActOnCapturedRegionError(); |
| 1932 | } |
| 1933 | } |
| 1934 | }; |
| 1935 | } // namespace |
| 1936 | |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1937 | StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, |
| 1938 | ArrayRef<OMPClause *> Clauses) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1939 | bool ErrorFound = false; |
| 1940 | CaptureRegionUnwinderRAII CaptureRegionUnwinder( |
| 1941 | *this, ErrorFound, DSAStack->getCurrentDirective()); |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1942 | if (!S.isUsable()) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1943 | ErrorFound = true; |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1944 | return StmtError(); |
| 1945 | } |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 1946 | |
| 1947 | OMPOrderedClause *OC = nullptr; |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 1948 | OMPScheduleClause *SC = nullptr; |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 1949 | SmallVector<OMPLinearClause *, 4> LCs; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 1950 | SmallVector<OMPClauseWithPreInit *, 8> PICs; |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1951 | // This is required for proper codegen. |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1952 | for (auto *Clause : Clauses) { |
Alexey Bataev | 16dc7b6 | 2015-05-20 03:46:04 +0000 | [diff] [blame] | 1953 | if (isOpenMPPrivate(Clause->getClauseKind()) || |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 1954 | Clause->getClauseKind() == OMPC_copyprivate || |
| 1955 | (getLangOpts().OpenMPUseTLS && |
| 1956 | getASTContext().getTargetInfo().isTLSSupported() && |
| 1957 | Clause->getClauseKind() == OMPC_copyin)) { |
| 1958 | DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1959 | // Mark all variables in private list clauses as used in inner region. |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1960 | for (auto *VarRef : Clause->children()) { |
| 1961 | if (auto *E = cast_or_null<Expr>(VarRef)) { |
Alexey Bataev | 8bf6b3e | 2015-04-02 13:07:08 +0000 | [diff] [blame] | 1962 | MarkDeclarationsReferencedInExpr(E); |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 1965 | DSAStack->setForceVarCapturing(/*V=*/false); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1966 | } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 1967 | if (auto *C = OMPClauseWithPreInit::get(Clause)) |
| 1968 | PICs.push_back(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1969 | if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { |
| 1970 | if (auto *E = C->getPostUpdateExpr()) |
| 1971 | MarkDeclarationsReferencedInExpr(E); |
| 1972 | } |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1973 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 1974 | if (Clause->getClauseKind() == OMPC_schedule) |
| 1975 | SC = cast<OMPScheduleClause>(Clause); |
| 1976 | else if (Clause->getClauseKind() == OMPC_ordered) |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 1977 | OC = cast<OMPOrderedClause>(Clause); |
| 1978 | else if (Clause->getClauseKind() == OMPC_linear) |
| 1979 | LCs.push_back(cast<OMPLinearClause>(Clause)); |
| 1980 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 1981 | // OpenMP, 2.7.1 Loop Construct, Restrictions |
| 1982 | // The nonmonotonic modifier cannot be specified if an ordered clause is |
| 1983 | // specified. |
| 1984 | if (SC && |
| 1985 | (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || |
| 1986 | SC->getSecondScheduleModifier() == |
| 1987 | OMPC_SCHEDULE_MODIFIER_nonmonotonic) && |
| 1988 | OC) { |
| 1989 | Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic |
| 1990 | ? SC->getFirstScheduleModifierLoc() |
| 1991 | : SC->getSecondScheduleModifierLoc(), |
| 1992 | diag::err_omp_schedule_nonmonotonic_ordered) |
| 1993 | << SourceRange(OC->getLocStart(), OC->getLocEnd()); |
| 1994 | ErrorFound = true; |
| 1995 | } |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 1996 | if (!LCs.empty() && OC && OC->getNumForLoops()) { |
| 1997 | for (auto *C : LCs) { |
| 1998 | Diag(C->getLocStart(), diag::err_omp_linear_ordered) |
| 1999 | << SourceRange(OC->getLocStart(), OC->getLocEnd()); |
| 2000 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 2001 | ErrorFound = true; |
| 2002 | } |
Alexey Bataev | 113438c | 2015-12-30 12:06:23 +0000 | [diff] [blame] | 2003 | if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && |
| 2004 | isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && |
| 2005 | OC->getNumForLoops()) { |
| 2006 | Diag(OC->getLocStart(), diag::err_omp_ordered_simd) |
| 2007 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 2008 | ErrorFound = true; |
| 2009 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 2010 | if (ErrorFound) { |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 2011 | return StmtError(); |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 2012 | } |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 2013 | StmtResult SR = S; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 2014 | SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 2015 | getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); |
| 2016 | for (auto ThisCaptureRegion : llvm::reverse(CaptureRegions)) { |
| 2017 | // Mark all variables in private list clauses as used in inner region. |
| 2018 | // Required for proper codegen of combined directives. |
| 2019 | // TODO: add processing for other clauses. |
| 2020 | if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { |
| 2021 | for (auto *C : PICs) { |
| 2022 | OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); |
| 2023 | // Find the particular capture region for the clause if the |
| 2024 | // directive is a combined one with multiple capture regions. |
| 2025 | // If the directive is not a combined one, the capture region |
| 2026 | // associated with the clause is OMPD_unknown and is generated |
| 2027 | // only once. |
| 2028 | if (CaptureRegion == ThisCaptureRegion || |
| 2029 | CaptureRegion == OMPD_unknown) { |
| 2030 | if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { |
| 2031 | for (auto *D : DS->decls()) |
| 2032 | MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); |
| 2033 | } |
| 2034 | } |
| 2035 | } |
| 2036 | } |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 2037 | SR = ActOnCapturedRegionEnd(SR.get()); |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 2038 | } |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 2039 | return SR; |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Jonas Hahnfeld | 64a9e3c | 2017-02-22 06:49:10 +0000 | [diff] [blame] | 2042 | static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, |
| 2043 | OpenMPDirectiveKind CancelRegion, |
| 2044 | SourceLocation StartLoc) { |
| 2045 | // CancelRegion is only needed for cancel and cancellation_point. |
| 2046 | if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) |
| 2047 | return false; |
| 2048 | |
| 2049 | if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || |
| 2050 | CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) |
| 2051 | return false; |
| 2052 | |
| 2053 | SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) |
| 2054 | << getOpenMPDirectiveName(CancelRegion); |
| 2055 | return true; |
| 2056 | } |
| 2057 | |
| 2058 | static bool checkNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2059 | OpenMPDirectiveKind CurrentRegion, |
| 2060 | const DeclarationNameInfo &CurrentName, |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2061 | OpenMPDirectiveKind CancelRegion, |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2062 | SourceLocation StartLoc) { |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2063 | if (Stack->getCurScope()) { |
| 2064 | auto ParentRegion = Stack->getParentDirective(); |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 2065 | auto OffendingRegion = ParentRegion; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2066 | bool NestingProhibited = false; |
| 2067 | bool CloseNesting = true; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2068 | bool OrphanSeen = false; |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2069 | enum { |
| 2070 | NoRecommend, |
| 2071 | ShouldBeInParallelRegion, |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2072 | ShouldBeInOrderedRegion, |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2073 | ShouldBeInTargetRegion, |
| 2074 | ShouldBeInTeamsRegion |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2075 | } Recommend = NoRecommend; |
Kelvin Li | fd8b574 | 2016-07-01 14:30:25 +0000 | [diff] [blame] | 2076 | if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2077 | // OpenMP [2.16, Nesting of Regions] |
| 2078 | // OpenMP constructs may not be nested inside a simd region. |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2079 | // OpenMP [2.8.1,simd Construct, Restrictions] |
Kelvin Li | fd8b574 | 2016-07-01 14:30:25 +0000 | [diff] [blame] | 2080 | // An ordered construct with the simd clause is the only OpenMP |
| 2081 | // construct that can appear in the simd region. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2082 | // Allowing a SIMD construct nested in another SIMD construct is an |
Kelvin Li | fd8b574 | 2016-07-01 14:30:25 +0000 | [diff] [blame] | 2083 | // extension. The OpenMP 4.5 spec does not allow it. Issue a warning |
| 2084 | // message. |
| 2085 | SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) |
| 2086 | ? diag::err_omp_prohibited_region_simd |
| 2087 | : diag::warn_omp_nesting_simd); |
| 2088 | return CurrentRegion != OMPD_simd; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2089 | } |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2090 | if (ParentRegion == OMPD_atomic) { |
| 2091 | // OpenMP [2.16, Nesting of Regions] |
| 2092 | // OpenMP constructs may not be nested inside an atomic region. |
| 2093 | SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); |
| 2094 | return true; |
| 2095 | } |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2096 | if (CurrentRegion == OMPD_section) { |
| 2097 | // OpenMP [2.7.2, sections Construct, Restrictions] |
| 2098 | // Orphaned section directives are prohibited. That is, the section |
| 2099 | // directives must appear within the sections construct and must not be |
| 2100 | // encountered elsewhere in the sections region. |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2101 | if (ParentRegion != OMPD_sections && |
| 2102 | ParentRegion != OMPD_parallel_sections) { |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2103 | SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) |
| 2104 | << (ParentRegion != OMPD_unknown) |
| 2105 | << getOpenMPDirectiveName(ParentRegion); |
| 2106 | return true; |
| 2107 | } |
| 2108 | return false; |
| 2109 | } |
Kelvin Li | 2b51f72 | 2016-07-26 04:32:50 +0000 | [diff] [blame] | 2110 | // Allow some constructs (except teams) to be orphaned (they could be |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2111 | // used in functions, called from OpenMP regions with the required |
Kelvin Li | 2b51f72 | 2016-07-26 04:32:50 +0000 | [diff] [blame] | 2112 | // preconditions). |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2113 | if (ParentRegion == OMPD_unknown && |
| 2114 | !isOpenMPNestingTeamsDirective(CurrentRegion)) |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2115 | return false; |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2116 | if (CurrentRegion == OMPD_cancellation_point || |
| 2117 | CurrentRegion == OMPD_cancel) { |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2118 | // OpenMP [2.16, Nesting of Regions] |
| 2119 | // A cancellation point construct for which construct-type-clause is |
| 2120 | // taskgroup must be nested inside a task construct. A cancellation |
| 2121 | // point construct for which construct-type-clause is not taskgroup must |
| 2122 | // be closely nested inside an OpenMP construct that matches the type |
| 2123 | // specified in construct-type-clause. |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2124 | // A cancel construct for which construct-type-clause is taskgroup must be |
| 2125 | // nested inside a task construct. A cancel construct for which |
| 2126 | // construct-type-clause is not taskgroup must be closely nested inside an |
| 2127 | // OpenMP construct that matches the type specified in |
| 2128 | // construct-type-clause. |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2129 | NestingProhibited = |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2130 | !((CancelRegion == OMPD_parallel && |
| 2131 | (ParentRegion == OMPD_parallel || |
| 2132 | ParentRegion == OMPD_target_parallel)) || |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2133 | (CancelRegion == OMPD_for && |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2134 | (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || |
| 2135 | ParentRegion == OMPD_target_parallel_for)) || |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2136 | (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || |
| 2137 | (CancelRegion == OMPD_sections && |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2138 | (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || |
| 2139 | ParentRegion == OMPD_parallel_sections))); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2140 | } else if (CurrentRegion == OMPD_master) { |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2141 | // OpenMP [2.16, Nesting of Regions] |
| 2142 | // A master region may not be closely nested inside a worksharing, |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2143 | // atomic, or explicit task region. |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2144 | NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 2145 | isOpenMPTaskingDirective(ParentRegion); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2146 | } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { |
| 2147 | // OpenMP [2.16, Nesting of Regions] |
| 2148 | // A critical region may not be nested (closely or otherwise) inside a |
| 2149 | // critical region with the same name. Note that this restriction is not |
| 2150 | // sufficient to prevent deadlock. |
| 2151 | SourceLocation PreviousCriticalLoc; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2152 | bool DeadLock = Stack->hasDirective( |
| 2153 | [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, |
| 2154 | const DeclarationNameInfo &DNI, |
| 2155 | SourceLocation Loc) -> bool { |
| 2156 | if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { |
| 2157 | PreviousCriticalLoc = Loc; |
| 2158 | return true; |
| 2159 | } else |
| 2160 | return false; |
| 2161 | }, |
| 2162 | false /* skip top directive */); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2163 | if (DeadLock) { |
| 2164 | SemaRef.Diag(StartLoc, |
| 2165 | diag::err_omp_prohibited_region_critical_same_name) |
| 2166 | << CurrentName.getName(); |
| 2167 | if (PreviousCriticalLoc.isValid()) |
| 2168 | SemaRef.Diag(PreviousCriticalLoc, |
| 2169 | diag::note_omp_previous_critical_region); |
| 2170 | return true; |
| 2171 | } |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2172 | } else if (CurrentRegion == OMPD_barrier) { |
| 2173 | // OpenMP [2.16, Nesting of Regions] |
| 2174 | // A barrier region may not be closely nested inside a worksharing, |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2175 | // explicit task, critical, ordered, atomic, or master region. |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 2176 | NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || |
| 2177 | isOpenMPTaskingDirective(ParentRegion) || |
| 2178 | ParentRegion == OMPD_master || |
| 2179 | ParentRegion == OMPD_critical || |
| 2180 | ParentRegion == OMPD_ordered; |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2181 | } else if (isOpenMPWorksharingDirective(CurrentRegion) && |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2182 | !isOpenMPParallelDirective(CurrentRegion) && |
| 2183 | !isOpenMPTeamsDirective(CurrentRegion)) { |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2184 | // OpenMP [2.16, Nesting of Regions] |
| 2185 | // A worksharing region may not be closely nested inside a worksharing, |
| 2186 | // explicit task, critical, ordered, atomic, or master region. |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 2187 | NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || |
| 2188 | isOpenMPTaskingDirective(ParentRegion) || |
| 2189 | ParentRegion == OMPD_master || |
| 2190 | ParentRegion == OMPD_critical || |
| 2191 | ParentRegion == OMPD_ordered; |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2192 | Recommend = ShouldBeInParallelRegion; |
| 2193 | } else if (CurrentRegion == OMPD_ordered) { |
| 2194 | // OpenMP [2.16, Nesting of Regions] |
| 2195 | // An ordered region may not be closely nested inside a critical, |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2196 | // atomic, or explicit task region. |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2197 | // An ordered region must be closely nested inside a loop region (or |
| 2198 | // parallel loop region) with an ordered clause. |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2199 | // OpenMP [2.8.1,simd Construct, Restrictions] |
| 2200 | // An ordered construct with the simd clause is the only OpenMP construct |
| 2201 | // that can appear in the simd region. |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2202 | NestingProhibited = ParentRegion == OMPD_critical || |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 2203 | isOpenMPTaskingDirective(ParentRegion) || |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2204 | !(isOpenMPSimdDirective(ParentRegion) || |
| 2205 | Stack->isParentOrderedRegion()); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2206 | Recommend = ShouldBeInOrderedRegion; |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2207 | } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2208 | // OpenMP [2.16, Nesting of Regions] |
| 2209 | // If specified, a teams construct must be contained within a target |
| 2210 | // construct. |
| 2211 | NestingProhibited = ParentRegion != OMPD_target; |
Kelvin Li | 2b51f72 | 2016-07-26 04:32:50 +0000 | [diff] [blame] | 2212 | OrphanSeen = ParentRegion == OMPD_unknown; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2213 | Recommend = ShouldBeInTargetRegion; |
| 2214 | Stack->setParentTeamsRegionLoc(Stack->getConstructLoc()); |
| 2215 | } |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2216 | if (!NestingProhibited && |
| 2217 | !isOpenMPTargetExecutionDirective(CurrentRegion) && |
| 2218 | !isOpenMPTargetDataManagementDirective(CurrentRegion) && |
| 2219 | (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2220 | // OpenMP [2.16, Nesting of Regions] |
| 2221 | // distribute, parallel, parallel sections, parallel workshare, and the |
| 2222 | // parallel loop and parallel loop SIMD constructs are the only OpenMP |
| 2223 | // constructs that can be closely nested in the teams region. |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2224 | NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && |
| 2225 | !isOpenMPDistributeDirective(CurrentRegion); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2226 | Recommend = ShouldBeInParallelRegion; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2227 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2228 | if (!NestingProhibited && |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2229 | isOpenMPNestingDistributeDirective(CurrentRegion)) { |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2230 | // OpenMP 4.5 [2.17 Nesting of Regions] |
| 2231 | // The region associated with the distribute construct must be strictly |
| 2232 | // nested inside a teams region |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2233 | NestingProhibited = |
| 2234 | (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2235 | Recommend = ShouldBeInTeamsRegion; |
| 2236 | } |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 2237 | if (!NestingProhibited && |
| 2238 | (isOpenMPTargetExecutionDirective(CurrentRegion) || |
| 2239 | isOpenMPTargetDataManagementDirective(CurrentRegion))) { |
| 2240 | // OpenMP 4.5 [2.17 Nesting of Regions] |
| 2241 | // If a target, target update, target data, target enter data, or |
| 2242 | // target exit data construct is encountered during execution of a |
| 2243 | // target region, the behavior is unspecified. |
| 2244 | NestingProhibited = Stack->hasDirective( |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 2245 | [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, |
| 2246 | SourceLocation) -> bool { |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 2247 | if (isOpenMPTargetExecutionDirective(K)) { |
| 2248 | OffendingRegion = K; |
| 2249 | return true; |
| 2250 | } else |
| 2251 | return false; |
| 2252 | }, |
| 2253 | false /* don't skip top directive */); |
| 2254 | CloseNesting = false; |
| 2255 | } |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2256 | if (NestingProhibited) { |
Kelvin Li | 2b51f72 | 2016-07-26 04:32:50 +0000 | [diff] [blame] | 2257 | if (OrphanSeen) { |
| 2258 | SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) |
| 2259 | << getOpenMPDirectiveName(CurrentRegion) << Recommend; |
| 2260 | } else { |
| 2261 | SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) |
| 2262 | << CloseNesting << getOpenMPDirectiveName(OffendingRegion) |
| 2263 | << Recommend << getOpenMPDirectiveName(CurrentRegion); |
| 2264 | } |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2265 | return true; |
| 2266 | } |
| 2267 | } |
| 2268 | return false; |
| 2269 | } |
| 2270 | |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2271 | static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, |
| 2272 | ArrayRef<OMPClause *> Clauses, |
| 2273 | ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { |
| 2274 | bool ErrorFound = false; |
| 2275 | unsigned NamedModifiersNumber = 0; |
| 2276 | SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( |
| 2277 | OMPD_unknown + 1); |
Alexey Bataev | ecb156a | 2015-09-15 17:23:56 +0000 | [diff] [blame] | 2278 | SmallVector<SourceLocation, 4> NameModifierLoc; |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2279 | for (const auto *C : Clauses) { |
| 2280 | if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { |
| 2281 | // At most one if clause without a directive-name-modifier can appear on |
| 2282 | // the directive. |
| 2283 | OpenMPDirectiveKind CurNM = IC->getNameModifier(); |
| 2284 | if (FoundNameModifiers[CurNM]) { |
| 2285 | S.Diag(C->getLocStart(), diag::err_omp_more_one_clause) |
| 2286 | << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) |
| 2287 | << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); |
| 2288 | ErrorFound = true; |
Alexey Bataev | ecb156a | 2015-09-15 17:23:56 +0000 | [diff] [blame] | 2289 | } else if (CurNM != OMPD_unknown) { |
| 2290 | NameModifierLoc.push_back(IC->getNameModifierLoc()); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2291 | ++NamedModifiersNumber; |
Alexey Bataev | ecb156a | 2015-09-15 17:23:56 +0000 | [diff] [blame] | 2292 | } |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2293 | FoundNameModifiers[CurNM] = IC; |
| 2294 | if (CurNM == OMPD_unknown) |
| 2295 | continue; |
| 2296 | // Check if the specified name modifier is allowed for the current |
| 2297 | // directive. |
| 2298 | // At most one if clause with the particular directive-name-modifier can |
| 2299 | // appear on the directive. |
| 2300 | bool MatchFound = false; |
| 2301 | for (auto NM : AllowedNameModifiers) { |
| 2302 | if (CurNM == NM) { |
| 2303 | MatchFound = true; |
| 2304 | break; |
| 2305 | } |
| 2306 | } |
| 2307 | if (!MatchFound) { |
| 2308 | S.Diag(IC->getNameModifierLoc(), |
| 2309 | diag::err_omp_wrong_if_directive_name_modifier) |
| 2310 | << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); |
| 2311 | ErrorFound = true; |
| 2312 | } |
| 2313 | } |
| 2314 | } |
| 2315 | // If any if clause on the directive includes a directive-name-modifier then |
| 2316 | // all if clauses on the directive must include a directive-name-modifier. |
| 2317 | if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { |
| 2318 | if (NamedModifiersNumber == AllowedNameModifiers.size()) { |
| 2319 | S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(), |
| 2320 | diag::err_omp_no_more_if_clause); |
| 2321 | } else { |
| 2322 | std::string Values; |
| 2323 | std::string Sep(", "); |
| 2324 | unsigned AllowedCnt = 0; |
| 2325 | unsigned TotalAllowedNum = |
| 2326 | AllowedNameModifiers.size() - NamedModifiersNumber; |
| 2327 | for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; |
| 2328 | ++Cnt) { |
| 2329 | OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; |
| 2330 | if (!FoundNameModifiers[NM]) { |
| 2331 | Values += "'"; |
| 2332 | Values += getOpenMPDirectiveName(NM); |
| 2333 | Values += "'"; |
| 2334 | if (AllowedCnt + 2 == TotalAllowedNum) |
| 2335 | Values += " or "; |
| 2336 | else if (AllowedCnt + 1 != TotalAllowedNum) |
| 2337 | Values += Sep; |
| 2338 | ++AllowedCnt; |
| 2339 | } |
| 2340 | } |
| 2341 | S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(), |
| 2342 | diag::err_omp_unnamed_if_clause) |
| 2343 | << (TotalAllowedNum > 1) << Values; |
| 2344 | } |
Alexey Bataev | ecb156a | 2015-09-15 17:23:56 +0000 | [diff] [blame] | 2345 | for (auto Loc : NameModifierLoc) { |
| 2346 | S.Diag(Loc, diag::note_omp_previous_named_if_clause); |
| 2347 | } |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2348 | ErrorFound = true; |
| 2349 | } |
| 2350 | return ErrorFound; |
| 2351 | } |
| 2352 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2353 | StmtResult Sema::ActOnOpenMPExecutableDirective( |
| 2354 | OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, |
| 2355 | OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, |
| 2356 | Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2357 | StmtResult Res = StmtError(); |
Jonas Hahnfeld | 64a9e3c | 2017-02-22 06:49:10 +0000 | [diff] [blame] | 2358 | // First check CancelRegion which is then used in checkNestingOfRegions. |
| 2359 | if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || |
| 2360 | checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2361 | StartLoc)) |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2362 | return StmtError(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2363 | |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2364 | llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 2365 | llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2366 | bool ErrorFound = false; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2367 | ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2368 | if (AStmt) { |
| 2369 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 2370 | |
| 2371 | // Check default data sharing attributes for referenced variables. |
| 2372 | DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); |
Arpith Chacko Jacob | 1f46b70 | 2017-01-23 15:38:49 +0000 | [diff] [blame] | 2373 | int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); |
| 2374 | Stmt *S = AStmt; |
| 2375 | while (--ThisCaptureLevel >= 0) |
| 2376 | S = cast<CapturedStmt>(S)->getCapturedStmt(); |
| 2377 | DSAChecker.Visit(S); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2378 | if (DSAChecker.isErrorFound()) |
| 2379 | return StmtError(); |
| 2380 | // Generate list of implicitly defined firstprivate variables. |
| 2381 | VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2382 | |
| 2383 | if (!DSAChecker.getImplicitFirstprivate().empty()) { |
| 2384 | if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( |
| 2385 | DSAChecker.getImplicitFirstprivate(), SourceLocation(), |
| 2386 | SourceLocation(), SourceLocation())) { |
| 2387 | ClausesWithImplicit.push_back(Implicit); |
| 2388 | ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != |
| 2389 | DSAChecker.getImplicitFirstprivate().size(); |
| 2390 | } else |
| 2391 | ErrorFound = true; |
| 2392 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2393 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2394 | |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2395 | llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2396 | switch (Kind) { |
| 2397 | case OMPD_parallel: |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2398 | Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2399 | EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2400 | AllowedNameModifiers.push_back(OMPD_parallel); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2401 | break; |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2402 | case OMPD_simd: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2403 | Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, |
| 2404 | VarsWithInheritedDSA); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2405 | break; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2406 | case OMPD_for: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2407 | Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, |
| 2408 | VarsWithInheritedDSA); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2409 | break; |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2410 | case OMPD_for_simd: |
| 2411 | Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2412 | EndLoc, VarsWithInheritedDSA); |
| 2413 | break; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2414 | case OMPD_sections: |
| 2415 | Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2416 | EndLoc); |
| 2417 | break; |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2418 | case OMPD_section: |
| 2419 | assert(ClausesWithImplicit.empty() && |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2420 | "No clauses are allowed for 'omp section' directive"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2421 | Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); |
| 2422 | break; |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2423 | case OMPD_single: |
| 2424 | Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2425 | EndLoc); |
| 2426 | break; |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2427 | case OMPD_master: |
| 2428 | assert(ClausesWithImplicit.empty() && |
| 2429 | "No clauses are allowed for 'omp master' directive"); |
| 2430 | Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); |
| 2431 | break; |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2432 | case OMPD_critical: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2433 | Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, |
| 2434 | StartLoc, EndLoc); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2435 | break; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2436 | case OMPD_parallel_for: |
| 2437 | Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2438 | EndLoc, VarsWithInheritedDSA); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2439 | AllowedNameModifiers.push_back(OMPD_parallel); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2440 | break; |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2441 | case OMPD_parallel_for_simd: |
| 2442 | Res = ActOnOpenMPParallelForSimdDirective( |
| 2443 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2444 | AllowedNameModifiers.push_back(OMPD_parallel); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2445 | break; |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2446 | case OMPD_parallel_sections: |
| 2447 | Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, |
| 2448 | StartLoc, EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2449 | AllowedNameModifiers.push_back(OMPD_parallel); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2450 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2451 | case OMPD_task: |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2452 | Res = |
| 2453 | ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2454 | AllowedNameModifiers.push_back(OMPD_task); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2455 | break; |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2456 | case OMPD_taskyield: |
| 2457 | assert(ClausesWithImplicit.empty() && |
| 2458 | "No clauses are allowed for 'omp taskyield' directive"); |
| 2459 | assert(AStmt == nullptr && |
| 2460 | "No associated statement allowed for 'omp taskyield' directive"); |
| 2461 | Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); |
| 2462 | break; |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2463 | case OMPD_barrier: |
| 2464 | assert(ClausesWithImplicit.empty() && |
| 2465 | "No clauses are allowed for 'omp barrier' directive"); |
| 2466 | assert(AStmt == nullptr && |
| 2467 | "No associated statement allowed for 'omp barrier' directive"); |
| 2468 | Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); |
| 2469 | break; |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2470 | case OMPD_taskwait: |
| 2471 | assert(ClausesWithImplicit.empty() && |
| 2472 | "No clauses are allowed for 'omp taskwait' directive"); |
| 2473 | assert(AStmt == nullptr && |
| 2474 | "No associated statement allowed for 'omp taskwait' directive"); |
| 2475 | Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); |
| 2476 | break; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2477 | case OMPD_taskgroup: |
| 2478 | assert(ClausesWithImplicit.empty() && |
| 2479 | "No clauses are allowed for 'omp taskgroup' directive"); |
| 2480 | Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc); |
| 2481 | break; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2482 | case OMPD_flush: |
| 2483 | assert(AStmt == nullptr && |
| 2484 | "No associated statement allowed for 'omp flush' directive"); |
| 2485 | Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); |
| 2486 | break; |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2487 | case OMPD_ordered: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2488 | Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2489 | EndLoc); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2490 | break; |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2491 | case OMPD_atomic: |
| 2492 | Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2493 | EndLoc); |
| 2494 | break; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2495 | case OMPD_teams: |
| 2496 | Res = |
| 2497 | ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); |
| 2498 | break; |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2499 | case OMPD_target: |
| 2500 | Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2501 | EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2502 | AllowedNameModifiers.push_back(OMPD_target); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2503 | break; |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2504 | case OMPD_target_parallel: |
| 2505 | Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, |
| 2506 | StartLoc, EndLoc); |
| 2507 | AllowedNameModifiers.push_back(OMPD_target); |
| 2508 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2509 | break; |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2510 | case OMPD_target_parallel_for: |
| 2511 | Res = ActOnOpenMPTargetParallelForDirective( |
| 2512 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2513 | AllowedNameModifiers.push_back(OMPD_target); |
| 2514 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2515 | break; |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2516 | case OMPD_cancellation_point: |
| 2517 | assert(ClausesWithImplicit.empty() && |
| 2518 | "No clauses are allowed for 'omp cancellation point' directive"); |
| 2519 | assert(AStmt == nullptr && "No associated statement allowed for 'omp " |
| 2520 | "cancellation point' directive"); |
| 2521 | Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); |
| 2522 | break; |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2523 | case OMPD_cancel: |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2524 | assert(AStmt == nullptr && |
| 2525 | "No associated statement allowed for 'omp cancel' directive"); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 2526 | Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, |
| 2527 | CancelRegion); |
| 2528 | AllowedNameModifiers.push_back(OMPD_cancel); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2529 | break; |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2530 | case OMPD_target_data: |
| 2531 | Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2532 | EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2533 | AllowedNameModifiers.push_back(OMPD_target_data); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2534 | break; |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2535 | case OMPD_target_enter_data: |
| 2536 | Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, |
| 2537 | EndLoc); |
| 2538 | AllowedNameModifiers.push_back(OMPD_target_enter_data); |
| 2539 | break; |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2540 | case OMPD_target_exit_data: |
| 2541 | Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, |
| 2542 | EndLoc); |
| 2543 | AllowedNameModifiers.push_back(OMPD_target_exit_data); |
| 2544 | break; |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2545 | case OMPD_taskloop: |
| 2546 | Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2547 | EndLoc, VarsWithInheritedDSA); |
| 2548 | AllowedNameModifiers.push_back(OMPD_taskloop); |
| 2549 | break; |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2550 | case OMPD_taskloop_simd: |
| 2551 | Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2552 | EndLoc, VarsWithInheritedDSA); |
| 2553 | AllowedNameModifiers.push_back(OMPD_taskloop); |
| 2554 | break; |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2555 | case OMPD_distribute: |
| 2556 | Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2557 | EndLoc, VarsWithInheritedDSA); |
| 2558 | break; |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 2559 | case OMPD_target_update: |
| 2560 | assert(!AStmt && "Statement is not allowed for target update"); |
| 2561 | Res = |
| 2562 | ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc); |
| 2563 | AllowedNameModifiers.push_back(OMPD_target_update); |
| 2564 | break; |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2565 | case OMPD_distribute_parallel_for: |
| 2566 | Res = ActOnOpenMPDistributeParallelForDirective( |
| 2567 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2568 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2569 | break; |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2570 | case OMPD_distribute_parallel_for_simd: |
| 2571 | Res = ActOnOpenMPDistributeParallelForSimdDirective( |
| 2572 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2573 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2574 | break; |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2575 | case OMPD_distribute_simd: |
| 2576 | Res = ActOnOpenMPDistributeSimdDirective( |
| 2577 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2578 | break; |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2579 | case OMPD_target_parallel_for_simd: |
| 2580 | Res = ActOnOpenMPTargetParallelForSimdDirective( |
| 2581 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2582 | AllowedNameModifiers.push_back(OMPD_target); |
| 2583 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2584 | break; |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2585 | case OMPD_target_simd: |
| 2586 | Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2587 | EndLoc, VarsWithInheritedDSA); |
| 2588 | AllowedNameModifiers.push_back(OMPD_target); |
| 2589 | break; |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2590 | case OMPD_teams_distribute: |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2591 | Res = ActOnOpenMPTeamsDistributeDirective( |
| 2592 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2593 | break; |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2594 | case OMPD_teams_distribute_simd: |
| 2595 | Res = ActOnOpenMPTeamsDistributeSimdDirective( |
| 2596 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2597 | break; |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2598 | case OMPD_teams_distribute_parallel_for_simd: |
| 2599 | Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( |
| 2600 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2601 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2602 | break; |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2603 | case OMPD_teams_distribute_parallel_for: |
| 2604 | Res = ActOnOpenMPTeamsDistributeParallelForDirective( |
| 2605 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2606 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2607 | break; |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2608 | case OMPD_target_teams: |
| 2609 | Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2610 | EndLoc); |
| 2611 | AllowedNameModifiers.push_back(OMPD_target); |
| 2612 | break; |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2613 | case OMPD_target_teams_distribute: |
| 2614 | Res = ActOnOpenMPTargetTeamsDistributeDirective( |
| 2615 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2616 | AllowedNameModifiers.push_back(OMPD_target); |
| 2617 | break; |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2618 | case OMPD_target_teams_distribute_parallel_for: |
| 2619 | Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( |
| 2620 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2621 | AllowedNameModifiers.push_back(OMPD_target); |
| 2622 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2623 | break; |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2624 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 2625 | Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( |
| 2626 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2627 | AllowedNameModifiers.push_back(OMPD_target); |
| 2628 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2629 | break; |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 2630 | case OMPD_target_teams_distribute_simd: |
| 2631 | Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( |
| 2632 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2633 | AllowedNameModifiers.push_back(OMPD_target); |
| 2634 | break; |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 2635 | case OMPD_declare_target: |
| 2636 | case OMPD_end_declare_target: |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2637 | case OMPD_threadprivate: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2638 | case OMPD_declare_reduction: |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2639 | case OMPD_declare_simd: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2640 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 2641 | case OMPD_unknown: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2642 | llvm_unreachable("Unknown OpenMP directive"); |
| 2643 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2644 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2645 | for (auto P : VarsWithInheritedDSA) { |
| 2646 | Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) |
| 2647 | << P.first << P.second->getSourceRange(); |
| 2648 | } |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2649 | ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; |
| 2650 | |
| 2651 | if (!AllowedNameModifiers.empty()) |
| 2652 | ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || |
| 2653 | ErrorFound; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2654 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2655 | if (ErrorFound) |
| 2656 | return StmtError(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2657 | return Res; |
| 2658 | } |
| 2659 | |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2660 | Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( |
| 2661 | DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 2662 | ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2663 | ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, |
| 2664 | ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 2665 | assert(Aligneds.size() == Alignments.size()); |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2666 | assert(Linears.size() == LinModifiers.size()); |
| 2667 | assert(Linears.size() == Steps.size()); |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2668 | if (!DG || DG.get().isNull()) |
| 2669 | return DeclGroupPtrTy(); |
| 2670 | |
| 2671 | if (!DG.get().isSingleDecl()) { |
Alexey Bataev | 20dfd77 | 2016-04-04 10:12:15 +0000 | [diff] [blame] | 2672 | Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2673 | return DG; |
| 2674 | } |
| 2675 | auto *ADecl = DG.get().getSingleDecl(); |
| 2676 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) |
| 2677 | ADecl = FTD->getTemplatedDecl(); |
| 2678 | |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2679 | auto *FD = dyn_cast<FunctionDecl>(ADecl); |
| 2680 | if (!FD) { |
| 2681 | Diag(ADecl->getLocation(), diag::err_omp_function_expected); |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2682 | return DeclGroupPtrTy(); |
| 2683 | } |
| 2684 | |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 2685 | // OpenMP [2.8.2, declare simd construct, Description] |
| 2686 | // The parameter of the simdlen clause must be a constant positive integer |
| 2687 | // expression. |
| 2688 | ExprResult SL; |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2689 | if (Simdlen) |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 2690 | SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2691 | // OpenMP [2.8.2, declare simd construct, Description] |
| 2692 | // The special this pointer can be used as if was one of the arguments to the |
| 2693 | // function in any of the linear, aligned, or uniform clauses. |
| 2694 | // The uniform clause declares one or more arguments to have an invariant |
| 2695 | // value for all concurrent invocations of the function in the execution of a |
| 2696 | // single SIMD loop. |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2697 | llvm::DenseMap<Decl *, Expr *> UniformedArgs; |
| 2698 | Expr *UniformedLinearThis = nullptr; |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2699 | for (auto *E : Uniforms) { |
| 2700 | E = E->IgnoreParenImpCasts(); |
| 2701 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2702 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 2703 | if (FD->getNumParams() > PVD->getFunctionScopeIndex() && |
| 2704 | FD->getParamDecl(PVD->getFunctionScopeIndex()) |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2705 | ->getCanonicalDecl() == PVD->getCanonicalDecl()) { |
| 2706 | UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E)); |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2707 | continue; |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2708 | } |
| 2709 | if (isa<CXXThisExpr>(E)) { |
| 2710 | UniformedLinearThis = E; |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2711 | continue; |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2712 | } |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2713 | Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) |
| 2714 | << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 2715 | } |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 2716 | // OpenMP [2.8.2, declare simd construct, Description] |
| 2717 | // The aligned clause declares that the object to which each list item points |
| 2718 | // is aligned to the number of bytes expressed in the optional parameter of |
| 2719 | // the aligned clause. |
| 2720 | // The special this pointer can be used as if was one of the arguments to the |
| 2721 | // function in any of the linear, aligned, or uniform clauses. |
| 2722 | // The type of list items appearing in the aligned clause must be array, |
| 2723 | // pointer, reference to array, or reference to pointer. |
| 2724 | llvm::DenseMap<Decl *, Expr *> AlignedArgs; |
| 2725 | Expr *AlignedThis = nullptr; |
| 2726 | for (auto *E : Aligneds) { |
| 2727 | E = E->IgnoreParenImpCasts(); |
| 2728 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2729 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 2730 | auto *CanonPVD = PVD->getCanonicalDecl(); |
| 2731 | if (FD->getNumParams() > PVD->getFunctionScopeIndex() && |
| 2732 | FD->getParamDecl(PVD->getFunctionScopeIndex()) |
| 2733 | ->getCanonicalDecl() == CanonPVD) { |
| 2734 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 2735 | // A list-item cannot appear in more than one aligned clause. |
| 2736 | if (AlignedArgs.count(CanonPVD) > 0) { |
| 2737 | Diag(E->getExprLoc(), diag::err_omp_aligned_twice) |
| 2738 | << 1 << E->getSourceRange(); |
| 2739 | Diag(AlignedArgs[CanonPVD]->getExprLoc(), |
| 2740 | diag::note_omp_explicit_dsa) |
| 2741 | << getOpenMPClauseName(OMPC_aligned); |
| 2742 | continue; |
| 2743 | } |
| 2744 | AlignedArgs[CanonPVD] = E; |
| 2745 | QualType QTy = PVD->getType() |
| 2746 | .getNonReferenceType() |
| 2747 | .getUnqualifiedType() |
| 2748 | .getCanonicalType(); |
| 2749 | const Type *Ty = QTy.getTypePtrOrNull(); |
| 2750 | if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { |
| 2751 | Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) |
| 2752 | << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); |
| 2753 | Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; |
| 2754 | } |
| 2755 | continue; |
| 2756 | } |
| 2757 | } |
| 2758 | if (isa<CXXThisExpr>(E)) { |
| 2759 | if (AlignedThis) { |
| 2760 | Diag(E->getExprLoc(), diag::err_omp_aligned_twice) |
| 2761 | << 2 << E->getSourceRange(); |
| 2762 | Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) |
| 2763 | << getOpenMPClauseName(OMPC_aligned); |
| 2764 | } |
| 2765 | AlignedThis = E; |
| 2766 | continue; |
| 2767 | } |
| 2768 | Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) |
| 2769 | << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); |
| 2770 | } |
| 2771 | // The optional parameter of the aligned clause, alignment, must be a constant |
| 2772 | // positive integer expression. If no optional parameter is specified, |
| 2773 | // implementation-defined default alignments for SIMD instructions on the |
| 2774 | // target platforms are assumed. |
| 2775 | SmallVector<Expr *, 4> NewAligns; |
| 2776 | for (auto *E : Alignments) { |
| 2777 | ExprResult Align; |
| 2778 | if (E) |
| 2779 | Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); |
| 2780 | NewAligns.push_back(Align.get()); |
| 2781 | } |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2782 | // OpenMP [2.8.2, declare simd construct, Description] |
| 2783 | // The linear clause declares one or more list items to be private to a SIMD |
| 2784 | // lane and to have a linear relationship with respect to the iteration space |
| 2785 | // of a loop. |
| 2786 | // The special this pointer can be used as if was one of the arguments to the |
| 2787 | // function in any of the linear, aligned, or uniform clauses. |
| 2788 | // When a linear-step expression is specified in a linear clause it must be |
| 2789 | // either a constant integer expression or an integer-typed parameter that is |
| 2790 | // specified in a uniform clause on the directive. |
| 2791 | llvm::DenseMap<Decl *, Expr *> LinearArgs; |
| 2792 | const bool IsUniformedThis = UniformedLinearThis != nullptr; |
| 2793 | auto MI = LinModifiers.begin(); |
| 2794 | for (auto *E : Linears) { |
| 2795 | auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); |
| 2796 | ++MI; |
| 2797 | E = E->IgnoreParenImpCasts(); |
| 2798 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2799 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 2800 | auto *CanonPVD = PVD->getCanonicalDecl(); |
| 2801 | if (FD->getNumParams() > PVD->getFunctionScopeIndex() && |
| 2802 | FD->getParamDecl(PVD->getFunctionScopeIndex()) |
| 2803 | ->getCanonicalDecl() == CanonPVD) { |
| 2804 | // OpenMP [2.15.3.7, linear Clause, Restrictions] |
| 2805 | // A list-item cannot appear in more than one linear clause. |
| 2806 | if (LinearArgs.count(CanonPVD) > 0) { |
| 2807 | Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) |
| 2808 | << getOpenMPClauseName(OMPC_linear) |
| 2809 | << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); |
| 2810 | Diag(LinearArgs[CanonPVD]->getExprLoc(), |
| 2811 | diag::note_omp_explicit_dsa) |
| 2812 | << getOpenMPClauseName(OMPC_linear); |
| 2813 | continue; |
| 2814 | } |
| 2815 | // Each argument can appear in at most one uniform or linear clause. |
| 2816 | if (UniformedArgs.count(CanonPVD) > 0) { |
| 2817 | Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) |
| 2818 | << getOpenMPClauseName(OMPC_linear) |
| 2819 | << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); |
| 2820 | Diag(UniformedArgs[CanonPVD]->getExprLoc(), |
| 2821 | diag::note_omp_explicit_dsa) |
| 2822 | << getOpenMPClauseName(OMPC_uniform); |
| 2823 | continue; |
| 2824 | } |
| 2825 | LinearArgs[CanonPVD] = E; |
| 2826 | if (E->isValueDependent() || E->isTypeDependent() || |
| 2827 | E->isInstantiationDependent() || |
| 2828 | E->containsUnexpandedParameterPack()) |
| 2829 | continue; |
| 2830 | (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, |
| 2831 | PVD->getOriginalType()); |
| 2832 | continue; |
| 2833 | } |
| 2834 | } |
| 2835 | if (isa<CXXThisExpr>(E)) { |
| 2836 | if (UniformedLinearThis) { |
| 2837 | Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) |
| 2838 | << getOpenMPClauseName(OMPC_linear) |
| 2839 | << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) |
| 2840 | << E->getSourceRange(); |
| 2841 | Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) |
| 2842 | << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform |
| 2843 | : OMPC_linear); |
| 2844 | continue; |
| 2845 | } |
| 2846 | UniformedLinearThis = E; |
| 2847 | if (E->isValueDependent() || E->isTypeDependent() || |
| 2848 | E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) |
| 2849 | continue; |
| 2850 | (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, |
| 2851 | E->getType()); |
| 2852 | continue; |
| 2853 | } |
| 2854 | Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) |
| 2855 | << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); |
| 2856 | } |
| 2857 | Expr *Step = nullptr; |
| 2858 | Expr *NewStep = nullptr; |
| 2859 | SmallVector<Expr *, 4> NewSteps; |
| 2860 | for (auto *E : Steps) { |
| 2861 | // Skip the same step expression, it was checked already. |
| 2862 | if (Step == E || !E) { |
| 2863 | NewSteps.push_back(E ? NewStep : nullptr); |
| 2864 | continue; |
| 2865 | } |
| 2866 | Step = E; |
| 2867 | if (auto *DRE = dyn_cast<DeclRefExpr>(Step)) |
| 2868 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 2869 | auto *CanonPVD = PVD->getCanonicalDecl(); |
| 2870 | if (UniformedArgs.count(CanonPVD) == 0) { |
| 2871 | Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) |
| 2872 | << Step->getSourceRange(); |
| 2873 | } else if (E->isValueDependent() || E->isTypeDependent() || |
| 2874 | E->isInstantiationDependent() || |
| 2875 | E->containsUnexpandedParameterPack() || |
| 2876 | CanonPVD->getType()->hasIntegerRepresentation()) |
| 2877 | NewSteps.push_back(Step); |
| 2878 | else { |
| 2879 | Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) |
| 2880 | << Step->getSourceRange(); |
| 2881 | } |
| 2882 | continue; |
| 2883 | } |
| 2884 | NewStep = Step; |
| 2885 | if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && |
| 2886 | !Step->isInstantiationDependent() && |
| 2887 | !Step->containsUnexpandedParameterPack()) { |
| 2888 | NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) |
| 2889 | .get(); |
| 2890 | if (NewStep) |
| 2891 | NewStep = VerifyIntegerConstantExpression(NewStep).get(); |
| 2892 | } |
| 2893 | NewSteps.push_back(NewStep); |
| 2894 | } |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2895 | auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( |
| 2896 | Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 2897 | Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2898 | const_cast<Expr **>(NewAligns.data()), NewAligns.size(), |
| 2899 | const_cast<Expr **>(Linears.data()), Linears.size(), |
| 2900 | const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), |
| 2901 | NewSteps.data(), NewSteps.size(), SR); |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2902 | ADecl->addAttr(NewAttr); |
| 2903 | return ConvertDeclToDeclGroup(ADecl); |
| 2904 | } |
| 2905 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2906 | StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, |
| 2907 | Stmt *AStmt, |
| 2908 | SourceLocation StartLoc, |
| 2909 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2910 | if (!AStmt) |
| 2911 | return StmtError(); |
| 2912 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2913 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 2914 | // 1.2.2 OpenMP Language Terminology |
| 2915 | // Structured block - An executable statement with a single entry at the |
| 2916 | // top and a single exit at the bottom. |
| 2917 | // The point of exit cannot be a branch out of the structured block. |
| 2918 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 2919 | CS->getCapturedDecl()->setNothrow(); |
| 2920 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2921 | getCurFunction()->setHasBranchProtectedScope(); |
| 2922 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2923 | return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, |
| 2924 | DSAStack->isCancelRegion()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2927 | namespace { |
| 2928 | /// \brief Helper class for checking canonical form of the OpenMP loops and |
| 2929 | /// extracting iteration space of each loop in the loop nest, that will be used |
| 2930 | /// for IR generation. |
| 2931 | class OpenMPIterationSpaceChecker { |
| 2932 | /// \brief Reference to Sema. |
| 2933 | Sema &SemaRef; |
| 2934 | /// \brief A location for diagnostics (when there is no some better location). |
| 2935 | SourceLocation DefaultLoc; |
| 2936 | /// \brief A location for diagnostics (when increment is not compatible). |
| 2937 | SourceLocation ConditionLoc; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2938 | /// \brief A source location for referring to loop init later. |
| 2939 | SourceRange InitSrcRange; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2940 | /// \brief A source location for referring to condition later. |
| 2941 | SourceRange ConditionSrcRange; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2942 | /// \brief A source location for referring to increment later. |
| 2943 | SourceRange IncrementSrcRange; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2944 | /// \brief Loop variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2945 | ValueDecl *LCDecl = nullptr; |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2946 | /// \brief Reference to loop variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2947 | Expr *LCRef = nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2948 | /// \brief Lower bound (initializer for the var). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2949 | Expr *LB = nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2950 | /// \brief Upper bound. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2951 | Expr *UB = nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2952 | /// \brief Loop step (increment). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2953 | Expr *Step = nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2954 | /// \brief This flag is true when condition is one of: |
| 2955 | /// Var < UB |
| 2956 | /// Var <= UB |
| 2957 | /// UB > Var |
| 2958 | /// UB >= Var |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2959 | bool TestIsLessOp = false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2960 | /// \brief This flag is true when condition is strict ( < or > ). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2961 | bool TestIsStrictOp = false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2962 | /// \brief This flag is true when step is subtracted on each iteration. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2963 | bool SubtractStep = false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2964 | |
| 2965 | public: |
| 2966 | OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2967 | : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2968 | /// \brief Check init-expr for canonical loop form and save loop counter |
| 2969 | /// variable - #Var and its initialization value - #LB. |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2970 | bool CheckInit(Stmt *S, bool EmitDiags = true); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2971 | /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags |
| 2972 | /// for less/greater and for strict/non-strict comparison. |
| 2973 | bool CheckCond(Expr *S); |
| 2974 | /// \brief Check incr-expr for canonical loop form and return true if it |
| 2975 | /// does not conform, otherwise save loop step (#Step). |
| 2976 | bool CheckInc(Expr *S); |
| 2977 | /// \brief Return the loop counter variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2978 | ValueDecl *GetLoopDecl() const { return LCDecl; } |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2979 | /// \brief Return the reference expression to loop counter variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2980 | Expr *GetLoopDeclRefExpr() const { return LCRef; } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2981 | /// \brief Source range of the loop init. |
| 2982 | SourceRange GetInitSrcRange() const { return InitSrcRange; } |
| 2983 | /// \brief Source range of the loop condition. |
| 2984 | SourceRange GetConditionSrcRange() const { return ConditionSrcRange; } |
| 2985 | /// \brief Source range of the loop increment. |
| 2986 | SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; } |
| 2987 | /// \brief True if the step should be subtracted. |
| 2988 | bool ShouldSubtractStep() const { return SubtractStep; } |
| 2989 | /// \brief Build the expression to calculate the number of iterations. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2990 | Expr * |
| 2991 | BuildNumIterations(Scope *S, const bool LimitedType, |
| 2992 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2993 | /// \brief Build the precondition expression for the loops. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2994 | Expr *BuildPreCond(Scope *S, Expr *Cond, |
| 2995 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2996 | /// \brief Build reference expression to the counter be used for codegen. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2997 | DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures, |
| 2998 | DSAStackTy &DSA) const; |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 2999 | /// \brief Build reference expression to the private counter be used for |
| 3000 | /// codegen. |
| 3001 | Expr *BuildPrivateCounterVar() const; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3002 | /// \brief Build initialization of the counter be used for codegen. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3003 | Expr *BuildCounterInit() const; |
| 3004 | /// \brief Build step of the counter be used for codegen. |
| 3005 | Expr *BuildCounterStep() const; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3006 | /// \brief Return true if any expression is dependent. |
| 3007 | bool Dependent() const; |
| 3008 | |
| 3009 | private: |
| 3010 | /// \brief Check the right-hand side of an assignment in the increment |
| 3011 | /// expression. |
| 3012 | bool CheckIncRHS(Expr *RHS); |
| 3013 | /// \brief Helper to set loop counter variable and its initializer. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3014 | bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3015 | /// \brief Helper to set upper bound. |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 3016 | bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, |
Craig Topper | 9cd5e4f | 2015-09-21 01:23:32 +0000 | [diff] [blame] | 3017 | SourceLocation SL); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3018 | /// \brief Helper to set loop increment. |
| 3019 | bool SetStep(Expr *NewStep, bool Subtract); |
| 3020 | }; |
| 3021 | |
| 3022 | bool OpenMPIterationSpaceChecker::Dependent() const { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3023 | if (!LCDecl) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3024 | assert(!LB && !UB && !Step); |
| 3025 | return false; |
| 3026 | } |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3027 | return LCDecl->getType()->isDependentType() || |
| 3028 | (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || |
| 3029 | (Step && Step->isValueDependent()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3032 | static Expr *getExprAsWritten(Expr *E) { |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3033 | if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E)) |
| 3034 | E = ExprTemp->getSubExpr(); |
| 3035 | |
| 3036 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) |
| 3037 | E = MTE->GetTemporaryExpr(); |
| 3038 | |
| 3039 | while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 3040 | E = Binder->getSubExpr(); |
| 3041 | |
| 3042 | if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 3043 | E = ICE->getSubExprAsWritten(); |
| 3044 | return E->IgnoreParens(); |
| 3045 | } |
| 3046 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3047 | bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl, |
| 3048 | Expr *NewLCRefExpr, |
| 3049 | Expr *NewLB) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3050 | // State consistency checking to ensure correct usage. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3051 | assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 3052 | UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3053 | if (!NewLCDecl || !NewLB) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3054 | return true; |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3055 | LCDecl = getCanonicalDecl(NewLCDecl); |
| 3056 | LCRef = NewLCRefExpr; |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3057 | if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) |
| 3058 | if (const CXXConstructorDecl *Ctor = CE->getConstructor()) |
Alexey Bataev | 0d08a7f | 2015-07-16 04:19:43 +0000 | [diff] [blame] | 3059 | if ((Ctor->isCopyOrMoveConstructor() || |
| 3060 | Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && |
| 3061 | CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3062 | NewLB = CE->getArg(0)->IgnoreParenImpCasts(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3063 | LB = NewLB; |
| 3064 | return false; |
| 3065 | } |
| 3066 | |
| 3067 | bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 3068 | SourceRange SR, SourceLocation SL) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3069 | // State consistency checking to ensure correct usage. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3070 | assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && |
| 3071 | Step == nullptr && !TestIsLessOp && !TestIsStrictOp); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3072 | if (!NewUB) |
| 3073 | return true; |
| 3074 | UB = NewUB; |
| 3075 | TestIsLessOp = LessOp; |
| 3076 | TestIsStrictOp = StrictOp; |
| 3077 | ConditionSrcRange = SR; |
| 3078 | ConditionLoc = SL; |
| 3079 | return false; |
| 3080 | } |
| 3081 | |
| 3082 | bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { |
| 3083 | // State consistency checking to ensure correct usage. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3084 | assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3085 | if (!NewStep) |
| 3086 | return true; |
| 3087 | if (!NewStep->isValueDependent()) { |
| 3088 | // Check that the step is integer expression. |
| 3089 | SourceLocation StepLoc = NewStep->getLocStart(); |
| 3090 | ExprResult Val = |
| 3091 | SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); |
| 3092 | if (Val.isInvalid()) |
| 3093 | return true; |
| 3094 | NewStep = Val.get(); |
| 3095 | |
| 3096 | // OpenMP [2.6, Canonical Loop Form, Restrictions] |
| 3097 | // If test-expr is of form var relational-op b and relational-op is < or |
| 3098 | // <= then incr-expr must cause var to increase on each iteration of the |
| 3099 | // loop. If test-expr is of form var relational-op b and relational-op is |
| 3100 | // > or >= then incr-expr must cause var to decrease on each iteration of |
| 3101 | // the loop. |
| 3102 | // If test-expr is of form b relational-op var and relational-op is < or |
| 3103 | // <= then incr-expr must cause var to decrease on each iteration of the |
| 3104 | // loop. If test-expr is of form b relational-op var and relational-op is |
| 3105 | // > or >= then incr-expr must cause var to increase on each iteration of |
| 3106 | // the loop. |
| 3107 | llvm::APSInt Result; |
| 3108 | bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); |
| 3109 | bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); |
| 3110 | bool IsConstNeg = |
| 3111 | IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3112 | bool IsConstPos = |
| 3113 | IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3114 | bool IsConstZero = IsConstant && !Result.getBoolValue(); |
| 3115 | if (UB && (IsConstZero || |
| 3116 | (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3117 | : (IsConstPos || (IsUnsigned && !Subtract))))) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3118 | SemaRef.Diag(NewStep->getExprLoc(), |
| 3119 | diag::err_omp_loop_incr_not_compatible) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3120 | << LCDecl << TestIsLessOp << NewStep->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3121 | SemaRef.Diag(ConditionLoc, |
| 3122 | diag::note_omp_loop_cond_requres_compatible_incr) |
| 3123 | << TestIsLessOp << ConditionSrcRange; |
| 3124 | return true; |
| 3125 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3126 | if (TestIsLessOp == Subtract) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3127 | NewStep = |
| 3128 | SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) |
| 3129 | .get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3130 | Subtract = !Subtract; |
| 3131 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | Step = NewStep; |
| 3135 | SubtractStep = Subtract; |
| 3136 | return false; |
| 3137 | } |
| 3138 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3139 | bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3140 | // Check init-expr for canonical loop form and save loop counter |
| 3141 | // variable - #Var and its initialization value - #LB. |
| 3142 | // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: |
| 3143 | // var = lb |
| 3144 | // integer-type var = lb |
| 3145 | // random-access-iterator-type var = lb |
| 3146 | // pointer-type var = lb |
| 3147 | // |
| 3148 | if (!S) { |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3149 | if (EmitDiags) { |
| 3150 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); |
| 3151 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3152 | return true; |
| 3153 | } |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 3154 | if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) |
| 3155 | if (!ExprTemp->cleanupsHaveSideEffects()) |
| 3156 | S = ExprTemp->getSubExpr(); |
| 3157 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3158 | InitSrcRange = S->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3159 | if (Expr *E = dyn_cast<Expr>(S)) |
| 3160 | S = E->IgnoreParens(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3161 | if (auto *BO = dyn_cast<BinaryOperator>(S)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3162 | if (BO->getOpcode() == BO_Assign) { |
| 3163 | auto *LHS = BO->getLHS()->IgnoreParens(); |
| 3164 | if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { |
| 3165 | if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) |
| 3166 | if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) |
| 3167 | return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); |
| 3168 | return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); |
| 3169 | } |
| 3170 | if (auto *ME = dyn_cast<MemberExpr>(LHS)) { |
| 3171 | if (ME->isArrow() && |
| 3172 | isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) |
| 3173 | return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); |
| 3174 | } |
| 3175 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3176 | } else if (auto *DS = dyn_cast<DeclStmt>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3177 | if (DS->isSingleDecl()) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3178 | if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3179 | if (Var->hasInit() && !Var->getType()->isReferenceType()) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3180 | // Accept non-canonical init form here but emit ext. warning. |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3181 | if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3182 | SemaRef.Diag(S->getLocStart(), |
| 3183 | diag::ext_omp_loop_not_canonical_init) |
| 3184 | << S->getSourceRange(); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3185 | return SetLCDeclAndLB(Var, nullptr, Var->getInit()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3186 | } |
| 3187 | } |
| 3188 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3189 | } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3190 | if (CE->getOperator() == OO_Equal) { |
| 3191 | auto *LHS = CE->getArg(0); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3192 | if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3193 | if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) |
| 3194 | if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) |
| 3195 | return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); |
| 3196 | return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); |
| 3197 | } |
| 3198 | if (auto *ME = dyn_cast<MemberExpr>(LHS)) { |
| 3199 | if (ME->isArrow() && |
| 3200 | isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) |
| 3201 | return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); |
| 3202 | } |
| 3203 | } |
| 3204 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3205 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3206 | if (Dependent() || SemaRef.CurContext->isDependentContext()) |
| 3207 | return false; |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3208 | if (EmitDiags) { |
| 3209 | SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) |
| 3210 | << S->getSourceRange(); |
| 3211 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3212 | return true; |
| 3213 | } |
| 3214 | |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3215 | /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3216 | /// variable (which may be the loop variable) if possible. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3217 | static const ValueDecl *GetInitLCDecl(Expr *E) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3218 | if (!E) |
Craig Topper | 4b56692 | 2014-06-09 02:04:02 +0000 | [diff] [blame] | 3219 | return nullptr; |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3220 | E = getExprAsWritten(E); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3221 | if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) |
| 3222 | if (const CXXConstructorDecl *Ctor = CE->getConstructor()) |
Alexey Bataev | 0d08a7f | 2015-07-16 04:19:43 +0000 | [diff] [blame] | 3223 | if ((Ctor->isCopyOrMoveConstructor() || |
| 3224 | Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && |
| 3225 | CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3226 | E = CE->getArg(0)->IgnoreParenImpCasts(); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3227 | if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { |
| 3228 | if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 3229 | if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) |
| 3230 | if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) |
| 3231 | return getCanonicalDecl(ME->getMemberDecl()); |
| 3232 | return getCanonicalDecl(VD); |
| 3233 | } |
| 3234 | } |
| 3235 | if (auto *ME = dyn_cast_or_null<MemberExpr>(E)) |
| 3236 | if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) |
| 3237 | return getCanonicalDecl(ME->getMemberDecl()); |
| 3238 | return nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { |
| 3242 | // Check test-expr for canonical form, save upper-bound UB, flags for |
| 3243 | // less/greater and for strict/non-strict comparison. |
| 3244 | // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: |
| 3245 | // var relational-op b |
| 3246 | // b relational-op var |
| 3247 | // |
| 3248 | if (!S) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3249 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3250 | return true; |
| 3251 | } |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3252 | S = getExprAsWritten(S); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3253 | SourceLocation CondLoc = S->getLocStart(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3254 | if (auto *BO = dyn_cast<BinaryOperator>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3255 | if (BO->isRelationalOp()) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3256 | if (GetInitLCDecl(BO->getLHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3257 | return SetUB(BO->getRHS(), |
| 3258 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), |
| 3259 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), |
| 3260 | BO->getSourceRange(), BO->getOperatorLoc()); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3261 | if (GetInitLCDecl(BO->getRHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3262 | return SetUB(BO->getLHS(), |
| 3263 | (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), |
| 3264 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), |
| 3265 | BO->getSourceRange(), BO->getOperatorLoc()); |
| 3266 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3267 | } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3268 | if (CE->getNumArgs() == 2) { |
| 3269 | auto Op = CE->getOperator(); |
| 3270 | switch (Op) { |
| 3271 | case OO_Greater: |
| 3272 | case OO_GreaterEqual: |
| 3273 | case OO_Less: |
| 3274 | case OO_LessEqual: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3275 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3276 | return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, |
| 3277 | Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), |
| 3278 | CE->getOperatorLoc()); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3279 | if (GetInitLCDecl(CE->getArg(1)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3280 | return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, |
| 3281 | Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), |
| 3282 | CE->getOperatorLoc()); |
| 3283 | break; |
| 3284 | default: |
| 3285 | break; |
| 3286 | } |
| 3287 | } |
| 3288 | } |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3289 | if (Dependent() || SemaRef.CurContext->isDependentContext()) |
| 3290 | return false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3291 | SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3292 | << S->getSourceRange() << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3293 | return true; |
| 3294 | } |
| 3295 | |
| 3296 | bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { |
| 3297 | // RHS of canonical loop form increment can be: |
| 3298 | // var + incr |
| 3299 | // incr + var |
| 3300 | // var - incr |
| 3301 | // |
| 3302 | RHS = RHS->IgnoreParenImpCasts(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3303 | if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3304 | if (BO->isAdditiveOp()) { |
| 3305 | bool IsAdd = BO->getOpcode() == BO_Add; |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3306 | if (GetInitLCDecl(BO->getLHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3307 | return SetStep(BO->getRHS(), !IsAdd); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3308 | if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3309 | return SetStep(BO->getLHS(), false); |
| 3310 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3311 | } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3312 | bool IsAdd = CE->getOperator() == OO_Plus; |
| 3313 | if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3314 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3315 | return SetStep(CE->getArg(1), !IsAdd); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3316 | if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3317 | return SetStep(CE->getArg(0), false); |
| 3318 | } |
| 3319 | } |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3320 | if (Dependent() || SemaRef.CurContext->isDependentContext()) |
| 3321 | return false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3322 | SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3323 | << RHS->getSourceRange() << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3324 | return true; |
| 3325 | } |
| 3326 | |
| 3327 | bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { |
| 3328 | // Check incr-expr for canonical loop form and return true if it |
| 3329 | // does not conform. |
| 3330 | // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: |
| 3331 | // ++var |
| 3332 | // var++ |
| 3333 | // --var |
| 3334 | // var-- |
| 3335 | // var += incr |
| 3336 | // var -= incr |
| 3337 | // var = var + incr |
| 3338 | // var = incr + var |
| 3339 | // var = var - incr |
| 3340 | // |
| 3341 | if (!S) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3342 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3343 | return true; |
| 3344 | } |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 3345 | if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) |
| 3346 | if (!ExprTemp->cleanupsHaveSideEffects()) |
| 3347 | S = ExprTemp->getSubExpr(); |
| 3348 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3349 | IncrementSrcRange = S->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3350 | S = S->IgnoreParens(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3351 | if (auto *UO = dyn_cast<UnaryOperator>(S)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3352 | if (UO->isIncrementDecrementOp() && |
| 3353 | GetInitLCDecl(UO->getSubExpr()) == LCDecl) |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3354 | return SetStep(SemaRef |
| 3355 | .ActOnIntegerConstant(UO->getLocStart(), |
| 3356 | (UO->isDecrementOp() ? -1 : 1)) |
| 3357 | .get(), |
| 3358 | false); |
| 3359 | } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3360 | switch (BO->getOpcode()) { |
| 3361 | case BO_AddAssign: |
| 3362 | case BO_SubAssign: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3363 | if (GetInitLCDecl(BO->getLHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3364 | return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); |
| 3365 | break; |
| 3366 | case BO_Assign: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3367 | if (GetInitLCDecl(BO->getLHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3368 | return CheckIncRHS(BO->getRHS()); |
| 3369 | break; |
| 3370 | default: |
| 3371 | break; |
| 3372 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3373 | } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3374 | switch (CE->getOperator()) { |
| 3375 | case OO_PlusPlus: |
| 3376 | case OO_MinusMinus: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3377 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3378 | return SetStep(SemaRef |
| 3379 | .ActOnIntegerConstant( |
| 3380 | CE->getLocStart(), |
| 3381 | ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) |
| 3382 | .get(), |
| 3383 | false); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3384 | break; |
| 3385 | case OO_PlusEqual: |
| 3386 | case OO_MinusEqual: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3387 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3388 | return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); |
| 3389 | break; |
| 3390 | case OO_Equal: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3391 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3392 | return CheckIncRHS(CE->getArg(1)); |
| 3393 | break; |
| 3394 | default: |
| 3395 | break; |
| 3396 | } |
| 3397 | } |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3398 | if (Dependent() || SemaRef.CurContext->isDependentContext()) |
| 3399 | return false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3400 | SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3401 | << S->getSourceRange() << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3402 | return true; |
| 3403 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3404 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3405 | static ExprResult |
| 3406 | tryBuildCapture(Sema &SemaRef, Expr *Capture, |
| 3407 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 3408 | if (SemaRef.CurContext->isDependentContext()) |
| 3409 | return ExprResult(Capture); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3410 | if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) |
| 3411 | return SemaRef.PerformImplicitConversion( |
| 3412 | Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, |
| 3413 | /*AllowExplicit=*/true); |
| 3414 | auto I = Captures.find(Capture); |
| 3415 | if (I != Captures.end()) |
| 3416 | return buildCapture(SemaRef, Capture, I->second); |
| 3417 | DeclRefExpr *Ref = nullptr; |
| 3418 | ExprResult Res = buildCapture(SemaRef, Capture, Ref); |
| 3419 | Captures[Capture] = Ref; |
| 3420 | return Res; |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3423 | /// \brief Build the expression to calculate the number of iterations. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3424 | Expr *OpenMPIterationSpaceChecker::BuildNumIterations( |
| 3425 | Scope *S, const bool LimitedType, |
| 3426 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3427 | ExprResult Diff; |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3428 | auto VarType = LCDecl->getType().getNonReferenceType(); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3429 | if (VarType->isIntegerType() || VarType->isPointerType() || |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3430 | SemaRef.getLangOpts().CPlusPlus) { |
| 3431 | // Upper - Lower |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3432 | auto *UBExpr = TestIsLessOp ? UB : LB; |
| 3433 | auto *LBExpr = TestIsLessOp ? LB : UB; |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3434 | Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); |
| 3435 | Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3436 | if (!Upper || !Lower) |
| 3437 | return nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3438 | |
| 3439 | Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); |
| 3440 | |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3441 | if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3442 | // BuildBinOp already emitted error, this one is to point user to upper |
| 3443 | // and lower bound, and to tell what is passed to 'operator-'. |
| 3444 | SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) |
| 3445 | << Upper->getSourceRange() << Lower->getSourceRange(); |
| 3446 | return nullptr; |
| 3447 | } |
| 3448 | } |
| 3449 | |
| 3450 | if (!Diff.isUsable()) |
| 3451 | return nullptr; |
| 3452 | |
| 3453 | // Upper - Lower [- 1] |
| 3454 | if (TestIsStrictOp) |
| 3455 | Diff = SemaRef.BuildBinOp( |
| 3456 | S, DefaultLoc, BO_Sub, Diff.get(), |
| 3457 | SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); |
| 3458 | if (!Diff.isUsable()) |
| 3459 | return nullptr; |
| 3460 | |
| 3461 | // Upper - Lower [- 1] + Step |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3462 | auto NewStep = tryBuildCapture(SemaRef, Step, Captures); |
| 3463 | if (!NewStep.isUsable()) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3464 | return nullptr; |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3465 | Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3466 | if (!Diff.isUsable()) |
| 3467 | return nullptr; |
| 3468 | |
| 3469 | // Parentheses (for dumping/debugging purposes only). |
| 3470 | Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); |
| 3471 | if (!Diff.isUsable()) |
| 3472 | return nullptr; |
| 3473 | |
| 3474 | // (Upper - Lower [- 1] + Step) / Step |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3475 | Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3476 | if (!Diff.isUsable()) |
| 3477 | return nullptr; |
| 3478 | |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3479 | // OpenMP runtime requires 32-bit or 64-bit loop variables. |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3480 | QualType Type = Diff.get()->getType(); |
| 3481 | auto &C = SemaRef.Context; |
| 3482 | bool UseVarType = VarType->hasIntegerRepresentation() && |
| 3483 | C.getTypeSize(Type) > C.getTypeSize(VarType); |
| 3484 | if (!Type->isIntegerType() || UseVarType) { |
| 3485 | unsigned NewSize = |
| 3486 | UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); |
| 3487 | bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() |
| 3488 | : Type->hasSignedIntegerRepresentation(); |
| 3489 | Type = C.getIntTypeForBitwidth(NewSize, IsSigned); |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3490 | if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { |
| 3491 | Diff = SemaRef.PerformImplicitConversion( |
| 3492 | Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); |
| 3493 | if (!Diff.isUsable()) |
| 3494 | return nullptr; |
| 3495 | } |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3496 | } |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3497 | if (LimitedType) { |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3498 | unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; |
| 3499 | if (NewSize != C.getTypeSize(Type)) { |
| 3500 | if (NewSize < C.getTypeSize(Type)) { |
| 3501 | assert(NewSize == 64 && "incorrect loop var size"); |
| 3502 | SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) |
| 3503 | << InitSrcRange << ConditionSrcRange; |
| 3504 | } |
| 3505 | QualType NewType = C.getIntTypeForBitwidth( |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3506 | NewSize, Type->hasSignedIntegerRepresentation() || |
| 3507 | C.getTypeSize(Type) < NewSize); |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3508 | if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { |
| 3509 | Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, |
| 3510 | Sema::AA_Converting, true); |
| 3511 | if (!Diff.isUsable()) |
| 3512 | return nullptr; |
| 3513 | } |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3514 | } |
| 3515 | } |
| 3516 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3517 | return Diff.get(); |
| 3518 | } |
| 3519 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3520 | Expr *OpenMPIterationSpaceChecker::BuildPreCond( |
| 3521 | Scope *S, Expr *Cond, |
| 3522 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3523 | // Try to build LB <op> UB, where <op> is <, >, <=, or >=. |
| 3524 | bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); |
| 3525 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3526 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3527 | auto NewLB = tryBuildCapture(SemaRef, LB, Captures); |
| 3528 | auto NewUB = tryBuildCapture(SemaRef, UB, Captures); |
| 3529 | if (!NewLB.isUsable() || !NewUB.isUsable()) |
| 3530 | return nullptr; |
| 3531 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3532 | auto CondExpr = SemaRef.BuildBinOp( |
| 3533 | S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) |
| 3534 | : (TestIsStrictOp ? BO_GT : BO_GE), |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3535 | NewLB.get(), NewUB.get()); |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3536 | if (CondExpr.isUsable()) { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3537 | if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), |
| 3538 | SemaRef.Context.BoolTy)) |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3539 | CondExpr = SemaRef.PerformImplicitConversion( |
| 3540 | CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, |
| 3541 | /*AllowExplicit=*/true); |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3542 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3543 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); |
| 3544 | // Otherwise use original loop conditon and evaluate it in runtime. |
| 3545 | return CondExpr.isUsable() ? CondExpr.get() : Cond; |
| 3546 | } |
| 3547 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3548 | /// \brief Build reference expression to the counter be used for codegen. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3549 | DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3550 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3551 | auto *VD = dyn_cast<VarDecl>(LCDecl); |
| 3552 | if (!VD) { |
| 3553 | VD = SemaRef.IsOpenMPCapturedDecl(LCDecl); |
| 3554 | auto *Ref = buildDeclRefExpr( |
| 3555 | SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3556 | DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false); |
| 3557 | // If the loop control decl is explicitly marked as private, do not mark it |
| 3558 | // as captured again. |
| 3559 | if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) |
| 3560 | Captures.insert(std::make_pair(LCRef, Ref)); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3561 | return Ref; |
| 3562 | } |
| 3563 | return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3564 | DefaultLoc); |
| 3565 | } |
| 3566 | |
| 3567 | Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3568 | if (LCDecl && !LCDecl->isInvalidDecl()) { |
| 3569 | auto Type = LCDecl->getType().getNonReferenceType(); |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 3570 | auto *PrivateVar = |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3571 | buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(), |
| 3572 | LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3573 | if (PrivateVar->isInvalidDecl()) |
| 3574 | return nullptr; |
| 3575 | return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); |
| 3576 | } |
| 3577 | return nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 3580 | /// \brief Build initialization of the counter to be used for codegen. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3581 | Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; } |
| 3582 | |
| 3583 | /// \brief Build step of the counter be used for codegen. |
| 3584 | Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; } |
| 3585 | |
| 3586 | /// \brief Iteration space of a single for loop. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3587 | struct LoopIterationSpace final { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3588 | /// \brief Condition of the loop. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3589 | Expr *PreCond = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3590 | /// \brief This expression calculates the number of iterations in the loop. |
| 3591 | /// It is always possible to calculate it before starting the loop. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3592 | Expr *NumIterations = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3593 | /// \brief The loop counter variable. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3594 | Expr *CounterVar = nullptr; |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3595 | /// \brief Private loop counter variable. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3596 | Expr *PrivateCounterVar = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3597 | /// \brief This is initializer for the initial value of #CounterVar. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3598 | Expr *CounterInit = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3599 | /// \brief This is step for the #CounterVar used to generate its update: |
| 3600 | /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3601 | Expr *CounterStep = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3602 | /// \brief Should step be subtracted? |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3603 | bool Subtract = false; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3604 | /// \brief Source range of the loop init. |
| 3605 | SourceRange InitSrcRange; |
| 3606 | /// \brief Source range of the loop condition. |
| 3607 | SourceRange CondSrcRange; |
| 3608 | /// \brief Source range of the loop increment. |
| 3609 | SourceRange IncSrcRange; |
| 3610 | }; |
| 3611 | |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3612 | } // namespace |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3613 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3614 | void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { |
| 3615 | assert(getLangOpts().OpenMP && "OpenMP is not active."); |
| 3616 | assert(Init && "Expected loop in canonical form."); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 3617 | unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); |
| 3618 | if (AssociatedLoops > 0 && |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3619 | isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { |
| 3620 | OpenMPIterationSpaceChecker ISC(*this, ForLoc); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3621 | if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) { |
| 3622 | if (auto *D = ISC.GetLoopDecl()) { |
| 3623 | auto *VD = dyn_cast<VarDecl>(D); |
| 3624 | if (!VD) { |
| 3625 | if (auto *Private = IsOpenMPCapturedDecl(D)) |
| 3626 | VD = Private; |
| 3627 | else { |
| 3628 | auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(), |
| 3629 | /*WithInit=*/false); |
| 3630 | VD = cast<VarDecl>(Ref->getDecl()); |
| 3631 | } |
| 3632 | } |
| 3633 | DSAStack->addLoopControlVariable(D, VD); |
| 3634 | } |
| 3635 | } |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 3636 | DSAStack->setAssociatedLoops(AssociatedLoops - 1); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3640 | /// \brief Called on a for stmt to check and extract its iteration space |
| 3641 | /// for further processing (such as collapsing). |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3642 | static bool CheckOpenMPIterationSpace( |
| 3643 | OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, |
| 3644 | unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3645 | Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 3646 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3647 | LoopIterationSpace &ResultIterSpace, |
| 3648 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3649 | // OpenMP [2.6, Canonical Loop Form] |
| 3650 | // for (init-expr; test-expr; incr-expr) structured-block |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3651 | auto *For = dyn_cast_or_null<ForStmt>(S); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3652 | if (!For) { |
| 3653 | SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3654 | << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) |
| 3655 | << getOpenMPDirectiveName(DKind) << NestedLoopCount |
| 3656 | << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; |
| 3657 | if (NestedLoopCount > 1) { |
| 3658 | if (CollapseLoopCountExpr && OrderedLoopCountExpr) |
| 3659 | SemaRef.Diag(DSA.getConstructLoc(), |
| 3660 | diag::note_omp_collapse_ordered_expr) |
| 3661 | << 2 << CollapseLoopCountExpr->getSourceRange() |
| 3662 | << OrderedLoopCountExpr->getSourceRange(); |
| 3663 | else if (CollapseLoopCountExpr) |
| 3664 | SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), |
| 3665 | diag::note_omp_collapse_ordered_expr) |
| 3666 | << 0 << CollapseLoopCountExpr->getSourceRange(); |
| 3667 | else |
| 3668 | SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), |
| 3669 | diag::note_omp_collapse_ordered_expr) |
| 3670 | << 1 << OrderedLoopCountExpr->getSourceRange(); |
| 3671 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3672 | return true; |
| 3673 | } |
| 3674 | assert(For->getBody()); |
| 3675 | |
| 3676 | OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); |
| 3677 | |
| 3678 | // Check init. |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 3679 | auto Init = For->getInit(); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3680 | if (ISC.CheckInit(Init)) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3681 | return true; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3682 | |
| 3683 | bool HasErrors = false; |
| 3684 | |
| 3685 | // Check loop variable's type. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3686 | if (auto *LCDecl = ISC.GetLoopDecl()) { |
| 3687 | auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3688 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3689 | // OpenMP [2.6, Canonical Loop Form] |
| 3690 | // Var is one of the following: |
| 3691 | // A variable of signed or unsigned integer type. |
| 3692 | // For C++, a variable of a random access iterator type. |
| 3693 | // For C, a variable of a pointer type. |
| 3694 | auto VarType = LCDecl->getType().getNonReferenceType(); |
| 3695 | if (!VarType->isDependentType() && !VarType->isIntegerType() && |
| 3696 | !VarType->isPointerType() && |
| 3697 | !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { |
| 3698 | SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) |
| 3699 | << SemaRef.getLangOpts().CPlusPlus; |
| 3700 | HasErrors = true; |
| 3701 | } |
| 3702 | |
| 3703 | // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in |
| 3704 | // a Construct |
| 3705 | // The loop iteration variable(s) in the associated for-loop(s) of a for or |
| 3706 | // parallel for construct is (are) private. |
| 3707 | // The loop iteration variable in the associated for-loop of a simd |
| 3708 | // construct with just one associated for-loop is linear with a |
| 3709 | // constant-linear-step that is the increment of the associated for-loop. |
| 3710 | // Exclude loop var from the list of variables with implicitly defined data |
| 3711 | // sharing attributes. |
| 3712 | VarsWithImplicitDSA.erase(LCDecl); |
| 3713 | |
| 3714 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 3715 | // in a Construct, C/C++]. |
| 3716 | // The loop iteration variable in the associated for-loop of a simd |
| 3717 | // construct with just one associated for-loop may be listed in a linear |
| 3718 | // clause with a constant-linear-step that is the increment of the |
| 3719 | // associated for-loop. |
| 3720 | // The loop iteration variable(s) in the associated for-loop(s) of a for or |
| 3721 | // parallel for construct may be listed in a private or lastprivate clause. |
| 3722 | DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); |
| 3723 | // If LoopVarRefExpr is nullptr it means the corresponding loop variable is |
| 3724 | // declared in the loop and it is predetermined as a private. |
| 3725 | auto PredeterminedCKind = |
| 3726 | isOpenMPSimdDirective(DKind) |
| 3727 | ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) |
| 3728 | : OMPC_private; |
| 3729 | if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && |
| 3730 | DVar.CKind != PredeterminedCKind) || |
| 3731 | ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || |
| 3732 | isOpenMPDistributeDirective(DKind)) && |
| 3733 | !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && |
| 3734 | DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && |
| 3735 | (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { |
| 3736 | SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) |
| 3737 | << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) |
| 3738 | << getOpenMPClauseName(PredeterminedCKind); |
| 3739 | if (DVar.RefExpr == nullptr) |
| 3740 | DVar.CKind = PredeterminedCKind; |
| 3741 | ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); |
| 3742 | HasErrors = true; |
| 3743 | } else if (LoopDeclRefExpr != nullptr) { |
| 3744 | // Make the loop iteration variable private (for worksharing constructs), |
| 3745 | // linear (for simd directives with the only one associated loop) or |
| 3746 | // lastprivate (for simd directives with several collapsed or ordered |
| 3747 | // loops). |
| 3748 | if (DVar.CKind == OMPC_unknown) |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 3749 | DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, |
| 3750 | [](OpenMPDirectiveKind) -> bool { return true; }, |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3751 | /*FromParent=*/false); |
| 3752 | DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); |
| 3753 | } |
| 3754 | |
| 3755 | assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); |
| 3756 | |
| 3757 | // Check test-expr. |
| 3758 | HasErrors |= ISC.CheckCond(For->getCond()); |
| 3759 | |
| 3760 | // Check incr-expr. |
| 3761 | HasErrors |= ISC.CheckInc(For->getInc()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3764 | if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3765 | return HasErrors; |
| 3766 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3767 | // Build the loop's iteration space representation. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3768 | ResultIterSpace.PreCond = |
| 3769 | ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures); |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3770 | ResultIterSpace.NumIterations = ISC.BuildNumIterations( |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3771 | DSA.getCurScope(), |
| 3772 | (isOpenMPWorksharingDirective(DKind) || |
| 3773 | isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), |
| 3774 | Captures); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3775 | ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3776 | ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3777 | ResultIterSpace.CounterInit = ISC.BuildCounterInit(); |
| 3778 | ResultIterSpace.CounterStep = ISC.BuildCounterStep(); |
| 3779 | ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange(); |
| 3780 | ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange(); |
| 3781 | ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange(); |
| 3782 | ResultIterSpace.Subtract = ISC.ShouldSubtractStep(); |
| 3783 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3784 | HasErrors |= (ResultIterSpace.PreCond == nullptr || |
| 3785 | ResultIterSpace.NumIterations == nullptr || |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3786 | ResultIterSpace.CounterVar == nullptr || |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3787 | ResultIterSpace.PrivateCounterVar == nullptr || |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3788 | ResultIterSpace.CounterInit == nullptr || |
| 3789 | ResultIterSpace.CounterStep == nullptr); |
| 3790 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3791 | return HasErrors; |
| 3792 | } |
| 3793 | |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3794 | /// \brief Build 'VarRef = Start. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3795 | static ExprResult |
| 3796 | BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, |
| 3797 | ExprResult Start, |
| 3798 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3799 | // Build 'VarRef = Start. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3800 | auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); |
| 3801 | if (!NewStart.isUsable()) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3802 | return ExprError(); |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3803 | if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3804 | VarRef.get()->getType())) { |
| 3805 | NewStart = SemaRef.PerformImplicitConversion( |
| 3806 | NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, |
| 3807 | /*AllowExplicit=*/true); |
| 3808 | if (!NewStart.isUsable()) |
| 3809 | return ExprError(); |
| 3810 | } |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3811 | |
| 3812 | auto Init = |
| 3813 | SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); |
| 3814 | return Init; |
| 3815 | } |
| 3816 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3817 | /// \brief Build 'VarRef = Start + Iter * Step'. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3818 | static ExprResult |
| 3819 | BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc, |
| 3820 | ExprResult VarRef, ExprResult Start, ExprResult Iter, |
| 3821 | ExprResult Step, bool Subtract, |
| 3822 | llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3823 | // Add parentheses (for debugging purposes only). |
| 3824 | Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); |
| 3825 | if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || |
| 3826 | !Step.isUsable()) |
| 3827 | return ExprError(); |
| 3828 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3829 | ExprResult NewStep = Step; |
| 3830 | if (Captures) |
| 3831 | NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3832 | if (NewStep.isInvalid()) |
| 3833 | return ExprError(); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3834 | ExprResult Update = |
| 3835 | SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3836 | if (!Update.isUsable()) |
| 3837 | return ExprError(); |
| 3838 | |
Alexey Bataev | c0214e0 | 2016-02-16 12:13:49 +0000 | [diff] [blame] | 3839 | // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or |
| 3840 | // 'VarRef = Start (+|-) Iter * Step'. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3841 | ExprResult NewStart = Start; |
| 3842 | if (Captures) |
| 3843 | NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3844 | if (NewStart.isInvalid()) |
| 3845 | return ExprError(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3846 | |
Alexey Bataev | c0214e0 | 2016-02-16 12:13:49 +0000 | [diff] [blame] | 3847 | // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. |
| 3848 | ExprResult SavedUpdate = Update; |
| 3849 | ExprResult UpdateVal; |
| 3850 | if (VarRef.get()->getType()->isOverloadableType() || |
| 3851 | NewStart.get()->getType()->isOverloadableType() || |
| 3852 | Update.get()->getType()->isOverloadableType()) { |
| 3853 | bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); |
| 3854 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); |
| 3855 | Update = |
| 3856 | SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); |
| 3857 | if (Update.isUsable()) { |
| 3858 | UpdateVal = |
| 3859 | SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, |
| 3860 | VarRef.get(), SavedUpdate.get()); |
| 3861 | if (UpdateVal.isUsable()) { |
| 3862 | Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), |
| 3863 | UpdateVal.get()); |
| 3864 | } |
| 3865 | } |
| 3866 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); |
| 3867 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3868 | |
Alexey Bataev | c0214e0 | 2016-02-16 12:13:49 +0000 | [diff] [blame] | 3869 | // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. |
| 3870 | if (!Update.isUsable() || !UpdateVal.isUsable()) { |
| 3871 | Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, |
| 3872 | NewStart.get(), SavedUpdate.get()); |
| 3873 | if (!Update.isUsable()) |
| 3874 | return ExprError(); |
| 3875 | |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3876 | if (!SemaRef.Context.hasSameType(Update.get()->getType(), |
| 3877 | VarRef.get()->getType())) { |
| 3878 | Update = SemaRef.PerformImplicitConversion( |
| 3879 | Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); |
| 3880 | if (!Update.isUsable()) |
| 3881 | return ExprError(); |
| 3882 | } |
Alexey Bataev | c0214e0 | 2016-02-16 12:13:49 +0000 | [diff] [blame] | 3883 | |
| 3884 | Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); |
| 3885 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3886 | return Update; |
| 3887 | } |
| 3888 | |
| 3889 | /// \brief Convert integer expression \a E to make it have at least \a Bits |
| 3890 | /// bits. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3891 | static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3892 | if (E == nullptr) |
| 3893 | return ExprError(); |
| 3894 | auto &C = SemaRef.Context; |
| 3895 | QualType OldType = E->getType(); |
| 3896 | unsigned HasBits = C.getTypeSize(OldType); |
| 3897 | if (HasBits >= Bits) |
| 3898 | return ExprResult(E); |
| 3899 | // OK to convert to signed, because new type has more bits than old. |
| 3900 | QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); |
| 3901 | return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, |
| 3902 | true); |
| 3903 | } |
| 3904 | |
| 3905 | /// \brief Check if the given expression \a E is a constant integer that fits |
| 3906 | /// into \a Bits bits. |
| 3907 | static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) { |
| 3908 | if (E == nullptr) |
| 3909 | return false; |
| 3910 | llvm::APSInt Result; |
| 3911 | if (E->isIntegerConstantExpr(Result, SemaRef.Context)) |
| 3912 | return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); |
| 3913 | return false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3916 | /// Build preinits statement for the given declarations. |
| 3917 | static Stmt *buildPreInits(ASTContext &Context, |
| 3918 | SmallVectorImpl<Decl *> &PreInits) { |
| 3919 | if (!PreInits.empty()) { |
| 3920 | return new (Context) DeclStmt( |
| 3921 | DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), |
| 3922 | SourceLocation(), SourceLocation()); |
| 3923 | } |
| 3924 | return nullptr; |
| 3925 | } |
| 3926 | |
| 3927 | /// Build preinits statement for the given declarations. |
| 3928 | static Stmt *buildPreInits(ASTContext &Context, |
| 3929 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { |
| 3930 | if (!Captures.empty()) { |
| 3931 | SmallVector<Decl *, 16> PreInits; |
| 3932 | for (auto &Pair : Captures) |
| 3933 | PreInits.push_back(Pair.second->getDecl()); |
| 3934 | return buildPreInits(Context, PreInits); |
| 3935 | } |
| 3936 | return nullptr; |
| 3937 | } |
| 3938 | |
| 3939 | /// Build postupdate expression for the given list of postupdates expressions. |
| 3940 | static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { |
| 3941 | Expr *PostUpdate = nullptr; |
| 3942 | if (!PostUpdates.empty()) { |
| 3943 | for (auto *E : PostUpdates) { |
| 3944 | Expr *ConvE = S.BuildCStyleCastExpr( |
| 3945 | E->getExprLoc(), |
| 3946 | S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), |
| 3947 | E->getExprLoc(), E) |
| 3948 | .get(); |
| 3949 | PostUpdate = PostUpdate |
| 3950 | ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, |
| 3951 | PostUpdate, ConvE) |
| 3952 | .get() |
| 3953 | : ConvE; |
| 3954 | } |
| 3955 | } |
| 3956 | return PostUpdate; |
| 3957 | } |
| 3958 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3959 | /// \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] | 3960 | /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, |
| 3961 | /// number of collapsed loops otherwise. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3962 | static unsigned |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3963 | CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, |
| 3964 | Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, |
| 3965 | DSAStackTy &DSA, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 3966 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3967 | OMPLoopDirective::HelperExprs &Built) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3968 | unsigned NestedLoopCount = 1; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3969 | if (CollapseLoopCountExpr) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3970 | // Found 'collapse' clause - calculate collapse number. |
| 3971 | llvm::APSInt Result; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3972 | if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) |
Alexey Bataev | 7b6bc88 | 2015-11-26 07:50:39 +0000 | [diff] [blame] | 3973 | NestedLoopCount = Result.getLimitedValue(); |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3974 | } |
| 3975 | if (OrderedLoopCountExpr) { |
| 3976 | // Found 'ordered' clause - calculate collapse number. |
| 3977 | llvm::APSInt Result; |
Alexey Bataev | 7b6bc88 | 2015-11-26 07:50:39 +0000 | [diff] [blame] | 3978 | if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { |
| 3979 | if (Result.getLimitedValue() < NestedLoopCount) { |
| 3980 | SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), |
| 3981 | diag::err_omp_wrong_ordered_loop_count) |
| 3982 | << OrderedLoopCountExpr->getSourceRange(); |
| 3983 | SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), |
| 3984 | diag::note_collapse_loop_count) |
| 3985 | << CollapseLoopCountExpr->getSourceRange(); |
| 3986 | } |
| 3987 | NestedLoopCount = Result.getLimitedValue(); |
| 3988 | } |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3989 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3990 | // This is helper routine for loop directives (e.g., 'for', 'simd', |
| 3991 | // 'for simd', etc.). |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3992 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3993 | SmallVector<LoopIterationSpace, 4> IterSpaces; |
| 3994 | IterSpaces.resize(NestedLoopCount); |
| 3995 | Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3996 | for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3997 | if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3998 | NestedLoopCount, CollapseLoopCountExpr, |
| 3999 | OrderedLoopCountExpr, VarsWithImplicitDSA, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4000 | IterSpaces[Cnt], Captures)) |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 4001 | return 0; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 4002 | // 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] | 4003 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 4004 | // All loops associated with the construct must be perfectly nested; that |
| 4005 | // is, there must be no intervening code nor any OpenMP directive between |
| 4006 | // any two loops. |
| 4007 | CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4010 | Built.clear(/* size */ NestedLoopCount); |
| 4011 | |
| 4012 | if (SemaRef.CurContext->isDependentContext()) |
| 4013 | return NestedLoopCount; |
| 4014 | |
| 4015 | // An example of what is generated for the following code: |
| 4016 | // |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4017 | // #pragma omp simd collapse(2) ordered(2) |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4018 | // for (i = 0; i < NI; ++i) |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4019 | // for (k = 0; k < NK; ++k) |
| 4020 | // for (j = J0; j < NJ; j+=2) { |
| 4021 | // <loop body> |
| 4022 | // } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4023 | // |
| 4024 | // We generate the code below. |
| 4025 | // Note: the loop body may be outlined in CodeGen. |
| 4026 | // Note: some counters may be C++ classes, operator- is used to find number of |
| 4027 | // iterations and operator+= to calculate counter value. |
| 4028 | // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 |
| 4029 | // or i64 is currently supported). |
| 4030 | // |
| 4031 | // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) |
| 4032 | // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { |
| 4033 | // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); |
| 4034 | // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; |
| 4035 | // // similar updates for vars in clauses (e.g. 'linear') |
| 4036 | // <loop body (using local i and j)> |
| 4037 | // } |
| 4038 | // i = NI; // assign final values of counters |
| 4039 | // j = NJ; |
| 4040 | // |
| 4041 | |
| 4042 | // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are |
| 4043 | // the iteration counts of the collapsed for loops. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 4044 | // Precondition tests if there is at least one iteration (all conditions are |
| 4045 | // true). |
| 4046 | auto PreCond = ExprResult(IterSpaces[0].PreCond); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4047 | auto N0 = IterSpaces[0].NumIterations; |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4048 | ExprResult LastIteration32 = WidenIterationCount( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4049 | 32 /* Bits */, SemaRef |
| 4050 | .PerformImplicitConversion( |
| 4051 | N0->IgnoreImpCasts(), N0->getType(), |
| 4052 | Sema::AA_Converting, /*AllowExplicit=*/true) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4053 | .get(), |
| 4054 | SemaRef); |
| 4055 | ExprResult LastIteration64 = WidenIterationCount( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4056 | 64 /* Bits */, SemaRef |
| 4057 | .PerformImplicitConversion( |
| 4058 | N0->IgnoreImpCasts(), N0->getType(), |
| 4059 | Sema::AA_Converting, /*AllowExplicit=*/true) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4060 | .get(), |
| 4061 | SemaRef); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4062 | |
| 4063 | if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) |
| 4064 | return NestedLoopCount; |
| 4065 | |
| 4066 | auto &C = SemaRef.Context; |
| 4067 | bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; |
| 4068 | |
| 4069 | Scope *CurScope = DSA.getCurScope(); |
| 4070 | for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 4071 | if (PreCond.isUsable()) { |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 4072 | PreCond = |
| 4073 | SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, |
| 4074 | PreCond.get(), IterSpaces[Cnt].PreCond); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 4075 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4076 | auto N = IterSpaces[Cnt].NumIterations; |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 4077 | SourceLocation Loc = N->getExprLoc(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4078 | AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; |
| 4079 | if (LastIteration32.isUsable()) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4080 | LastIteration32 = SemaRef.BuildBinOp( |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 4081 | CurScope, Loc, BO_Mul, LastIteration32.get(), |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4082 | SemaRef |
| 4083 | .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), |
| 4084 | Sema::AA_Converting, |
| 4085 | /*AllowExplicit=*/true) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4086 | .get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4087 | if (LastIteration64.isUsable()) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4088 | LastIteration64 = SemaRef.BuildBinOp( |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 4089 | CurScope, Loc, BO_Mul, LastIteration64.get(), |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4090 | SemaRef |
| 4091 | .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), |
| 4092 | Sema::AA_Converting, |
| 4093 | /*AllowExplicit=*/true) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4094 | .get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | // Choose either the 32-bit or 64-bit version. |
| 4098 | ExprResult LastIteration = LastIteration64; |
| 4099 | if (LastIteration32.isUsable() && |
| 4100 | C.getTypeSize(LastIteration32.get()->getType()) == 32 && |
| 4101 | (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || |
| 4102 | FitsInto( |
| 4103 | 32 /* Bits */, |
| 4104 | LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), |
| 4105 | LastIteration64.get(), SemaRef))) |
| 4106 | LastIteration = LastIteration32; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4107 | QualType VType = LastIteration.get()->getType(); |
| 4108 | QualType RealVType = VType; |
| 4109 | QualType StrideVType = VType; |
| 4110 | if (isOpenMPTaskLoopDirective(DKind)) { |
| 4111 | VType = |
| 4112 | SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 4113 | StrideVType = |
| 4114 | SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 4115 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4116 | |
| 4117 | if (!LastIteration.isUsable()) |
| 4118 | return 0; |
| 4119 | |
| 4120 | // Save the number of iterations. |
| 4121 | ExprResult NumIterations = LastIteration; |
| 4122 | { |
| 4123 | LastIteration = SemaRef.BuildBinOp( |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 4124 | CurScope, LastIteration.get()->getExprLoc(), BO_Sub, |
| 4125 | LastIteration.get(), |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4126 | SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); |
| 4127 | if (!LastIteration.isUsable()) |
| 4128 | return 0; |
| 4129 | } |
| 4130 | |
| 4131 | // Calculate the last iteration number beforehand instead of doing this on |
| 4132 | // each iteration. Do not do this if the number of iterations may be kfold-ed. |
| 4133 | llvm::APSInt Result; |
| 4134 | bool IsConstant = |
| 4135 | LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); |
| 4136 | ExprResult CalcLastIteration; |
| 4137 | if (!IsConstant) { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4138 | ExprResult SaveRef = |
| 4139 | tryBuildCapture(SemaRef, LastIteration.get(), Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4140 | LastIteration = SaveRef; |
| 4141 | |
| 4142 | // Prepare SaveRef + 1. |
| 4143 | NumIterations = SemaRef.BuildBinOp( |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 4144 | CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4145 | SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); |
| 4146 | if (!NumIterations.isUsable()) |
| 4147 | return 0; |
| 4148 | } |
| 4149 | |
| 4150 | SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); |
| 4151 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4152 | // Build variables passed into runtime, necessary for worksharing directives. |
Carlo Bertolli | ffafe10 | 2017-04-20 00:39:39 +0000 | [diff] [blame] | 4153 | ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 4154 | if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || |
| 4155 | isOpenMPDistributeDirective(DKind)) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4156 | // Lower bound variable, initialized with zero. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 4157 | VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); |
| 4158 | LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4159 | SemaRef.AddInitializerToDecl(LBDecl, |
| 4160 | SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), |
| 4161 | /*DirectInit*/ false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4162 | |
| 4163 | // Upper bound variable, initialized with last iteration number. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 4164 | VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); |
| 4165 | UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4166 | SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4167 | /*DirectInit*/ false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4168 | |
| 4169 | // A 32-bit variable-flag where runtime returns 1 for the last iteration. |
| 4170 | // This will be used to implement clause 'lastprivate'. |
| 4171 | QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 4172 | VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); |
| 4173 | IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4174 | SemaRef.AddInitializerToDecl(ILDecl, |
| 4175 | SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), |
| 4176 | /*DirectInit*/ false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4177 | |
| 4178 | // Stride variable returned by runtime (we initialize it to 1 by default). |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4179 | VarDecl *STDecl = |
| 4180 | buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); |
| 4181 | ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4182 | SemaRef.AddInitializerToDecl(STDecl, |
| 4183 | SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), |
| 4184 | /*DirectInit*/ false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4185 | |
| 4186 | // Build expression: UB = min(UB, LastIteration) |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4187 | // It is necessary for CodeGen of directives with static scheduling. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4188 | ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, |
| 4189 | UB.get(), LastIteration.get()); |
| 4190 | ExprResult CondOp = SemaRef.ActOnConditionalOp( |
| 4191 | InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get()); |
| 4192 | EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), |
| 4193 | CondOp.get()); |
| 4194 | EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 4195 | |
| 4196 | // If we have a combined directive that combines 'distribute', 'for' or |
| 4197 | // 'simd' we need to be able to access the bounds of the schedule of the |
| 4198 | // enclosing region. E.g. in 'distribute parallel for' the bounds obtained |
| 4199 | // by scheduling 'distribute' have to be passed to the schedule of 'for'. |
| 4200 | if (isOpenMPLoopBoundSharingDirective(DKind)) { |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 4201 | |
Carlo Bertolli | ffafe10 | 2017-04-20 00:39:39 +0000 | [diff] [blame] | 4202 | // Lower bound variable, initialized with zero. |
| 4203 | VarDecl *CombLBDecl = |
| 4204 | buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); |
| 4205 | CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); |
| 4206 | SemaRef.AddInitializerToDecl( |
| 4207 | CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), |
| 4208 | /*DirectInit*/ false); |
| 4209 | |
| 4210 | // Upper bound variable, initialized with last iteration number. |
| 4211 | VarDecl *CombUBDecl = |
| 4212 | buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); |
| 4213 | CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); |
| 4214 | SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), |
| 4215 | /*DirectInit*/ false); |
| 4216 | |
| 4217 | ExprResult CombIsUBGreater = SemaRef.BuildBinOp( |
| 4218 | CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); |
| 4219 | ExprResult CombCondOp = |
| 4220 | SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), |
| 4221 | LastIteration.get(), CombUB.get()); |
| 4222 | CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), |
| 4223 | CombCondOp.get()); |
| 4224 | CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get()); |
| 4225 | |
| 4226 | auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 4227 | // We expect to have at least 2 more parameters than the 'parallel' |
| 4228 | // directive does - the lower and upper bounds of the previous schedule. |
| 4229 | assert(CD->getNumParams() >= 4 && |
| 4230 | "Unexpected number of parameters in loop combined directive"); |
| 4231 | |
| 4232 | // Set the proper type for the bounds given what we learned from the |
| 4233 | // enclosed loops. |
| 4234 | auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2); |
| 4235 | auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3); |
| 4236 | |
| 4237 | // Previous lower and upper bounds are obtained from the region |
| 4238 | // parameters. |
| 4239 | PrevLB = |
| 4240 | buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); |
| 4241 | PrevUB = |
| 4242 | buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); |
| 4243 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | // Build the iteration variable and its initialization before loop. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4247 | ExprResult IV; |
Carlo Bertolli | ffafe10 | 2017-04-20 00:39:39 +0000 | [diff] [blame] | 4248 | ExprResult Init, CombInit; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4249 | { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4250 | VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); |
| 4251 | IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4252 | Expr *RHS = |
| 4253 | (isOpenMPWorksharingDirective(DKind) || |
| 4254 | isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) |
| 4255 | ? LB.get() |
| 4256 | : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4257 | Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); |
| 4258 | Init = SemaRef.ActOnFinishFullExpr(Init.get()); |
Carlo Bertolli | ffafe10 | 2017-04-20 00:39:39 +0000 | [diff] [blame] | 4259 | |
| 4260 | if (isOpenMPLoopBoundSharingDirective(DKind)) { |
| 4261 | Expr *CombRHS = |
| 4262 | (isOpenMPWorksharingDirective(DKind) || |
| 4263 | isOpenMPTaskLoopDirective(DKind) || |
| 4264 | isOpenMPDistributeDirective(DKind)) |
| 4265 | ? CombLB.get() |
| 4266 | : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); |
| 4267 | CombInit = |
| 4268 | SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); |
| 4269 | CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get()); |
| 4270 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4273 | // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4274 | SourceLocation CondLoc; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4275 | ExprResult Cond = |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 4276 | (isOpenMPWorksharingDirective(DKind) || |
| 4277 | isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4278 | ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) |
| 4279 | : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), |
| 4280 | NumIterations.get()); |
Carlo Bertolli | ffafe10 | 2017-04-20 00:39:39 +0000 | [diff] [blame] | 4281 | ExprResult CombCond; |
| 4282 | if (isOpenMPLoopBoundSharingDirective(DKind)) { |
| 4283 | CombCond = |
| 4284 | SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get()); |
| 4285 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4286 | // Loop increment (IV = IV + 1) |
| 4287 | SourceLocation IncLoc; |
| 4288 | ExprResult Inc = |
| 4289 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), |
| 4290 | SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); |
| 4291 | if (!Inc.isUsable()) |
| 4292 | return 0; |
| 4293 | Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4294 | Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); |
| 4295 | if (!Inc.isUsable()) |
| 4296 | return 0; |
| 4297 | |
| 4298 | // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). |
| 4299 | // Used for directives with static scheduling. |
Carlo Bertolli | ffafe10 | 2017-04-20 00:39:39 +0000 | [diff] [blame] | 4300 | // In combined construct, add combined version that use CombLB and CombUB |
| 4301 | // base variables for the update |
| 4302 | ExprResult NextLB, NextUB, CombNextLB, CombNextUB; |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 4303 | if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || |
| 4304 | isOpenMPDistributeDirective(DKind)) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4305 | // LB + ST |
| 4306 | NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); |
| 4307 | if (!NextLB.isUsable()) |
| 4308 | return 0; |
| 4309 | // LB = LB + ST |
| 4310 | NextLB = |
| 4311 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); |
| 4312 | NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); |
| 4313 | if (!NextLB.isUsable()) |
| 4314 | return 0; |
| 4315 | // UB + ST |
| 4316 | NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); |
| 4317 | if (!NextUB.isUsable()) |
| 4318 | return 0; |
| 4319 | // UB = UB + ST |
| 4320 | NextUB = |
| 4321 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); |
| 4322 | NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); |
| 4323 | if (!NextUB.isUsable()) |
| 4324 | return 0; |
Carlo Bertolli | ffafe10 | 2017-04-20 00:39:39 +0000 | [diff] [blame] | 4325 | if (isOpenMPLoopBoundSharingDirective(DKind)) { |
| 4326 | CombNextLB = |
| 4327 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); |
| 4328 | if (!NextLB.isUsable()) |
| 4329 | return 0; |
| 4330 | // LB = LB + ST |
| 4331 | CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), |
| 4332 | CombNextLB.get()); |
| 4333 | CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get()); |
| 4334 | if (!CombNextLB.isUsable()) |
| 4335 | return 0; |
| 4336 | // UB + ST |
| 4337 | CombNextUB = |
| 4338 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); |
| 4339 | if (!CombNextUB.isUsable()) |
| 4340 | return 0; |
| 4341 | // UB = UB + ST |
| 4342 | CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), |
| 4343 | CombNextUB.get()); |
| 4344 | CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get()); |
| 4345 | if (!CombNextUB.isUsable()) |
| 4346 | return 0; |
| 4347 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4348 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4349 | |
Carlo Bertolli | ffafe10 | 2017-04-20 00:39:39 +0000 | [diff] [blame] | 4350 | // Create increment expression for distribute loop when combined in a same |
Carlo Bertolli | 8429d81 | 2017-02-17 21:29:13 +0000 | [diff] [blame] | 4351 | // directive with for as IV = IV + ST; ensure upper bound expression based |
| 4352 | // on PrevUB instead of NumIterations - used to implement 'for' when found |
| 4353 | // in combination with 'distribute', like in 'distribute parallel for' |
| 4354 | SourceLocation DistIncLoc; |
| 4355 | ExprResult DistCond, DistInc, PrevEUB; |
| 4356 | if (isOpenMPLoopBoundSharingDirective(DKind)) { |
| 4357 | DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()); |
| 4358 | assert(DistCond.isUsable() && "distribute cond expr was not built"); |
| 4359 | |
| 4360 | DistInc = |
| 4361 | SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); |
| 4362 | assert(DistInc.isUsable() && "distribute inc expr was not built"); |
| 4363 | DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), |
| 4364 | DistInc.get()); |
| 4365 | DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get()); |
| 4366 | assert(DistInc.isUsable() && "distribute inc expr was not built"); |
| 4367 | |
| 4368 | // Build expression: UB = min(UB, prevUB) for #for in composite or combined |
| 4369 | // construct |
| 4370 | SourceLocation DistEUBLoc; |
| 4371 | ExprResult IsUBGreater = |
| 4372 | SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); |
| 4373 | ExprResult CondOp = SemaRef.ActOnConditionalOp( |
| 4374 | DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); |
| 4375 | PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), |
| 4376 | CondOp.get()); |
| 4377 | PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get()); |
| 4378 | } |
| 4379 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4380 | // Build updates and final values of the loop counters. |
| 4381 | bool HasErrors = false; |
| 4382 | Built.Counters.resize(NestedLoopCount); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4383 | Built.Inits.resize(NestedLoopCount); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4384 | Built.Updates.resize(NestedLoopCount); |
| 4385 | Built.Finals.resize(NestedLoopCount); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4386 | SmallVector<Expr *, 4> LoopMultipliers; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4387 | { |
| 4388 | ExprResult Div; |
| 4389 | // Go from inner nested loop to outer. |
| 4390 | for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { |
| 4391 | LoopIterationSpace &IS = IterSpaces[Cnt]; |
| 4392 | SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); |
| 4393 | // Build: Iter = (IV / Div) % IS.NumIters |
| 4394 | // where Div is product of previous iterations' IS.NumIters. |
| 4395 | ExprResult Iter; |
| 4396 | if (Div.isUsable()) { |
| 4397 | Iter = |
| 4398 | SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); |
| 4399 | } else { |
| 4400 | Iter = IV; |
| 4401 | assert((Cnt == (int)NestedLoopCount - 1) && |
| 4402 | "unusable div expected on first iteration only"); |
| 4403 | } |
| 4404 | |
| 4405 | if (Cnt != 0 && Iter.isUsable()) |
| 4406 | Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), |
| 4407 | IS.NumIterations); |
| 4408 | if (!Iter.isUsable()) { |
| 4409 | HasErrors = true; |
| 4410 | break; |
| 4411 | } |
| 4412 | |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 4413 | // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4414 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); |
| 4415 | auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(), |
| 4416 | IS.CounterVar->getExprLoc(), |
| 4417 | /*RefersToCapture=*/true); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4418 | ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4419 | IS.CounterInit, Captures); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4420 | if (!Init.isUsable()) { |
| 4421 | HasErrors = true; |
| 4422 | break; |
| 4423 | } |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4424 | ExprResult Update = BuildCounterUpdate( |
| 4425 | SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, |
| 4426 | IS.CounterStep, IS.Subtract, &Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4427 | if (!Update.isUsable()) { |
| 4428 | HasErrors = true; |
| 4429 | break; |
| 4430 | } |
| 4431 | |
| 4432 | // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step |
| 4433 | ExprResult Final = BuildCounterUpdate( |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 4434 | SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4435 | IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4436 | if (!Final.isUsable()) { |
| 4437 | HasErrors = true; |
| 4438 | break; |
| 4439 | } |
| 4440 | |
| 4441 | // Build Div for the next iteration: Div <- Div * IS.NumIters |
| 4442 | if (Cnt != 0) { |
| 4443 | if (Div.isUnset()) |
| 4444 | Div = IS.NumIterations; |
| 4445 | else |
| 4446 | Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), |
| 4447 | IS.NumIterations); |
| 4448 | |
| 4449 | // Add parentheses (for debugging purposes only). |
| 4450 | if (Div.isUsable()) |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4451 | Div = tryBuildCapture(SemaRef, Div.get(), Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4452 | if (!Div.isUsable()) { |
| 4453 | HasErrors = true; |
| 4454 | break; |
| 4455 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4456 | LoopMultipliers.push_back(Div.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4457 | } |
| 4458 | if (!Update.isUsable() || !Final.isUsable()) { |
| 4459 | HasErrors = true; |
| 4460 | break; |
| 4461 | } |
| 4462 | // Save results |
| 4463 | Built.Counters[Cnt] = IS.CounterVar; |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 4464 | Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4465 | Built.Inits[Cnt] = Init.get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4466 | Built.Updates[Cnt] = Update.get(); |
| 4467 | Built.Finals[Cnt] = Final.get(); |
| 4468 | } |
| 4469 | } |
| 4470 | |
| 4471 | if (HasErrors) |
| 4472 | return 0; |
| 4473 | |
| 4474 | // Save results |
| 4475 | Built.IterationVarRef = IV.get(); |
| 4476 | Built.LastIteration = LastIteration.get(); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 4477 | Built.NumIterations = NumIterations.get(); |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 4478 | Built.CalcLastIteration = |
| 4479 | SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4480 | Built.PreCond = PreCond.get(); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4481 | Built.PreInits = buildPreInits(C, Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4482 | Built.Cond = Cond.get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4483 | Built.Init = Init.get(); |
| 4484 | Built.Inc = Inc.get(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4485 | Built.LB = LB.get(); |
| 4486 | Built.UB = UB.get(); |
| 4487 | Built.IL = IL.get(); |
| 4488 | Built.ST = ST.get(); |
| 4489 | Built.EUB = EUB.get(); |
| 4490 | Built.NLB = NextLB.get(); |
| 4491 | Built.NUB = NextUB.get(); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 4492 | Built.PrevLB = PrevLB.get(); |
| 4493 | Built.PrevUB = PrevUB.get(); |
Carlo Bertolli | 8429d81 | 2017-02-17 21:29:13 +0000 | [diff] [blame] | 4494 | Built.DistInc = DistInc.get(); |
| 4495 | Built.PrevEUB = PrevEUB.get(); |
Carlo Bertolli | ffafe10 | 2017-04-20 00:39:39 +0000 | [diff] [blame] | 4496 | Built.DistCombinedFields.LB = CombLB.get(); |
| 4497 | Built.DistCombinedFields.UB = CombUB.get(); |
| 4498 | Built.DistCombinedFields.EUB = CombEUB.get(); |
| 4499 | Built.DistCombinedFields.Init = CombInit.get(); |
| 4500 | Built.DistCombinedFields.Cond = CombCond.get(); |
| 4501 | Built.DistCombinedFields.NLB = CombNextLB.get(); |
| 4502 | Built.DistCombinedFields.NUB = CombNextUB.get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4503 | |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4504 | Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get(); |
| 4505 | // Fill data for doacross depend clauses. |
| 4506 | for (auto Pair : DSA.getDoacrossDependClauses()) { |
| 4507 | if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) |
| 4508 | Pair.first->setCounterValue(CounterVal); |
| 4509 | else { |
| 4510 | if (NestedLoopCount != Pair.second.size() || |
| 4511 | NestedLoopCount != LoopMultipliers.size() + 1) { |
| 4512 | // Erroneous case - clause has some problems. |
| 4513 | Pair.first->setCounterValue(CounterVal); |
| 4514 | continue; |
| 4515 | } |
| 4516 | assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink); |
| 4517 | auto I = Pair.second.rbegin(); |
| 4518 | auto IS = IterSpaces.rbegin(); |
| 4519 | auto ILM = LoopMultipliers.rbegin(); |
| 4520 | Expr *UpCounterVal = CounterVal; |
| 4521 | Expr *Multiplier = nullptr; |
| 4522 | for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { |
| 4523 | if (I->first) { |
| 4524 | assert(IS->CounterStep); |
| 4525 | Expr *NormalizedOffset = |
| 4526 | SemaRef |
| 4527 | .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div, |
| 4528 | I->first, IS->CounterStep) |
| 4529 | .get(); |
| 4530 | if (Multiplier) { |
| 4531 | NormalizedOffset = |
| 4532 | SemaRef |
| 4533 | .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul, |
| 4534 | NormalizedOffset, Multiplier) |
| 4535 | .get(); |
| 4536 | } |
| 4537 | assert(I->second == OO_Plus || I->second == OO_Minus); |
| 4538 | BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4539 | UpCounterVal = SemaRef |
| 4540 | .BuildBinOp(CurScope, I->first->getExprLoc(), BOK, |
| 4541 | UpCounterVal, NormalizedOffset) |
| 4542 | .get(); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4543 | } |
| 4544 | Multiplier = *ILM; |
| 4545 | ++I; |
| 4546 | ++IS; |
| 4547 | ++ILM; |
| 4548 | } |
| 4549 | Pair.first->setCounterValue(UpCounterVal); |
| 4550 | } |
| 4551 | } |
| 4552 | |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 4553 | return NestedLoopCount; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4556 | static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 4557 | auto CollapseClauses = |
| 4558 | OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); |
| 4559 | if (CollapseClauses.begin() != CollapseClauses.end()) |
| 4560 | return (*CollapseClauses.begin())->getNumForLoops(); |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 4561 | return nullptr; |
| 4562 | } |
| 4563 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4564 | static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 4565 | auto OrderedClauses = |
| 4566 | OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); |
| 4567 | if (OrderedClauses.begin() != OrderedClauses.end()) |
| 4568 | return (*OrderedClauses.begin())->getNumForLoops(); |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4569 | return nullptr; |
| 4570 | } |
| 4571 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 4572 | static bool checkSimdlenSafelenSpecified(Sema &S, |
| 4573 | const ArrayRef<OMPClause *> Clauses) { |
| 4574 | OMPSafelenClause *Safelen = nullptr; |
| 4575 | OMPSimdlenClause *Simdlen = nullptr; |
| 4576 | |
| 4577 | for (auto *Clause : Clauses) { |
| 4578 | if (Clause->getClauseKind() == OMPC_safelen) |
| 4579 | Safelen = cast<OMPSafelenClause>(Clause); |
| 4580 | else if (Clause->getClauseKind() == OMPC_simdlen) |
| 4581 | Simdlen = cast<OMPSimdlenClause>(Clause); |
| 4582 | if (Safelen && Simdlen) |
| 4583 | break; |
| 4584 | } |
| 4585 | |
| 4586 | if (Simdlen && Safelen) { |
| 4587 | llvm::APSInt SimdlenRes, SafelenRes; |
| 4588 | auto SimdlenLength = Simdlen->getSimdlen(); |
| 4589 | auto SafelenLength = Safelen->getSafelen(); |
| 4590 | if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || |
| 4591 | SimdlenLength->isInstantiationDependent() || |
| 4592 | SimdlenLength->containsUnexpandedParameterPack()) |
| 4593 | return false; |
| 4594 | if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || |
| 4595 | SafelenLength->isInstantiationDependent() || |
| 4596 | SafelenLength->containsUnexpandedParameterPack()) |
| 4597 | return false; |
| 4598 | SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context); |
| 4599 | SafelenLength->EvaluateAsInt(SafelenRes, S.Context); |
| 4600 | // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] |
| 4601 | // If both simdlen and safelen clauses are specified, the value of the |
| 4602 | // simdlen parameter must be less than or equal to the value of the safelen |
| 4603 | // parameter. |
| 4604 | if (SimdlenRes > SafelenRes) { |
| 4605 | S.Diag(SimdlenLength->getExprLoc(), |
| 4606 | diag::err_omp_wrong_simdlen_safelen_values) |
| 4607 | << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); |
| 4608 | return true; |
| 4609 | } |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 4610 | } |
| 4611 | return false; |
| 4612 | } |
| 4613 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4614 | StmtResult Sema::ActOnOpenMPSimdDirective( |
| 4615 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4616 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4617 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4618 | if (!AStmt) |
| 4619 | return StmtError(); |
| 4620 | |
| 4621 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4622 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4623 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4624 | // define the nested loops number. |
| 4625 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 4626 | OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), |
| 4627 | AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 4628 | if (NestedLoopCount == 0) |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4629 | return StmtError(); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4630 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4631 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 4632 | "omp simd loop exprs were not built"); |
| 4633 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 4634 | if (!CurContext->isDependentContext()) { |
| 4635 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4636 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4637 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 4638 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4639 | B.NumIterations, *this, CurScope, |
| 4640 | DSAStack)) |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 4641 | return StmtError(); |
| 4642 | } |
| 4643 | } |
| 4644 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 4645 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 4646 | return StmtError(); |
| 4647 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4648 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4649 | return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
| 4650 | Clauses, AStmt, B); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4653 | StmtResult Sema::ActOnOpenMPForDirective( |
| 4654 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4655 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4656 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4657 | if (!AStmt) |
| 4658 | return StmtError(); |
| 4659 | |
| 4660 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4661 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4662 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4663 | // define the nested loops number. |
| 4664 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 4665 | OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), |
| 4666 | AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 4667 | if (NestedLoopCount == 0) |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 4668 | return StmtError(); |
| 4669 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4670 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 4671 | "omp for loop exprs were not built"); |
| 4672 | |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4673 | if (!CurContext->isDependentContext()) { |
| 4674 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4675 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4676 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4677 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4678 | B.NumIterations, *this, CurScope, |
| 4679 | DSAStack)) |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4680 | return StmtError(); |
| 4681 | } |
| 4682 | } |
| 4683 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 4684 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4685 | return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4686 | Clauses, AStmt, B, DSAStack->isCancelRegion()); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4689 | StmtResult Sema::ActOnOpenMPForSimdDirective( |
| 4690 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4691 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4692 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4693 | if (!AStmt) |
| 4694 | return StmtError(); |
| 4695 | |
| 4696 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4697 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4698 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4699 | // define the nested loops number. |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4700 | unsigned NestedLoopCount = |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4701 | CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), |
| 4702 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 4703 | VarsWithImplicitDSA, B); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4704 | if (NestedLoopCount == 0) |
| 4705 | return StmtError(); |
| 4706 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4707 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 4708 | "omp for simd loop exprs were not built"); |
| 4709 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 4710 | if (!CurContext->isDependentContext()) { |
| 4711 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4712 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4713 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 4714 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4715 | B.NumIterations, *this, CurScope, |
| 4716 | DSAStack)) |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 4717 | return StmtError(); |
| 4718 | } |
| 4719 | } |
| 4720 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 4721 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 4722 | return StmtError(); |
| 4723 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4724 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4725 | return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
| 4726 | Clauses, AStmt, B); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4729 | StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, |
| 4730 | Stmt *AStmt, |
| 4731 | SourceLocation StartLoc, |
| 4732 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4733 | if (!AStmt) |
| 4734 | return StmtError(); |
| 4735 | |
| 4736 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4737 | auto BaseStmt = AStmt; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4738 | while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4739 | BaseStmt = CS->getCapturedStmt(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4740 | if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4741 | auto S = C->children(); |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4742 | if (S.begin() == S.end()) |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4743 | return StmtError(); |
| 4744 | // All associated statements must be '#pragma omp section' except for |
| 4745 | // the first one. |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4746 | for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4747 | if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { |
| 4748 | if (SectionStmt) |
| 4749 | Diag(SectionStmt->getLocStart(), |
| 4750 | diag::err_omp_sections_substmt_not_section); |
| 4751 | return StmtError(); |
| 4752 | } |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4753 | cast<OMPSectionDirective>(SectionStmt) |
| 4754 | ->setHasCancel(DSAStack->isCancelRegion()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4755 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4756 | } else { |
| 4757 | Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); |
| 4758 | return StmtError(); |
| 4759 | } |
| 4760 | |
| 4761 | getCurFunction()->setHasBranchProtectedScope(); |
| 4762 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4763 | return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, |
| 4764 | DSAStack->isCancelRegion()); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4767 | StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, |
| 4768 | SourceLocation StartLoc, |
| 4769 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4770 | if (!AStmt) |
| 4771 | return StmtError(); |
| 4772 | |
| 4773 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4774 | |
| 4775 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4776 | DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4777 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4778 | return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, |
| 4779 | DSAStack->isCancelRegion()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 4782 | StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, |
| 4783 | Stmt *AStmt, |
| 4784 | SourceLocation StartLoc, |
| 4785 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4786 | if (!AStmt) |
| 4787 | return StmtError(); |
| 4788 | |
| 4789 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | 74a05c9 | 2014-07-15 02:55:09 +0000 | [diff] [blame] | 4790 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 4791 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | 74a05c9 | 2014-07-15 02:55:09 +0000 | [diff] [blame] | 4792 | |
Alexey Bataev | 3255bf3 | 2015-01-19 05:20:46 +0000 | [diff] [blame] | 4793 | // OpenMP [2.7.3, single Construct, Restrictions] |
| 4794 | // The copyprivate clause must not be used with the nowait clause. |
| 4795 | OMPClause *Nowait = nullptr; |
| 4796 | OMPClause *Copyprivate = nullptr; |
| 4797 | for (auto *Clause : Clauses) { |
| 4798 | if (Clause->getClauseKind() == OMPC_nowait) |
| 4799 | Nowait = Clause; |
| 4800 | else if (Clause->getClauseKind() == OMPC_copyprivate) |
| 4801 | Copyprivate = Clause; |
| 4802 | if (Copyprivate && Nowait) { |
| 4803 | Diag(Copyprivate->getLocStart(), |
| 4804 | diag::err_omp_single_copyprivate_with_nowait); |
| 4805 | Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); |
| 4806 | return StmtError(); |
| 4807 | } |
| 4808 | } |
| 4809 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 4810 | return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 4811 | } |
| 4812 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 4813 | StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, |
| 4814 | SourceLocation StartLoc, |
| 4815 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4816 | if (!AStmt) |
| 4817 | return StmtError(); |
| 4818 | |
| 4819 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 4820 | |
| 4821 | getCurFunction()->setHasBranchProtectedScope(); |
| 4822 | |
| 4823 | return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 4824 | } |
| 4825 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 4826 | StmtResult Sema::ActOnOpenMPCriticalDirective( |
| 4827 | const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, |
| 4828 | Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4829 | if (!AStmt) |
| 4830 | return StmtError(); |
| 4831 | |
| 4832 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 4833 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 4834 | bool ErrorFound = false; |
| 4835 | llvm::APSInt Hint; |
| 4836 | SourceLocation HintLoc; |
| 4837 | bool DependentHint = false; |
| 4838 | for (auto *C : Clauses) { |
| 4839 | if (C->getClauseKind() == OMPC_hint) { |
| 4840 | if (!DirName.getName()) { |
| 4841 | Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name); |
| 4842 | ErrorFound = true; |
| 4843 | } |
| 4844 | Expr *E = cast<OMPHintClause>(C)->getHint(); |
| 4845 | if (E->isTypeDependent() || E->isValueDependent() || |
| 4846 | E->isInstantiationDependent()) |
| 4847 | DependentHint = true; |
| 4848 | else { |
| 4849 | Hint = E->EvaluateKnownConstInt(Context); |
| 4850 | HintLoc = C->getLocStart(); |
| 4851 | } |
| 4852 | } |
| 4853 | } |
| 4854 | if (ErrorFound) |
| 4855 | return StmtError(); |
| 4856 | auto Pair = DSAStack->getCriticalWithHint(DirName); |
| 4857 | if (Pair.first && DirName.getName() && !DependentHint) { |
| 4858 | if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { |
| 4859 | Diag(StartLoc, diag::err_omp_critical_with_hint); |
| 4860 | if (HintLoc.isValid()) { |
| 4861 | Diag(HintLoc, diag::note_omp_critical_hint_here) |
| 4862 | << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); |
| 4863 | } else |
| 4864 | Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; |
| 4865 | if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) { |
| 4866 | Diag(C->getLocStart(), diag::note_omp_critical_hint_here) |
| 4867 | << 1 |
| 4868 | << C->getHint()->EvaluateKnownConstInt(Context).toString( |
| 4869 | /*Radix=*/10, /*Signed=*/false); |
| 4870 | } else |
| 4871 | Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1; |
| 4872 | } |
| 4873 | } |
| 4874 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 4875 | getCurFunction()->setHasBranchProtectedScope(); |
| 4876 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 4877 | auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, |
| 4878 | Clauses, AStmt); |
| 4879 | if (!Pair.first && DirName.getName() && !DependentHint) |
| 4880 | DSAStack->addCriticalWithHint(Dir, Hint); |
| 4881 | return Dir; |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4884 | StmtResult Sema::ActOnOpenMPParallelForDirective( |
| 4885 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4886 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4887 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4888 | if (!AStmt) |
| 4889 | return StmtError(); |
| 4890 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4891 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 4892 | // 1.2.2 OpenMP Language Terminology |
| 4893 | // Structured block - An executable statement with a single entry at the |
| 4894 | // top and a single exit at the bottom. |
| 4895 | // The point of exit cannot be a branch out of the structured block. |
| 4896 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 4897 | CS->getCapturedDecl()->setNothrow(); |
| 4898 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4899 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4900 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4901 | // define the nested loops number. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4902 | unsigned NestedLoopCount = |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4903 | CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), |
| 4904 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 4905 | VarsWithImplicitDSA, B); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4906 | if (NestedLoopCount == 0) |
| 4907 | return StmtError(); |
| 4908 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4909 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 4910 | "omp parallel for loop exprs were not built"); |
| 4911 | |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4912 | if (!CurContext->isDependentContext()) { |
| 4913 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4914 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4915 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4916 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4917 | B.NumIterations, *this, CurScope, |
| 4918 | DSAStack)) |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4919 | return StmtError(); |
| 4920 | } |
| 4921 | } |
| 4922 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4923 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4924 | return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4925 | NestedLoopCount, Clauses, AStmt, B, |
| 4926 | DSAStack->isCancelRegion()); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4929 | StmtResult Sema::ActOnOpenMPParallelForSimdDirective( |
| 4930 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4931 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4932 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4933 | if (!AStmt) |
| 4934 | return StmtError(); |
| 4935 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4936 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 4937 | // 1.2.2 OpenMP Language Terminology |
| 4938 | // Structured block - An executable statement with a single entry at the |
| 4939 | // top and a single exit at the bottom. |
| 4940 | // The point of exit cannot be a branch out of the structured block. |
| 4941 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 4942 | CS->getCapturedDecl()->setNothrow(); |
| 4943 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4944 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4945 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4946 | // define the nested loops number. |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4947 | unsigned NestedLoopCount = |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4948 | CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), |
| 4949 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 4950 | VarsWithImplicitDSA, B); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4951 | if (NestedLoopCount == 0) |
| 4952 | return StmtError(); |
| 4953 | |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 4954 | if (!CurContext->isDependentContext()) { |
| 4955 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4956 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4957 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 4958 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4959 | B.NumIterations, *this, CurScope, |
| 4960 | DSAStack)) |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 4961 | return StmtError(); |
| 4962 | } |
| 4963 | } |
| 4964 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 4965 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 4966 | return StmtError(); |
| 4967 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4968 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4969 | return OMPParallelForSimdDirective::Create( |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4970 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4971 | } |
| 4972 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4973 | StmtResult |
| 4974 | Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, |
| 4975 | Stmt *AStmt, SourceLocation StartLoc, |
| 4976 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4977 | if (!AStmt) |
| 4978 | return StmtError(); |
| 4979 | |
| 4980 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4981 | auto BaseStmt = AStmt; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4982 | while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4983 | BaseStmt = CS->getCapturedStmt(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4984 | if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4985 | auto S = C->children(); |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4986 | if (S.begin() == S.end()) |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4987 | return StmtError(); |
| 4988 | // All associated statements must be '#pragma omp section' except for |
| 4989 | // the first one. |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4990 | for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4991 | if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { |
| 4992 | if (SectionStmt) |
| 4993 | Diag(SectionStmt->getLocStart(), |
| 4994 | diag::err_omp_parallel_sections_substmt_not_section); |
| 4995 | return StmtError(); |
| 4996 | } |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4997 | cast<OMPSectionDirective>(SectionStmt) |
| 4998 | ->setHasCancel(DSAStack->isCancelRegion()); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4999 | } |
| 5000 | } else { |
| 5001 | Diag(AStmt->getLocStart(), |
| 5002 | diag::err_omp_parallel_sections_not_compound_stmt); |
| 5003 | return StmtError(); |
| 5004 | } |
| 5005 | |
| 5006 | getCurFunction()->setHasBranchProtectedScope(); |
| 5007 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 5008 | return OMPParallelSectionsDirective::Create( |
| 5009 | Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5012 | StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, |
| 5013 | Stmt *AStmt, SourceLocation StartLoc, |
| 5014 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 5015 | if (!AStmt) |
| 5016 | return StmtError(); |
| 5017 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5018 | auto *CS = cast<CapturedStmt>(AStmt); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5019 | // 1.2.2 OpenMP Language Terminology |
| 5020 | // Structured block - An executable statement with a single entry at the |
| 5021 | // top and a single exit at the bottom. |
| 5022 | // The point of exit cannot be a branch out of the structured block. |
| 5023 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5024 | CS->getCapturedDecl()->setNothrow(); |
| 5025 | |
| 5026 | getCurFunction()->setHasBranchProtectedScope(); |
| 5027 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 5028 | return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, |
| 5029 | DSAStack->isCancelRegion()); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 5032 | StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, |
| 5033 | SourceLocation EndLoc) { |
| 5034 | return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); |
| 5035 | } |
| 5036 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 5037 | StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, |
| 5038 | SourceLocation EndLoc) { |
| 5039 | return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); |
| 5040 | } |
| 5041 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 5042 | StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, |
| 5043 | SourceLocation EndLoc) { |
| 5044 | return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); |
| 5045 | } |
| 5046 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 5047 | StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt, |
| 5048 | SourceLocation StartLoc, |
| 5049 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 5050 | if (!AStmt) |
| 5051 | return StmtError(); |
| 5052 | |
| 5053 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 5054 | |
| 5055 | getCurFunction()->setHasBranchProtectedScope(); |
| 5056 | |
| 5057 | return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 5058 | } |
| 5059 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 5060 | StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, |
| 5061 | SourceLocation StartLoc, |
| 5062 | SourceLocation EndLoc) { |
| 5063 | assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); |
| 5064 | return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); |
| 5065 | } |
| 5066 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 5067 | StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, |
| 5068 | Stmt *AStmt, |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 5069 | SourceLocation StartLoc, |
| 5070 | SourceLocation EndLoc) { |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5071 | OMPClause *DependFound = nullptr; |
| 5072 | OMPClause *DependSourceClause = nullptr; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 5073 | OMPClause *DependSinkClause = nullptr; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5074 | bool ErrorFound = false; |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 5075 | OMPThreadsClause *TC = nullptr; |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 5076 | OMPSIMDClause *SC = nullptr; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5077 | for (auto *C : Clauses) { |
| 5078 | if (auto *DC = dyn_cast<OMPDependClause>(C)) { |
| 5079 | DependFound = C; |
| 5080 | if (DC->getDependencyKind() == OMPC_DEPEND_source) { |
| 5081 | if (DependSourceClause) { |
| 5082 | Diag(C->getLocStart(), diag::err_omp_more_one_clause) |
| 5083 | << getOpenMPDirectiveName(OMPD_ordered) |
| 5084 | << getOpenMPClauseName(OMPC_depend) << 2; |
| 5085 | ErrorFound = true; |
| 5086 | } else |
| 5087 | DependSourceClause = C; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 5088 | if (DependSinkClause) { |
| 5089 | Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) |
| 5090 | << 0; |
| 5091 | ErrorFound = true; |
| 5092 | } |
| 5093 | } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { |
| 5094 | if (DependSourceClause) { |
| 5095 | Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) |
| 5096 | << 1; |
| 5097 | ErrorFound = true; |
| 5098 | } |
| 5099 | DependSinkClause = C; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5100 | } |
| 5101 | } else if (C->getClauseKind() == OMPC_threads) |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 5102 | TC = cast<OMPThreadsClause>(C); |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 5103 | else if (C->getClauseKind() == OMPC_simd) |
| 5104 | SC = cast<OMPSIMDClause>(C); |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 5105 | } |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5106 | if (!ErrorFound && !SC && |
| 5107 | isOpenMPSimdDirective(DSAStack->getParentDirective())) { |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 5108 | // OpenMP [2.8.1,simd Construct, Restrictions] |
| 5109 | // An ordered construct with the simd clause is the only OpenMP construct |
| 5110 | // that can appear in the simd region. |
| 5111 | Diag(StartLoc, diag::err_omp_prohibited_region_simd); |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5112 | ErrorFound = true; |
| 5113 | } else if (DependFound && (TC || SC)) { |
| 5114 | Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd) |
| 5115 | << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); |
| 5116 | ErrorFound = true; |
| 5117 | } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) { |
| 5118 | Diag(DependFound->getLocStart(), |
| 5119 | diag::err_omp_ordered_directive_without_param); |
| 5120 | ErrorFound = true; |
| 5121 | } else if (TC || Clauses.empty()) { |
| 5122 | if (auto *Param = DSAStack->getParentOrderedRegionParam()) { |
| 5123 | SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc; |
| 5124 | Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) |
| 5125 | << (TC != nullptr); |
| 5126 | Diag(Param->getLocStart(), diag::note_omp_ordered_param); |
| 5127 | ErrorFound = true; |
| 5128 | } |
| 5129 | } |
| 5130 | if ((!AStmt && !DependFound) || ErrorFound) |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 5131 | return StmtError(); |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5132 | |
| 5133 | if (AStmt) { |
| 5134 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 5135 | |
| 5136 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 5137 | } |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 5138 | |
| 5139 | return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5142 | namespace { |
| 5143 | /// \brief Helper class for checking expression in 'omp atomic [update]' |
| 5144 | /// construct. |
| 5145 | class OpenMPAtomicUpdateChecker { |
| 5146 | /// \brief Error results for atomic update expressions. |
| 5147 | enum ExprAnalysisErrorCode { |
| 5148 | /// \brief A statement is not an expression statement. |
| 5149 | NotAnExpression, |
| 5150 | /// \brief Expression is not builtin binary or unary operation. |
| 5151 | NotABinaryOrUnaryExpression, |
| 5152 | /// \brief Unary operation is not post-/pre- increment/decrement operation. |
| 5153 | NotAnUnaryIncDecExpression, |
| 5154 | /// \brief An expression is not of scalar type. |
| 5155 | NotAScalarType, |
| 5156 | /// \brief A binary operation is not an assignment operation. |
| 5157 | NotAnAssignmentOp, |
| 5158 | /// \brief RHS part of the binary operation is not a binary expression. |
| 5159 | NotABinaryExpression, |
| 5160 | /// \brief RHS part is not additive/multiplicative/shift/biwise binary |
| 5161 | /// expression. |
| 5162 | NotABinaryOperator, |
| 5163 | /// \brief RHS binary operation does not have reference to the updated LHS |
| 5164 | /// part. |
| 5165 | NotAnUpdateExpression, |
| 5166 | /// \brief No errors is found. |
| 5167 | NoError |
| 5168 | }; |
| 5169 | /// \brief Reference to Sema. |
| 5170 | Sema &SemaRef; |
| 5171 | /// \brief A location for note diagnostics (when error is found). |
| 5172 | SourceLocation NoteLoc; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5173 | /// \brief 'x' lvalue part of the source atomic expression. |
| 5174 | Expr *X; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5175 | /// \brief 'expr' rvalue part of the source atomic expression. |
| 5176 | Expr *E; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5177 | /// \brief Helper expression of the form |
| 5178 | /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or |
| 5179 | /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. |
| 5180 | Expr *UpdateExpr; |
| 5181 | /// \brief Is 'x' a LHS in a RHS part of full update expression. It is |
| 5182 | /// important for non-associative operations. |
| 5183 | bool IsXLHSInRHSPart; |
| 5184 | BinaryOperatorKind Op; |
| 5185 | SourceLocation OpLoc; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5186 | /// \brief true if the source expression is a postfix unary operation, false |
| 5187 | /// if it is a prefix unary operation. |
| 5188 | bool IsPostfixUpdate; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5189 | |
| 5190 | public: |
| 5191 | OpenMPAtomicUpdateChecker(Sema &SemaRef) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5192 | : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5193 | IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5194 | /// \brief Check specified statement that it is suitable for 'atomic update' |
| 5195 | /// constructs and extract 'x', 'expr' and Operation from the original |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5196 | /// expression. If DiagId and NoteId == 0, then only check is performed |
| 5197 | /// without error notification. |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5198 | /// \param DiagId Diagnostic which should be emitted if error is found. |
| 5199 | /// \param NoteId Diagnostic note for the main error message. |
| 5200 | /// \return true if statement is not an update expression, false otherwise. |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5201 | bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5202 | /// \brief Return the 'x' lvalue part of the source atomic expression. |
| 5203 | Expr *getX() const { return X; } |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5204 | /// \brief Return the 'expr' rvalue part of the source atomic expression. |
| 5205 | Expr *getExpr() const { return E; } |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5206 | /// \brief Return the update expression used in calculation of the updated |
| 5207 | /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or |
| 5208 | /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. |
| 5209 | Expr *getUpdateExpr() const { return UpdateExpr; } |
| 5210 | /// \brief Return true if 'x' is LHS in RHS part of full update expression, |
| 5211 | /// false otherwise. |
| 5212 | bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } |
| 5213 | |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5214 | /// \brief true if the source expression is a postfix unary operation, false |
| 5215 | /// if it is a prefix unary operation. |
| 5216 | bool isPostfixUpdate() const { return IsPostfixUpdate; } |
| 5217 | |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5218 | private: |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5219 | bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, |
| 5220 | unsigned NoteId = 0); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5221 | }; |
| 5222 | } // namespace |
| 5223 | |
| 5224 | bool OpenMPAtomicUpdateChecker::checkBinaryOperation( |
| 5225 | BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { |
| 5226 | ExprAnalysisErrorCode ErrorFound = NoError; |
| 5227 | SourceLocation ErrorLoc, NoteLoc; |
| 5228 | SourceRange ErrorRange, NoteRange; |
| 5229 | // Allowed constructs are: |
| 5230 | // x = x binop expr; |
| 5231 | // x = expr binop x; |
| 5232 | if (AtomicBinOp->getOpcode() == BO_Assign) { |
| 5233 | X = AtomicBinOp->getLHS(); |
| 5234 | if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( |
| 5235 | AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { |
| 5236 | if (AtomicInnerBinOp->isMultiplicativeOp() || |
| 5237 | AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || |
| 5238 | AtomicInnerBinOp->isBitwiseOp()) { |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5239 | Op = AtomicInnerBinOp->getOpcode(); |
| 5240 | OpLoc = AtomicInnerBinOp->getOperatorLoc(); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5241 | auto *LHS = AtomicInnerBinOp->getLHS(); |
| 5242 | auto *RHS = AtomicInnerBinOp->getRHS(); |
| 5243 | llvm::FoldingSetNodeID XId, LHSId, RHSId; |
| 5244 | X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), |
| 5245 | /*Canonical=*/true); |
| 5246 | LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), |
| 5247 | /*Canonical=*/true); |
| 5248 | RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), |
| 5249 | /*Canonical=*/true); |
| 5250 | if (XId == LHSId) { |
| 5251 | E = RHS; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5252 | IsXLHSInRHSPart = true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5253 | } else if (XId == RHSId) { |
| 5254 | E = LHS; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5255 | IsXLHSInRHSPart = false; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5256 | } else { |
| 5257 | ErrorLoc = AtomicInnerBinOp->getExprLoc(); |
| 5258 | ErrorRange = AtomicInnerBinOp->getSourceRange(); |
| 5259 | NoteLoc = X->getExprLoc(); |
| 5260 | NoteRange = X->getSourceRange(); |
| 5261 | ErrorFound = NotAnUpdateExpression; |
| 5262 | } |
| 5263 | } else { |
| 5264 | ErrorLoc = AtomicInnerBinOp->getExprLoc(); |
| 5265 | ErrorRange = AtomicInnerBinOp->getSourceRange(); |
| 5266 | NoteLoc = AtomicInnerBinOp->getOperatorLoc(); |
| 5267 | NoteRange = SourceRange(NoteLoc, NoteLoc); |
| 5268 | ErrorFound = NotABinaryOperator; |
| 5269 | } |
| 5270 | } else { |
| 5271 | NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); |
| 5272 | NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); |
| 5273 | ErrorFound = NotABinaryExpression; |
| 5274 | } |
| 5275 | } else { |
| 5276 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 5277 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 5278 | NoteLoc = AtomicBinOp->getOperatorLoc(); |
| 5279 | NoteRange = SourceRange(NoteLoc, NoteLoc); |
| 5280 | ErrorFound = NotAnAssignmentOp; |
| 5281 | } |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5282 | if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5283 | SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; |
| 5284 | SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; |
| 5285 | return true; |
| 5286 | } else if (SemaRef.CurContext->isDependentContext()) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5287 | E = X = UpdateExpr = nullptr; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 5288 | return ErrorFound != NoError; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
| 5291 | bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, |
| 5292 | unsigned NoteId) { |
| 5293 | ExprAnalysisErrorCode ErrorFound = NoError; |
| 5294 | SourceLocation ErrorLoc, NoteLoc; |
| 5295 | SourceRange ErrorRange, NoteRange; |
| 5296 | // Allowed constructs are: |
| 5297 | // x++; |
| 5298 | // x--; |
| 5299 | // ++x; |
| 5300 | // --x; |
| 5301 | // x binop= expr; |
| 5302 | // x = x binop expr; |
| 5303 | // x = expr binop x; |
| 5304 | if (auto *AtomicBody = dyn_cast<Expr>(S)) { |
| 5305 | AtomicBody = AtomicBody->IgnoreParenImpCasts(); |
| 5306 | if (AtomicBody->getType()->isScalarType() || |
| 5307 | AtomicBody->isInstantiationDependent()) { |
| 5308 | if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( |
| 5309 | AtomicBody->IgnoreParenImpCasts())) { |
| 5310 | // Check for Compound Assignment Operation |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5311 | Op = BinaryOperator::getOpForCompoundAssignment( |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5312 | AtomicCompAssignOp->getOpcode()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5313 | OpLoc = AtomicCompAssignOp->getOperatorLoc(); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5314 | E = AtomicCompAssignOp->getRHS(); |
Kelvin Li | 4f161cf | 2016-07-20 19:41:17 +0000 | [diff] [blame] | 5315 | X = AtomicCompAssignOp->getLHS()->IgnoreParens(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5316 | IsXLHSInRHSPart = true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5317 | } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( |
| 5318 | AtomicBody->IgnoreParenImpCasts())) { |
| 5319 | // Check for Binary Operation |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5320 | if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5321 | return true; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5322 | } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( |
| 5323 | AtomicBody->IgnoreParenImpCasts())) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5324 | // Check for Unary Operation |
| 5325 | if (AtomicUnaryOp->isIncrementDecrementOp()) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5326 | IsPostfixUpdate = AtomicUnaryOp->isPostfix(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5327 | Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; |
| 5328 | OpLoc = AtomicUnaryOp->getOperatorLoc(); |
Kelvin Li | 4f161cf | 2016-07-20 19:41:17 +0000 | [diff] [blame] | 5329 | X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5330 | E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); |
| 5331 | IsXLHSInRHSPart = true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5332 | } else { |
| 5333 | ErrorFound = NotAnUnaryIncDecExpression; |
| 5334 | ErrorLoc = AtomicUnaryOp->getExprLoc(); |
| 5335 | ErrorRange = AtomicUnaryOp->getSourceRange(); |
| 5336 | NoteLoc = AtomicUnaryOp->getOperatorLoc(); |
| 5337 | NoteRange = SourceRange(NoteLoc, NoteLoc); |
| 5338 | } |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5339 | } else if (!AtomicBody->isInstantiationDependent()) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5340 | ErrorFound = NotABinaryOrUnaryExpression; |
| 5341 | NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); |
| 5342 | NoteRange = ErrorRange = AtomicBody->getSourceRange(); |
| 5343 | } |
| 5344 | } else { |
| 5345 | ErrorFound = NotAScalarType; |
| 5346 | NoteLoc = ErrorLoc = AtomicBody->getLocStart(); |
| 5347 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
| 5348 | } |
| 5349 | } else { |
| 5350 | ErrorFound = NotAnExpression; |
| 5351 | NoteLoc = ErrorLoc = S->getLocStart(); |
| 5352 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
| 5353 | } |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5354 | if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5355 | SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; |
| 5356 | SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; |
| 5357 | return true; |
| 5358 | } else if (SemaRef.CurContext->isDependentContext()) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5359 | E = X = UpdateExpr = nullptr; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 5360 | if (ErrorFound == NoError && E && X) { |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5361 | // Build an update expression of form 'OpaqueValueExpr(x) binop |
| 5362 | // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop |
| 5363 | // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. |
| 5364 | auto *OVEX = new (SemaRef.getASTContext()) |
| 5365 | OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); |
| 5366 | auto *OVEExpr = new (SemaRef.getASTContext()) |
| 5367 | OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); |
| 5368 | auto Update = |
| 5369 | SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, |
| 5370 | IsXLHSInRHSPart ? OVEExpr : OVEX); |
| 5371 | if (Update.isInvalid()) |
| 5372 | return true; |
| 5373 | Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), |
| 5374 | Sema::AA_Casting); |
| 5375 | if (Update.isInvalid()) |
| 5376 | return true; |
| 5377 | UpdateExpr = Update.get(); |
| 5378 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 5379 | return ErrorFound != NoError; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5382 | StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, |
| 5383 | Stmt *AStmt, |
| 5384 | SourceLocation StartLoc, |
| 5385 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 5386 | if (!AStmt) |
| 5387 | return StmtError(); |
| 5388 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5389 | auto *CS = cast<CapturedStmt>(AStmt); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5390 | // 1.2.2 OpenMP Language Terminology |
| 5391 | // Structured block - An executable statement with a single entry at the |
| 5392 | // top and a single exit at the bottom. |
| 5393 | // The point of exit cannot be a branch out of the structured block. |
| 5394 | // longjmp() and throw() must not violate the entry/exit criteria. |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5395 | OpenMPClauseKind AtomicKind = OMPC_unknown; |
| 5396 | SourceLocation AtomicKindLoc; |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 5397 | for (auto *C : Clauses) { |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 5398 | if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 5399 | C->getClauseKind() == OMPC_update || |
| 5400 | C->getClauseKind() == OMPC_capture) { |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5401 | if (AtomicKind != OMPC_unknown) { |
| 5402 | Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) |
| 5403 | << SourceRange(C->getLocStart(), C->getLocEnd()); |
| 5404 | Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) |
| 5405 | << getOpenMPClauseName(AtomicKind); |
| 5406 | } else { |
| 5407 | AtomicKind = C->getClauseKind(); |
| 5408 | AtomicKindLoc = C->getLocStart(); |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 5409 | } |
| 5410 | } |
| 5411 | } |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5412 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 5413 | auto Body = CS->getCapturedStmt(); |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 5414 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) |
| 5415 | Body = EWC->getSubExpr(); |
| 5416 | |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5417 | Expr *X = nullptr; |
| 5418 | Expr *V = nullptr; |
| 5419 | Expr *E = nullptr; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5420 | Expr *UE = nullptr; |
| 5421 | bool IsXLHSInRHSPart = false; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5422 | bool IsPostfixUpdate = false; |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5423 | // OpenMP [2.12.6, atomic Construct] |
| 5424 | // In the next expressions: |
| 5425 | // * x and v (as applicable) are both l-value expressions with scalar type. |
| 5426 | // * During the execution of an atomic region, multiple syntactic |
| 5427 | // occurrences of x must designate the same storage location. |
| 5428 | // * Neither of v and expr (as applicable) may access the storage location |
| 5429 | // designated by x. |
| 5430 | // * Neither of x and expr (as applicable) may access the storage location |
| 5431 | // designated by v. |
| 5432 | // * expr is an expression with scalar type. |
| 5433 | // * binop is one of +, *, -, /, &, ^, |, <<, or >>. |
| 5434 | // * binop, binop=, ++, and -- are not overloaded operators. |
| 5435 | // * The expression x binop expr must be numerically equivalent to x binop |
| 5436 | // (expr). This requirement is satisfied if the operators in expr have |
| 5437 | // precedence greater than binop, or by using parentheses around expr or |
| 5438 | // subexpressions of expr. |
| 5439 | // * The expression expr binop x must be numerically equivalent to (expr) |
| 5440 | // binop x. This requirement is satisfied if the operators in expr have |
| 5441 | // precedence equal to or greater than binop, or by using parentheses around |
| 5442 | // expr or subexpressions of expr. |
| 5443 | // * For forms that allow multiple occurrences of x, the number of times |
| 5444 | // that x is evaluated is unspecified. |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5445 | if (AtomicKind == OMPC_read) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5446 | enum { |
| 5447 | NotAnExpression, |
| 5448 | NotAnAssignmentOp, |
| 5449 | NotAScalarType, |
| 5450 | NotAnLValue, |
| 5451 | NoError |
| 5452 | } ErrorFound = NoError; |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5453 | SourceLocation ErrorLoc, NoteLoc; |
| 5454 | SourceRange ErrorRange, NoteRange; |
| 5455 | // If clause is read: |
| 5456 | // v = x; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5457 | if (auto *AtomicBody = dyn_cast<Expr>(Body)) { |
| 5458 | auto *AtomicBinOp = |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5459 | dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); |
| 5460 | if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { |
| 5461 | X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); |
| 5462 | V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); |
| 5463 | if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && |
| 5464 | (V->isInstantiationDependent() || V->getType()->isScalarType())) { |
| 5465 | if (!X->isLValue() || !V->isLValue()) { |
| 5466 | auto NotLValueExpr = X->isLValue() ? V : X; |
| 5467 | ErrorFound = NotAnLValue; |
| 5468 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 5469 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 5470 | NoteLoc = NotLValueExpr->getExprLoc(); |
| 5471 | NoteRange = NotLValueExpr->getSourceRange(); |
| 5472 | } |
| 5473 | } else if (!X->isInstantiationDependent() || |
| 5474 | !V->isInstantiationDependent()) { |
| 5475 | auto NotScalarExpr = |
| 5476 | (X->isInstantiationDependent() || X->getType()->isScalarType()) |
| 5477 | ? V |
| 5478 | : X; |
| 5479 | ErrorFound = NotAScalarType; |
| 5480 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 5481 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 5482 | NoteLoc = NotScalarExpr->getExprLoc(); |
| 5483 | NoteRange = NotScalarExpr->getSourceRange(); |
| 5484 | } |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5485 | } else if (!AtomicBody->isInstantiationDependent()) { |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5486 | ErrorFound = NotAnAssignmentOp; |
| 5487 | ErrorLoc = AtomicBody->getExprLoc(); |
| 5488 | ErrorRange = AtomicBody->getSourceRange(); |
| 5489 | NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() |
| 5490 | : AtomicBody->getExprLoc(); |
| 5491 | NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() |
| 5492 | : AtomicBody->getSourceRange(); |
| 5493 | } |
| 5494 | } else { |
| 5495 | ErrorFound = NotAnExpression; |
| 5496 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 5497 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5498 | } |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5499 | if (ErrorFound != NoError) { |
| 5500 | Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) |
| 5501 | << ErrorRange; |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5502 | Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound |
| 5503 | << NoteRange; |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5504 | return StmtError(); |
| 5505 | } else if (CurContext->isDependentContext()) |
| 5506 | V = X = nullptr; |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5507 | } else if (AtomicKind == OMPC_write) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5508 | enum { |
| 5509 | NotAnExpression, |
| 5510 | NotAnAssignmentOp, |
| 5511 | NotAScalarType, |
| 5512 | NotAnLValue, |
| 5513 | NoError |
| 5514 | } ErrorFound = NoError; |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5515 | SourceLocation ErrorLoc, NoteLoc; |
| 5516 | SourceRange ErrorRange, NoteRange; |
| 5517 | // If clause is write: |
| 5518 | // x = expr; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5519 | if (auto *AtomicBody = dyn_cast<Expr>(Body)) { |
| 5520 | auto *AtomicBinOp = |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5521 | dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); |
| 5522 | if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 5523 | X = AtomicBinOp->getLHS(); |
| 5524 | E = AtomicBinOp->getRHS(); |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5525 | if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && |
| 5526 | (E->isInstantiationDependent() || E->getType()->isScalarType())) { |
| 5527 | if (!X->isLValue()) { |
| 5528 | ErrorFound = NotAnLValue; |
| 5529 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 5530 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 5531 | NoteLoc = X->getExprLoc(); |
| 5532 | NoteRange = X->getSourceRange(); |
| 5533 | } |
| 5534 | } else if (!X->isInstantiationDependent() || |
| 5535 | !E->isInstantiationDependent()) { |
| 5536 | auto NotScalarExpr = |
| 5537 | (X->isInstantiationDependent() || X->getType()->isScalarType()) |
| 5538 | ? E |
| 5539 | : X; |
| 5540 | ErrorFound = NotAScalarType; |
| 5541 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 5542 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 5543 | NoteLoc = NotScalarExpr->getExprLoc(); |
| 5544 | NoteRange = NotScalarExpr->getSourceRange(); |
| 5545 | } |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5546 | } else if (!AtomicBody->isInstantiationDependent()) { |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5547 | ErrorFound = NotAnAssignmentOp; |
| 5548 | ErrorLoc = AtomicBody->getExprLoc(); |
| 5549 | ErrorRange = AtomicBody->getSourceRange(); |
| 5550 | NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() |
| 5551 | : AtomicBody->getExprLoc(); |
| 5552 | NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() |
| 5553 | : AtomicBody->getSourceRange(); |
| 5554 | } |
| 5555 | } else { |
| 5556 | ErrorFound = NotAnExpression; |
| 5557 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 5558 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5559 | } |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5560 | if (ErrorFound != NoError) { |
| 5561 | Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) |
| 5562 | << ErrorRange; |
| 5563 | Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound |
| 5564 | << NoteRange; |
| 5565 | return StmtError(); |
| 5566 | } else if (CurContext->isDependentContext()) |
| 5567 | E = X = nullptr; |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 5568 | } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5569 | // If clause is update: |
| 5570 | // x++; |
| 5571 | // x--; |
| 5572 | // ++x; |
| 5573 | // --x; |
| 5574 | // x binop= expr; |
| 5575 | // x = x binop expr; |
| 5576 | // x = expr binop x; |
| 5577 | OpenMPAtomicUpdateChecker Checker(*this); |
| 5578 | if (Checker.checkStatement( |
| 5579 | Body, (AtomicKind == OMPC_update) |
| 5580 | ? diag::err_omp_atomic_update_not_expression_statement |
| 5581 | : diag::err_omp_atomic_not_expression_statement, |
| 5582 | diag::note_omp_atomic_update)) |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 5583 | return StmtError(); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5584 | if (!CurContext->isDependentContext()) { |
| 5585 | E = Checker.getExpr(); |
| 5586 | X = Checker.getX(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5587 | UE = Checker.getUpdateExpr(); |
| 5588 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 5589 | } |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 5590 | } else if (AtomicKind == OMPC_capture) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5591 | enum { |
| 5592 | NotAnAssignmentOp, |
| 5593 | NotACompoundStatement, |
| 5594 | NotTwoSubstatements, |
| 5595 | NotASpecificExpression, |
| 5596 | NoError |
| 5597 | } ErrorFound = NoError; |
| 5598 | SourceLocation ErrorLoc, NoteLoc; |
| 5599 | SourceRange ErrorRange, NoteRange; |
| 5600 | if (auto *AtomicBody = dyn_cast<Expr>(Body)) { |
| 5601 | // If clause is a capture: |
| 5602 | // v = x++; |
| 5603 | // v = x--; |
| 5604 | // v = ++x; |
| 5605 | // v = --x; |
| 5606 | // v = x binop= expr; |
| 5607 | // v = x = x binop expr; |
| 5608 | // v = x = expr binop x; |
| 5609 | auto *AtomicBinOp = |
| 5610 | dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); |
| 5611 | if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { |
| 5612 | V = AtomicBinOp->getLHS(); |
| 5613 | Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); |
| 5614 | OpenMPAtomicUpdateChecker Checker(*this); |
| 5615 | if (Checker.checkStatement( |
| 5616 | Body, diag::err_omp_atomic_capture_not_expression_statement, |
| 5617 | diag::note_omp_atomic_update)) |
| 5618 | return StmtError(); |
| 5619 | E = Checker.getExpr(); |
| 5620 | X = Checker.getX(); |
| 5621 | UE = Checker.getUpdateExpr(); |
| 5622 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
| 5623 | IsPostfixUpdate = Checker.isPostfixUpdate(); |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5624 | } else if (!AtomicBody->isInstantiationDependent()) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5625 | ErrorLoc = AtomicBody->getExprLoc(); |
| 5626 | ErrorRange = AtomicBody->getSourceRange(); |
| 5627 | NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() |
| 5628 | : AtomicBody->getExprLoc(); |
| 5629 | NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() |
| 5630 | : AtomicBody->getSourceRange(); |
| 5631 | ErrorFound = NotAnAssignmentOp; |
| 5632 | } |
| 5633 | if (ErrorFound != NoError) { |
| 5634 | Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) |
| 5635 | << ErrorRange; |
| 5636 | Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; |
| 5637 | return StmtError(); |
| 5638 | } else if (CurContext->isDependentContext()) { |
| 5639 | UE = V = E = X = nullptr; |
| 5640 | } |
| 5641 | } else { |
| 5642 | // If clause is a capture: |
| 5643 | // { v = x; x = expr; } |
| 5644 | // { v = x; x++; } |
| 5645 | // { v = x; x--; } |
| 5646 | // { v = x; ++x; } |
| 5647 | // { v = x; --x; } |
| 5648 | // { v = x; x binop= expr; } |
| 5649 | // { v = x; x = x binop expr; } |
| 5650 | // { v = x; x = expr binop x; } |
| 5651 | // { x++; v = x; } |
| 5652 | // { x--; v = x; } |
| 5653 | // { ++x; v = x; } |
| 5654 | // { --x; v = x; } |
| 5655 | // { x binop= expr; v = x; } |
| 5656 | // { x = x binop expr; v = x; } |
| 5657 | // { x = expr binop x; v = x; } |
| 5658 | if (auto *CS = dyn_cast<CompoundStmt>(Body)) { |
| 5659 | // Check that this is { expr1; expr2; } |
| 5660 | if (CS->size() == 2) { |
| 5661 | auto *First = CS->body_front(); |
| 5662 | auto *Second = CS->body_back(); |
| 5663 | if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) |
| 5664 | First = EWC->getSubExpr()->IgnoreParenImpCasts(); |
| 5665 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) |
| 5666 | Second = EWC->getSubExpr()->IgnoreParenImpCasts(); |
| 5667 | // Need to find what subexpression is 'v' and what is 'x'. |
| 5668 | OpenMPAtomicUpdateChecker Checker(*this); |
| 5669 | bool IsUpdateExprFound = !Checker.checkStatement(Second); |
| 5670 | BinaryOperator *BinOp = nullptr; |
| 5671 | if (IsUpdateExprFound) { |
| 5672 | BinOp = dyn_cast<BinaryOperator>(First); |
| 5673 | IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; |
| 5674 | } |
| 5675 | if (IsUpdateExprFound && !CurContext->isDependentContext()) { |
| 5676 | // { v = x; x++; } |
| 5677 | // { v = x; x--; } |
| 5678 | // { v = x; ++x; } |
| 5679 | // { v = x; --x; } |
| 5680 | // { v = x; x binop= expr; } |
| 5681 | // { v = x; x = x binop expr; } |
| 5682 | // { v = x; x = expr binop x; } |
| 5683 | // Check that the first expression has form v = x. |
| 5684 | auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); |
| 5685 | llvm::FoldingSetNodeID XId, PossibleXId; |
| 5686 | Checker.getX()->Profile(XId, Context, /*Canonical=*/true); |
| 5687 | PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); |
| 5688 | IsUpdateExprFound = XId == PossibleXId; |
| 5689 | if (IsUpdateExprFound) { |
| 5690 | V = BinOp->getLHS(); |
| 5691 | X = Checker.getX(); |
| 5692 | E = Checker.getExpr(); |
| 5693 | UE = Checker.getUpdateExpr(); |
| 5694 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 5695 | IsPostfixUpdate = true; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5696 | } |
| 5697 | } |
| 5698 | if (!IsUpdateExprFound) { |
| 5699 | IsUpdateExprFound = !Checker.checkStatement(First); |
| 5700 | BinOp = nullptr; |
| 5701 | if (IsUpdateExprFound) { |
| 5702 | BinOp = dyn_cast<BinaryOperator>(Second); |
| 5703 | IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; |
| 5704 | } |
| 5705 | if (IsUpdateExprFound && !CurContext->isDependentContext()) { |
| 5706 | // { x++; v = x; } |
| 5707 | // { x--; v = x; } |
| 5708 | // { ++x; v = x; } |
| 5709 | // { --x; v = x; } |
| 5710 | // { x binop= expr; v = x; } |
| 5711 | // { x = x binop expr; v = x; } |
| 5712 | // { x = expr binop x; v = x; } |
| 5713 | // Check that the second expression has form v = x. |
| 5714 | auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); |
| 5715 | llvm::FoldingSetNodeID XId, PossibleXId; |
| 5716 | Checker.getX()->Profile(XId, Context, /*Canonical=*/true); |
| 5717 | PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); |
| 5718 | IsUpdateExprFound = XId == PossibleXId; |
| 5719 | if (IsUpdateExprFound) { |
| 5720 | V = BinOp->getLHS(); |
| 5721 | X = Checker.getX(); |
| 5722 | E = Checker.getExpr(); |
| 5723 | UE = Checker.getUpdateExpr(); |
| 5724 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 5725 | IsPostfixUpdate = false; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5726 | } |
| 5727 | } |
| 5728 | } |
| 5729 | if (!IsUpdateExprFound) { |
| 5730 | // { v = x; x = expr; } |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5731 | auto *FirstExpr = dyn_cast<Expr>(First); |
| 5732 | auto *SecondExpr = dyn_cast<Expr>(Second); |
| 5733 | if (!FirstExpr || !SecondExpr || |
| 5734 | !(FirstExpr->isInstantiationDependent() || |
| 5735 | SecondExpr->isInstantiationDependent())) { |
| 5736 | auto *FirstBinOp = dyn_cast<BinaryOperator>(First); |
| 5737 | if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5738 | ErrorFound = NotAnAssignmentOp; |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5739 | NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() |
| 5740 | : First->getLocStart(); |
| 5741 | NoteRange = ErrorRange = FirstBinOp |
| 5742 | ? FirstBinOp->getSourceRange() |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5743 | : SourceRange(ErrorLoc, ErrorLoc); |
| 5744 | } else { |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5745 | auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); |
| 5746 | if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { |
| 5747 | ErrorFound = NotAnAssignmentOp; |
| 5748 | NoteLoc = ErrorLoc = SecondBinOp |
| 5749 | ? SecondBinOp->getOperatorLoc() |
| 5750 | : Second->getLocStart(); |
| 5751 | NoteRange = ErrorRange = |
| 5752 | SecondBinOp ? SecondBinOp->getSourceRange() |
| 5753 | : SourceRange(ErrorLoc, ErrorLoc); |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5754 | } else { |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5755 | auto *PossibleXRHSInFirst = |
| 5756 | FirstBinOp->getRHS()->IgnoreParenImpCasts(); |
| 5757 | auto *PossibleXLHSInSecond = |
| 5758 | SecondBinOp->getLHS()->IgnoreParenImpCasts(); |
| 5759 | llvm::FoldingSetNodeID X1Id, X2Id; |
| 5760 | PossibleXRHSInFirst->Profile(X1Id, Context, |
| 5761 | /*Canonical=*/true); |
| 5762 | PossibleXLHSInSecond->Profile(X2Id, Context, |
| 5763 | /*Canonical=*/true); |
| 5764 | IsUpdateExprFound = X1Id == X2Id; |
| 5765 | if (IsUpdateExprFound) { |
| 5766 | V = FirstBinOp->getLHS(); |
| 5767 | X = SecondBinOp->getLHS(); |
| 5768 | E = SecondBinOp->getRHS(); |
| 5769 | UE = nullptr; |
| 5770 | IsXLHSInRHSPart = false; |
| 5771 | IsPostfixUpdate = true; |
| 5772 | } else { |
| 5773 | ErrorFound = NotASpecificExpression; |
| 5774 | ErrorLoc = FirstBinOp->getExprLoc(); |
| 5775 | ErrorRange = FirstBinOp->getSourceRange(); |
| 5776 | NoteLoc = SecondBinOp->getLHS()->getExprLoc(); |
| 5777 | NoteRange = SecondBinOp->getRHS()->getSourceRange(); |
| 5778 | } |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5779 | } |
| 5780 | } |
| 5781 | } |
| 5782 | } |
| 5783 | } else { |
| 5784 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 5785 | NoteRange = ErrorRange = |
| 5786 | SourceRange(Body->getLocStart(), Body->getLocStart()); |
| 5787 | ErrorFound = NotTwoSubstatements; |
| 5788 | } |
| 5789 | } else { |
| 5790 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 5791 | NoteRange = ErrorRange = |
| 5792 | SourceRange(Body->getLocStart(), Body->getLocStart()); |
| 5793 | ErrorFound = NotACompoundStatement; |
| 5794 | } |
| 5795 | if (ErrorFound != NoError) { |
| 5796 | Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) |
| 5797 | << ErrorRange; |
| 5798 | Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; |
| 5799 | return StmtError(); |
| 5800 | } else if (CurContext->isDependentContext()) { |
| 5801 | UE = V = E = X = nullptr; |
| 5802 | } |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 5803 | } |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5804 | } |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5805 | |
| 5806 | getCurFunction()->setHasBranchProtectedScope(); |
| 5807 | |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5808 | return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5809 | X, V, E, UE, IsXLHSInRHSPart, |
| 5810 | IsPostfixUpdate); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5811 | } |
| 5812 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5813 | StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, |
| 5814 | Stmt *AStmt, |
| 5815 | SourceLocation StartLoc, |
| 5816 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 5817 | if (!AStmt) |
| 5818 | return StmtError(); |
| 5819 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 5820 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5821 | // 1.2.2 OpenMP Language Terminology |
| 5822 | // Structured block - An executable statement with a single entry at the |
| 5823 | // top and a single exit at the bottom. |
| 5824 | // The point of exit cannot be a branch out of the structured block. |
| 5825 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5826 | CS->getCapturedDecl()->setNothrow(); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5827 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5828 | // OpenMP [2.16, Nesting of Regions] |
| 5829 | // If specified, a teams construct must be contained within a target |
| 5830 | // construct. That target construct must contain no statements or directives |
| 5831 | // outside of the teams construct. |
| 5832 | if (DSAStack->hasInnerTeamsRegion()) { |
| 5833 | auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true); |
| 5834 | bool OMPTeamsFound = true; |
| 5835 | if (auto *CS = dyn_cast<CompoundStmt>(S)) { |
| 5836 | auto I = CS->body_begin(); |
| 5837 | while (I != CS->body_end()) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5838 | auto *OED = dyn_cast<OMPExecutableDirective>(*I); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5839 | if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { |
| 5840 | OMPTeamsFound = false; |
| 5841 | break; |
| 5842 | } |
| 5843 | ++I; |
| 5844 | } |
| 5845 | assert(I != CS->body_end() && "Not found statement"); |
| 5846 | S = *I; |
Kelvin Li | 3834dce | 2016-06-27 19:15:43 +0000 | [diff] [blame] | 5847 | } else { |
| 5848 | auto *OED = dyn_cast<OMPExecutableDirective>(S); |
| 5849 | OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5850 | } |
| 5851 | if (!OMPTeamsFound) { |
| 5852 | Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); |
| 5853 | Diag(DSAStack->getInnerTeamsRegionLoc(), |
| 5854 | diag::note_omp_nested_teams_construct_here); |
| 5855 | Diag(S->getLocStart(), diag::note_omp_nested_statement_here) |
| 5856 | << isa<OMPExecutableDirective>(S); |
| 5857 | return StmtError(); |
| 5858 | } |
| 5859 | } |
| 5860 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5861 | getCurFunction()->setHasBranchProtectedScope(); |
| 5862 | |
| 5863 | return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 5864 | } |
| 5865 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 5866 | StmtResult |
| 5867 | Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, |
| 5868 | Stmt *AStmt, SourceLocation StartLoc, |
| 5869 | SourceLocation EndLoc) { |
| 5870 | if (!AStmt) |
| 5871 | return StmtError(); |
| 5872 | |
| 5873 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5874 | // 1.2.2 OpenMP Language Terminology |
| 5875 | // Structured block - An executable statement with a single entry at the |
| 5876 | // top and a single exit at the bottom. |
| 5877 | // The point of exit cannot be a branch out of the structured block. |
| 5878 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5879 | CS->getCapturedDecl()->setNothrow(); |
| 5880 | |
| 5881 | getCurFunction()->setHasBranchProtectedScope(); |
| 5882 | |
| 5883 | return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 5884 | AStmt); |
| 5885 | } |
| 5886 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5887 | StmtResult Sema::ActOnOpenMPTargetParallelForDirective( |
| 5888 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 5889 | SourceLocation EndLoc, |
| 5890 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 5891 | if (!AStmt) |
| 5892 | return StmtError(); |
| 5893 | |
| 5894 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5895 | // 1.2.2 OpenMP Language Terminology |
| 5896 | // Structured block - An executable statement with a single entry at the |
| 5897 | // top and a single exit at the bottom. |
| 5898 | // The point of exit cannot be a branch out of the structured block. |
| 5899 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5900 | CS->getCapturedDecl()->setNothrow(); |
| 5901 | |
| 5902 | OMPLoopDirective::HelperExprs B; |
| 5903 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 5904 | // define the nested loops number. |
| 5905 | unsigned NestedLoopCount = |
| 5906 | CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), |
| 5907 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 5908 | VarsWithImplicitDSA, B); |
| 5909 | if (NestedLoopCount == 0) |
| 5910 | return StmtError(); |
| 5911 | |
| 5912 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 5913 | "omp target parallel for loop exprs were not built"); |
| 5914 | |
| 5915 | if (!CurContext->isDependentContext()) { |
| 5916 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 5917 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5918 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5919 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 5920 | B.NumIterations, *this, CurScope, |
| 5921 | DSAStack)) |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5922 | return StmtError(); |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | getCurFunction()->setHasBranchProtectedScope(); |
| 5927 | return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, |
| 5928 | NestedLoopCount, Clauses, AStmt, |
| 5929 | B, DSAStack->isCancelRegion()); |
| 5930 | } |
| 5931 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5932 | /// \brief Check for existence of a map clause in the list of clauses. |
| 5933 | static bool HasMapClause(ArrayRef<OMPClause *> Clauses) { |
| 5934 | for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); |
| 5935 | I != E; ++I) { |
| 5936 | if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) { |
| 5937 | return true; |
| 5938 | } |
| 5939 | } |
| 5940 | |
| 5941 | return false; |
| 5942 | } |
| 5943 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5944 | StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, |
| 5945 | Stmt *AStmt, |
| 5946 | SourceLocation StartLoc, |
| 5947 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 5948 | if (!AStmt) |
| 5949 | return StmtError(); |
| 5950 | |
| 5951 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 5952 | |
Arpith Chacko Jacob | 46a04bb | 2016-01-21 19:57:55 +0000 | [diff] [blame] | 5953 | // OpenMP [2.10.1, Restrictions, p. 97] |
| 5954 | // At least one map clause must appear on the directive. |
| 5955 | if (!HasMapClause(Clauses)) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5956 | Diag(StartLoc, diag::err_omp_no_map_for_directive) |
| 5957 | << getOpenMPDirectiveName(OMPD_target_data); |
Arpith Chacko Jacob | 46a04bb | 2016-01-21 19:57:55 +0000 | [diff] [blame] | 5958 | return StmtError(); |
| 5959 | } |
| 5960 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5961 | getCurFunction()->setHasBranchProtectedScope(); |
| 5962 | |
| 5963 | return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 5964 | AStmt); |
| 5965 | } |
| 5966 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5967 | StmtResult |
| 5968 | Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, |
| 5969 | SourceLocation StartLoc, |
| 5970 | SourceLocation EndLoc) { |
| 5971 | // OpenMP [2.10.2, Restrictions, p. 99] |
| 5972 | // At least one map clause must appear on the directive. |
| 5973 | if (!HasMapClause(Clauses)) { |
| 5974 | Diag(StartLoc, diag::err_omp_no_map_for_directive) |
| 5975 | << getOpenMPDirectiveName(OMPD_target_enter_data); |
| 5976 | return StmtError(); |
| 5977 | } |
| 5978 | |
| 5979 | return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, |
| 5980 | Clauses); |
| 5981 | } |
| 5982 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 5983 | StmtResult |
| 5984 | Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, |
| 5985 | SourceLocation StartLoc, |
| 5986 | SourceLocation EndLoc) { |
| 5987 | // OpenMP [2.10.3, Restrictions, p. 102] |
| 5988 | // At least one map clause must appear on the directive. |
| 5989 | if (!HasMapClause(Clauses)) { |
| 5990 | Diag(StartLoc, diag::err_omp_no_map_for_directive) |
| 5991 | << getOpenMPDirectiveName(OMPD_target_exit_data); |
| 5992 | return StmtError(); |
| 5993 | } |
| 5994 | |
| 5995 | return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses); |
| 5996 | } |
| 5997 | |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5998 | StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, |
| 5999 | SourceLocation StartLoc, |
| 6000 | SourceLocation EndLoc) { |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 6001 | bool seenMotionClause = false; |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 6002 | for (auto *C : Clauses) { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 6003 | if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from) |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 6004 | seenMotionClause = true; |
| 6005 | } |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 6006 | if (!seenMotionClause) { |
| 6007 | Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); |
| 6008 | return StmtError(); |
| 6009 | } |
| 6010 | return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses); |
| 6011 | } |
| 6012 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 6013 | StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, |
| 6014 | Stmt *AStmt, SourceLocation StartLoc, |
| 6015 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 6016 | if (!AStmt) |
| 6017 | return StmtError(); |
| 6018 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 6019 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6020 | // 1.2.2 OpenMP Language Terminology |
| 6021 | // Structured block - An executable statement with a single entry at the |
| 6022 | // top and a single exit at the bottom. |
| 6023 | // The point of exit cannot be a branch out of the structured block. |
| 6024 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6025 | CS->getCapturedDecl()->setNothrow(); |
| 6026 | |
| 6027 | getCurFunction()->setHasBranchProtectedScope(); |
| 6028 | |
| 6029 | return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 6030 | } |
| 6031 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 6032 | StmtResult |
| 6033 | Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, |
| 6034 | SourceLocation EndLoc, |
| 6035 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 6036 | if (DSAStack->isParentNowaitRegion()) { |
| 6037 | Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; |
| 6038 | return StmtError(); |
| 6039 | } |
| 6040 | if (DSAStack->isParentOrderedRegion()) { |
| 6041 | Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; |
| 6042 | return StmtError(); |
| 6043 | } |
| 6044 | return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, |
| 6045 | CancelRegion); |
| 6046 | } |
| 6047 | |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6048 | StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, |
| 6049 | SourceLocation StartLoc, |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 6050 | SourceLocation EndLoc, |
| 6051 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 6052 | if (DSAStack->isParentNowaitRegion()) { |
| 6053 | Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; |
| 6054 | return StmtError(); |
| 6055 | } |
| 6056 | if (DSAStack->isParentOrderedRegion()) { |
| 6057 | Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; |
| 6058 | return StmtError(); |
| 6059 | } |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6060 | DSAStack->setParentCancelRegion(/*Cancel=*/true); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6061 | return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 6062 | CancelRegion); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 6063 | } |
| 6064 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 6065 | static bool checkGrainsizeNumTasksClauses(Sema &S, |
| 6066 | ArrayRef<OMPClause *> Clauses) { |
| 6067 | OMPClause *PrevClause = nullptr; |
| 6068 | bool ErrorFound = false; |
| 6069 | for (auto *C : Clauses) { |
| 6070 | if (C->getClauseKind() == OMPC_grainsize || |
| 6071 | C->getClauseKind() == OMPC_num_tasks) { |
| 6072 | if (!PrevClause) |
| 6073 | PrevClause = C; |
| 6074 | else if (PrevClause->getClauseKind() != C->getClauseKind()) { |
| 6075 | S.Diag(C->getLocStart(), |
| 6076 | diag::err_omp_grainsize_num_tasks_mutually_exclusive) |
| 6077 | << getOpenMPClauseName(C->getClauseKind()) |
| 6078 | << getOpenMPClauseName(PrevClause->getClauseKind()); |
| 6079 | S.Diag(PrevClause->getLocStart(), |
| 6080 | diag::note_omp_previous_grainsize_num_tasks) |
| 6081 | << getOpenMPClauseName(PrevClause->getClauseKind()); |
| 6082 | ErrorFound = true; |
| 6083 | } |
| 6084 | } |
| 6085 | } |
| 6086 | return ErrorFound; |
| 6087 | } |
| 6088 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 6089 | StmtResult Sema::ActOnOpenMPTaskLoopDirective( |
| 6090 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6091 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 6092 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 6093 | if (!AStmt) |
| 6094 | return StmtError(); |
| 6095 | |
| 6096 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 6097 | OMPLoopDirective::HelperExprs B; |
| 6098 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 6099 | // define the nested loops number. |
| 6100 | unsigned NestedLoopCount = |
| 6101 | CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 6102 | /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 6103 | VarsWithImplicitDSA, B); |
| 6104 | if (NestedLoopCount == 0) |
| 6105 | return StmtError(); |
| 6106 | |
| 6107 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6108 | "omp for loop exprs were not built"); |
| 6109 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 6110 | // OpenMP, [2.9.2 taskloop Construct, Restrictions] |
| 6111 | // The grainsize clause and num_tasks clause are mutually exclusive and may |
| 6112 | // not appear on the same taskloop directive. |
| 6113 | if (checkGrainsizeNumTasksClauses(*this, Clauses)) |
| 6114 | return StmtError(); |
| 6115 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 6116 | getCurFunction()->setHasBranchProtectedScope(); |
| 6117 | return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, |
| 6118 | NestedLoopCount, Clauses, AStmt, B); |
| 6119 | } |
| 6120 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 6121 | StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( |
| 6122 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6123 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 6124 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 6125 | if (!AStmt) |
| 6126 | return StmtError(); |
| 6127 | |
| 6128 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 6129 | OMPLoopDirective::HelperExprs B; |
| 6130 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 6131 | // define the nested loops number. |
| 6132 | unsigned NestedLoopCount = |
| 6133 | CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), |
| 6134 | /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, |
| 6135 | VarsWithImplicitDSA, B); |
| 6136 | if (NestedLoopCount == 0) |
| 6137 | return StmtError(); |
| 6138 | |
| 6139 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6140 | "omp for loop exprs were not built"); |
| 6141 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 6142 | if (!CurContext->isDependentContext()) { |
| 6143 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6144 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6145 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 6146 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 6147 | B.NumIterations, *this, CurScope, |
| 6148 | DSAStack)) |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 6149 | return StmtError(); |
| 6150 | } |
| 6151 | } |
| 6152 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 6153 | // OpenMP, [2.9.2 taskloop Construct, Restrictions] |
| 6154 | // The grainsize clause and num_tasks clause are mutually exclusive and may |
| 6155 | // not appear on the same taskloop directive. |
| 6156 | if (checkGrainsizeNumTasksClauses(*this, Clauses)) |
| 6157 | return StmtError(); |
| 6158 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 6159 | getCurFunction()->setHasBranchProtectedScope(); |
| 6160 | return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, |
| 6161 | NestedLoopCount, Clauses, AStmt, B); |
| 6162 | } |
| 6163 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 6164 | StmtResult Sema::ActOnOpenMPDistributeDirective( |
| 6165 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6166 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 6167 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 6168 | if (!AStmt) |
| 6169 | return StmtError(); |
| 6170 | |
| 6171 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 6172 | OMPLoopDirective::HelperExprs B; |
| 6173 | // In presence of clause 'collapse' with number of loops, it will |
| 6174 | // define the nested loops number. |
| 6175 | unsigned NestedLoopCount = |
| 6176 | CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), |
| 6177 | nullptr /*ordered not a clause on distribute*/, AStmt, |
| 6178 | *this, *DSAStack, VarsWithImplicitDSA, B); |
| 6179 | if (NestedLoopCount == 0) |
| 6180 | return StmtError(); |
| 6181 | |
| 6182 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6183 | "omp for loop exprs were not built"); |
| 6184 | |
| 6185 | getCurFunction()->setHasBranchProtectedScope(); |
| 6186 | return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, |
| 6187 | NestedLoopCount, Clauses, AStmt, B); |
| 6188 | } |
| 6189 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 6190 | StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( |
| 6191 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6192 | SourceLocation EndLoc, |
| 6193 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6194 | if (!AStmt) |
| 6195 | return StmtError(); |
| 6196 | |
| 6197 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6198 | // 1.2.2 OpenMP Language Terminology |
| 6199 | // Structured block - An executable statement with a single entry at the |
| 6200 | // top and a single exit at the bottom. |
| 6201 | // The point of exit cannot be a branch out of the structured block. |
| 6202 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6203 | CS->getCapturedDecl()->setNothrow(); |
| 6204 | |
| 6205 | OMPLoopDirective::HelperExprs B; |
| 6206 | // In presence of clause 'collapse' with number of loops, it will |
| 6207 | // define the nested loops number. |
| 6208 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 6209 | OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), |
| 6210 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6211 | VarsWithImplicitDSA, B); |
| 6212 | if (NestedLoopCount == 0) |
| 6213 | return StmtError(); |
| 6214 | |
| 6215 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6216 | "omp for loop exprs were not built"); |
| 6217 | |
| 6218 | getCurFunction()->setHasBranchProtectedScope(); |
| 6219 | return OMPDistributeParallelForDirective::Create( |
| 6220 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6221 | } |
| 6222 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 6223 | StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( |
| 6224 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6225 | SourceLocation EndLoc, |
| 6226 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6227 | if (!AStmt) |
| 6228 | return StmtError(); |
| 6229 | |
| 6230 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6231 | // 1.2.2 OpenMP Language Terminology |
| 6232 | // Structured block - An executable statement with a single entry at the |
| 6233 | // top and a single exit at the bottom. |
| 6234 | // The point of exit cannot be a branch out of the structured block. |
| 6235 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6236 | CS->getCapturedDecl()->setNothrow(); |
| 6237 | |
| 6238 | OMPLoopDirective::HelperExprs B; |
| 6239 | // In presence of clause 'collapse' with number of loops, it will |
| 6240 | // define the nested loops number. |
| 6241 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 6242 | OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), |
| 6243 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6244 | VarsWithImplicitDSA, B); |
| 6245 | if (NestedLoopCount == 0) |
| 6246 | return StmtError(); |
| 6247 | |
| 6248 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6249 | "omp for loop exprs were not built"); |
| 6250 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 6251 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 6252 | return StmtError(); |
| 6253 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 6254 | getCurFunction()->setHasBranchProtectedScope(); |
| 6255 | return OMPDistributeParallelForSimdDirective::Create( |
| 6256 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6257 | } |
| 6258 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 6259 | StmtResult Sema::ActOnOpenMPDistributeSimdDirective( |
| 6260 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6261 | SourceLocation EndLoc, |
| 6262 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6263 | if (!AStmt) |
| 6264 | return StmtError(); |
| 6265 | |
| 6266 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6267 | // 1.2.2 OpenMP Language Terminology |
| 6268 | // Structured block - An executable statement with a single entry at the |
| 6269 | // top and a single exit at the bottom. |
| 6270 | // The point of exit cannot be a branch out of the structured block. |
| 6271 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6272 | CS->getCapturedDecl()->setNothrow(); |
| 6273 | |
| 6274 | OMPLoopDirective::HelperExprs B; |
| 6275 | // In presence of clause 'collapse' with number of loops, it will |
| 6276 | // define the nested loops number. |
| 6277 | unsigned NestedLoopCount = |
| 6278 | CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), |
| 6279 | nullptr /*ordered not a clause on distribute*/, AStmt, |
| 6280 | *this, *DSAStack, VarsWithImplicitDSA, B); |
| 6281 | if (NestedLoopCount == 0) |
| 6282 | return StmtError(); |
| 6283 | |
| 6284 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6285 | "omp for loop exprs were not built"); |
| 6286 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 6287 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 6288 | return StmtError(); |
| 6289 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 6290 | getCurFunction()->setHasBranchProtectedScope(); |
| 6291 | return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, |
| 6292 | NestedLoopCount, Clauses, AStmt, B); |
| 6293 | } |
| 6294 | |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 6295 | StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( |
| 6296 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6297 | SourceLocation EndLoc, |
| 6298 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6299 | if (!AStmt) |
| 6300 | return StmtError(); |
| 6301 | |
| 6302 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6303 | // 1.2.2 OpenMP Language Terminology |
| 6304 | // Structured block - An executable statement with a single entry at the |
| 6305 | // top and a single exit at the bottom. |
| 6306 | // The point of exit cannot be a branch out of the structured block. |
| 6307 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6308 | CS->getCapturedDecl()->setNothrow(); |
| 6309 | |
| 6310 | OMPLoopDirective::HelperExprs B; |
| 6311 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 6312 | // define the nested loops number. |
| 6313 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 6314 | OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), |
| 6315 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 6316 | VarsWithImplicitDSA, B); |
| 6317 | if (NestedLoopCount == 0) |
| 6318 | return StmtError(); |
| 6319 | |
| 6320 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6321 | "omp target parallel for simd loop exprs were not built"); |
| 6322 | |
| 6323 | if (!CurContext->isDependentContext()) { |
| 6324 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6325 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6326 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 6327 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6328 | B.NumIterations, *this, CurScope, |
| 6329 | DSAStack)) |
| 6330 | return StmtError(); |
| 6331 | } |
| 6332 | } |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 6333 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 6334 | return StmtError(); |
| 6335 | |
| 6336 | getCurFunction()->setHasBranchProtectedScope(); |
| 6337 | return OMPTargetParallelForSimdDirective::Create( |
| 6338 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6339 | } |
| 6340 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 6341 | StmtResult Sema::ActOnOpenMPTargetSimdDirective( |
| 6342 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6343 | SourceLocation EndLoc, |
| 6344 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6345 | if (!AStmt) |
| 6346 | return StmtError(); |
| 6347 | |
| 6348 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6349 | // 1.2.2 OpenMP Language Terminology |
| 6350 | // Structured block - An executable statement with a single entry at the |
| 6351 | // top and a single exit at the bottom. |
| 6352 | // The point of exit cannot be a branch out of the structured block. |
| 6353 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6354 | CS->getCapturedDecl()->setNothrow(); |
| 6355 | |
| 6356 | OMPLoopDirective::HelperExprs B; |
| 6357 | // In presence of clause 'collapse' with number of loops, it will define the |
| 6358 | // nested loops number. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6359 | unsigned NestedLoopCount = |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 6360 | CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), |
| 6361 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 6362 | VarsWithImplicitDSA, B); |
| 6363 | if (NestedLoopCount == 0) |
| 6364 | return StmtError(); |
| 6365 | |
| 6366 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6367 | "omp target simd loop exprs were not built"); |
| 6368 | |
| 6369 | if (!CurContext->isDependentContext()) { |
| 6370 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6371 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6372 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 6373 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6374 | B.NumIterations, *this, CurScope, |
| 6375 | DSAStack)) |
| 6376 | return StmtError(); |
| 6377 | } |
| 6378 | } |
| 6379 | |
| 6380 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 6381 | return StmtError(); |
| 6382 | |
| 6383 | getCurFunction()->setHasBranchProtectedScope(); |
| 6384 | return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, |
| 6385 | NestedLoopCount, Clauses, AStmt, B); |
| 6386 | } |
| 6387 | |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 6388 | StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( |
| 6389 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6390 | SourceLocation EndLoc, |
| 6391 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6392 | if (!AStmt) |
| 6393 | return StmtError(); |
| 6394 | |
| 6395 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6396 | // 1.2.2 OpenMP Language Terminology |
| 6397 | // Structured block - An executable statement with a single entry at the |
| 6398 | // top and a single exit at the bottom. |
| 6399 | // The point of exit cannot be a branch out of the structured block. |
| 6400 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6401 | CS->getCapturedDecl()->setNothrow(); |
| 6402 | |
| 6403 | OMPLoopDirective::HelperExprs B; |
| 6404 | // In presence of clause 'collapse' with number of loops, it will |
| 6405 | // define the nested loops number. |
| 6406 | unsigned NestedLoopCount = |
| 6407 | CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), |
| 6408 | nullptr /*ordered not a clause on distribute*/, AStmt, |
| 6409 | *this, *DSAStack, VarsWithImplicitDSA, B); |
| 6410 | if (NestedLoopCount == 0) |
| 6411 | return StmtError(); |
| 6412 | |
| 6413 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6414 | "omp teams distribute loop exprs were not built"); |
| 6415 | |
| 6416 | getCurFunction()->setHasBranchProtectedScope(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6417 | return OMPTeamsDistributeDirective::Create( |
| 6418 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 6421 | StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( |
| 6422 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6423 | SourceLocation EndLoc, |
| 6424 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6425 | if (!AStmt) |
| 6426 | return StmtError(); |
| 6427 | |
| 6428 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6429 | // 1.2.2 OpenMP Language Terminology |
| 6430 | // Structured block - An executable statement with a single entry at the |
| 6431 | // top and a single exit at the bottom. |
| 6432 | // The point of exit cannot be a branch out of the structured block. |
| 6433 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6434 | CS->getCapturedDecl()->setNothrow(); |
| 6435 | |
| 6436 | OMPLoopDirective::HelperExprs B; |
| 6437 | // In presence of clause 'collapse' with number of loops, it will |
| 6438 | // define the nested loops number. |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 6439 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 6440 | OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), |
| 6441 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6442 | VarsWithImplicitDSA, B); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 6443 | |
| 6444 | if (NestedLoopCount == 0) |
| 6445 | return StmtError(); |
| 6446 | |
| 6447 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6448 | "omp teams distribute simd loop exprs were not built"); |
| 6449 | |
| 6450 | if (!CurContext->isDependentContext()) { |
| 6451 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6452 | for (auto C : Clauses) { |
| 6453 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6454 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6455 | B.NumIterations, *this, CurScope, |
| 6456 | DSAStack)) |
| 6457 | return StmtError(); |
| 6458 | } |
| 6459 | } |
| 6460 | |
| 6461 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 6462 | return StmtError(); |
| 6463 | |
| 6464 | getCurFunction()->setHasBranchProtectedScope(); |
| 6465 | return OMPTeamsDistributeSimdDirective::Create( |
| 6466 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6467 | } |
| 6468 | |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 6469 | StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( |
| 6470 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6471 | SourceLocation EndLoc, |
| 6472 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6473 | if (!AStmt) |
| 6474 | return StmtError(); |
| 6475 | |
| 6476 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6477 | // 1.2.2 OpenMP Language Terminology |
| 6478 | // Structured block - An executable statement with a single entry at the |
| 6479 | // top and a single exit at the bottom. |
| 6480 | // The point of exit cannot be a branch out of the structured block. |
| 6481 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6482 | CS->getCapturedDecl()->setNothrow(); |
| 6483 | |
| 6484 | OMPLoopDirective::HelperExprs B; |
| 6485 | // In presence of clause 'collapse' with number of loops, it will |
| 6486 | // define the nested loops number. |
| 6487 | auto NestedLoopCount = CheckOpenMPLoop( |
| 6488 | OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), |
| 6489 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6490 | VarsWithImplicitDSA, B); |
| 6491 | |
| 6492 | if (NestedLoopCount == 0) |
| 6493 | return StmtError(); |
| 6494 | |
| 6495 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6496 | "omp for loop exprs were not built"); |
| 6497 | |
| 6498 | if (!CurContext->isDependentContext()) { |
| 6499 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6500 | for (auto C : Clauses) { |
| 6501 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6502 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6503 | B.NumIterations, *this, CurScope, |
| 6504 | DSAStack)) |
| 6505 | return StmtError(); |
| 6506 | } |
| 6507 | } |
| 6508 | |
| 6509 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 6510 | return StmtError(); |
| 6511 | |
| 6512 | getCurFunction()->setHasBranchProtectedScope(); |
| 6513 | return OMPTeamsDistributeParallelForSimdDirective::Create( |
| 6514 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6515 | } |
| 6516 | |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 6517 | StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( |
| 6518 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6519 | SourceLocation EndLoc, |
| 6520 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6521 | if (!AStmt) |
| 6522 | return StmtError(); |
| 6523 | |
| 6524 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6525 | // 1.2.2 OpenMP Language Terminology |
| 6526 | // Structured block - An executable statement with a single entry at the |
| 6527 | // top and a single exit at the bottom. |
| 6528 | // The point of exit cannot be a branch out of the structured block. |
| 6529 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6530 | CS->getCapturedDecl()->setNothrow(); |
| 6531 | |
| 6532 | OMPLoopDirective::HelperExprs B; |
| 6533 | // In presence of clause 'collapse' with number of loops, it will |
| 6534 | // define the nested loops number. |
| 6535 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 6536 | OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), |
| 6537 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6538 | VarsWithImplicitDSA, B); |
| 6539 | |
| 6540 | if (NestedLoopCount == 0) |
| 6541 | return StmtError(); |
| 6542 | |
| 6543 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6544 | "omp for loop exprs were not built"); |
| 6545 | |
| 6546 | if (!CurContext->isDependentContext()) { |
| 6547 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6548 | for (auto C : Clauses) { |
| 6549 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6550 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6551 | B.NumIterations, *this, CurScope, |
| 6552 | DSAStack)) |
| 6553 | return StmtError(); |
| 6554 | } |
| 6555 | } |
| 6556 | |
| 6557 | getCurFunction()->setHasBranchProtectedScope(); |
| 6558 | return OMPTeamsDistributeParallelForDirective::Create( |
| 6559 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6560 | } |
| 6561 | |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 6562 | StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, |
| 6563 | Stmt *AStmt, |
| 6564 | SourceLocation StartLoc, |
| 6565 | SourceLocation EndLoc) { |
| 6566 | if (!AStmt) |
| 6567 | return StmtError(); |
| 6568 | |
| 6569 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6570 | // 1.2.2 OpenMP Language Terminology |
| 6571 | // Structured block - An executable statement with a single entry at the |
| 6572 | // top and a single exit at the bottom. |
| 6573 | // The point of exit cannot be a branch out of the structured block. |
| 6574 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6575 | CS->getCapturedDecl()->setNothrow(); |
| 6576 | |
| 6577 | getCurFunction()->setHasBranchProtectedScope(); |
| 6578 | |
| 6579 | return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 6580 | AStmt); |
| 6581 | } |
| 6582 | |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 6583 | StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( |
| 6584 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6585 | SourceLocation EndLoc, |
| 6586 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6587 | if (!AStmt) |
| 6588 | return StmtError(); |
| 6589 | |
| 6590 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6591 | // 1.2.2 OpenMP Language Terminology |
| 6592 | // Structured block - An executable statement with a single entry at the |
| 6593 | // top and a single exit at the bottom. |
| 6594 | // The point of exit cannot be a branch out of the structured block. |
| 6595 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6596 | CS->getCapturedDecl()->setNothrow(); |
| 6597 | |
| 6598 | OMPLoopDirective::HelperExprs B; |
| 6599 | // In presence of clause 'collapse' with number of loops, it will |
| 6600 | // define the nested loops number. |
| 6601 | auto NestedLoopCount = CheckOpenMPLoop( |
| 6602 | OMPD_target_teams_distribute, |
| 6603 | getCollapseNumberExpr(Clauses), |
| 6604 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6605 | VarsWithImplicitDSA, B); |
| 6606 | if (NestedLoopCount == 0) |
| 6607 | return StmtError(); |
| 6608 | |
| 6609 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6610 | "omp target teams distribute loop exprs were not built"); |
| 6611 | |
| 6612 | getCurFunction()->setHasBranchProtectedScope(); |
| 6613 | return OMPTargetTeamsDistributeDirective::Create( |
| 6614 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6615 | } |
| 6616 | |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 6617 | StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( |
| 6618 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6619 | SourceLocation EndLoc, |
| 6620 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6621 | if (!AStmt) |
| 6622 | return StmtError(); |
| 6623 | |
| 6624 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6625 | // 1.2.2 OpenMP Language Terminology |
| 6626 | // Structured block - An executable statement with a single entry at the |
| 6627 | // top and a single exit at the bottom. |
| 6628 | // The point of exit cannot be a branch out of the structured block. |
| 6629 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6630 | CS->getCapturedDecl()->setNothrow(); |
| 6631 | |
| 6632 | OMPLoopDirective::HelperExprs B; |
| 6633 | // In presence of clause 'collapse' with number of loops, it will |
| 6634 | // define the nested loops number. |
| 6635 | auto NestedLoopCount = CheckOpenMPLoop( |
| 6636 | OMPD_target_teams_distribute_parallel_for, |
| 6637 | getCollapseNumberExpr(Clauses), |
| 6638 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6639 | VarsWithImplicitDSA, B); |
| 6640 | if (NestedLoopCount == 0) |
| 6641 | return StmtError(); |
| 6642 | |
| 6643 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6644 | "omp target teams distribute parallel for loop exprs were not built"); |
| 6645 | |
| 6646 | if (!CurContext->isDependentContext()) { |
| 6647 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6648 | for (auto C : Clauses) { |
| 6649 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6650 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6651 | B.NumIterations, *this, CurScope, |
| 6652 | DSAStack)) |
| 6653 | return StmtError(); |
| 6654 | } |
| 6655 | } |
| 6656 | |
| 6657 | getCurFunction()->setHasBranchProtectedScope(); |
| 6658 | return OMPTargetTeamsDistributeParallelForDirective::Create( |
| 6659 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6660 | } |
| 6661 | |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 6662 | StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( |
| 6663 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6664 | SourceLocation EndLoc, |
| 6665 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6666 | if (!AStmt) |
| 6667 | return StmtError(); |
| 6668 | |
| 6669 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6670 | // 1.2.2 OpenMP Language Terminology |
| 6671 | // Structured block - An executable statement with a single entry at the |
| 6672 | // top and a single exit at the bottom. |
| 6673 | // The point of exit cannot be a branch out of the structured block. |
| 6674 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6675 | CS->getCapturedDecl()->setNothrow(); |
| 6676 | |
| 6677 | OMPLoopDirective::HelperExprs B; |
| 6678 | // In presence of clause 'collapse' with number of loops, it will |
| 6679 | // define the nested loops number. |
| 6680 | auto NestedLoopCount = CheckOpenMPLoop( |
| 6681 | OMPD_target_teams_distribute_parallel_for_simd, |
| 6682 | getCollapseNumberExpr(Clauses), |
| 6683 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6684 | VarsWithImplicitDSA, B); |
| 6685 | if (NestedLoopCount == 0) |
| 6686 | return StmtError(); |
| 6687 | |
| 6688 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6689 | "omp target teams distribute parallel for simd loop exprs were not " |
| 6690 | "built"); |
| 6691 | |
| 6692 | if (!CurContext->isDependentContext()) { |
| 6693 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6694 | for (auto C : Clauses) { |
| 6695 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6696 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6697 | B.NumIterations, *this, CurScope, |
| 6698 | DSAStack)) |
| 6699 | return StmtError(); |
| 6700 | } |
| 6701 | } |
| 6702 | |
| 6703 | getCurFunction()->setHasBranchProtectedScope(); |
| 6704 | return OMPTargetTeamsDistributeParallelForSimdDirective::Create( |
| 6705 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6706 | } |
| 6707 | |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 6708 | StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( |
| 6709 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6710 | SourceLocation EndLoc, |
| 6711 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6712 | if (!AStmt) |
| 6713 | return StmtError(); |
| 6714 | |
| 6715 | auto *CS = cast<CapturedStmt>(AStmt); |
| 6716 | // 1.2.2 OpenMP Language Terminology |
| 6717 | // Structured block - An executable statement with a single entry at the |
| 6718 | // top and a single exit at the bottom. |
| 6719 | // The point of exit cannot be a branch out of the structured block. |
| 6720 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6721 | CS->getCapturedDecl()->setNothrow(); |
| 6722 | |
| 6723 | OMPLoopDirective::HelperExprs B; |
| 6724 | // In presence of clause 'collapse' with number of loops, it will |
| 6725 | // define the nested loops number. |
| 6726 | auto NestedLoopCount = CheckOpenMPLoop( |
| 6727 | OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), |
| 6728 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6729 | VarsWithImplicitDSA, B); |
| 6730 | if (NestedLoopCount == 0) |
| 6731 | return StmtError(); |
| 6732 | |
| 6733 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6734 | "omp target teams distribute simd loop exprs were not built"); |
| 6735 | |
| 6736 | getCurFunction()->setHasBranchProtectedScope(); |
| 6737 | return OMPTargetTeamsDistributeSimdDirective::Create( |
| 6738 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6739 | } |
| 6740 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6741 | OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6742 | SourceLocation StartLoc, |
| 6743 | SourceLocation LParenLoc, |
| 6744 | SourceLocation EndLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 6745 | OMPClause *Res = nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6746 | switch (Kind) { |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 6747 | case OMPC_final: |
| 6748 | Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6749 | break; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6750 | case OMPC_num_threads: |
| 6751 | Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6752 | break; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6753 | case OMPC_safelen: |
| 6754 | Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6755 | break; |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 6756 | case OMPC_simdlen: |
| 6757 | Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6758 | break; |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 6759 | case OMPC_collapse: |
| 6760 | Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6761 | break; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 6762 | case OMPC_ordered: |
| 6763 | Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); |
| 6764 | break; |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 6765 | case OMPC_device: |
| 6766 | Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6767 | break; |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 6768 | case OMPC_num_teams: |
| 6769 | Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6770 | break; |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6771 | case OMPC_thread_limit: |
| 6772 | Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6773 | break; |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 6774 | case OMPC_priority: |
| 6775 | Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6776 | break; |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 6777 | case OMPC_grainsize: |
| 6778 | Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6779 | break; |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 6780 | case OMPC_num_tasks: |
| 6781 | Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6782 | break; |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 6783 | case OMPC_hint: |
| 6784 | Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6785 | break; |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 6786 | case OMPC_if: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6787 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 6788 | case OMPC_proc_bind: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6789 | case OMPC_schedule: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6790 | case OMPC_private: |
| 6791 | case OMPC_firstprivate: |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 6792 | case OMPC_lastprivate: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6793 | case OMPC_shared: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 6794 | case OMPC_reduction: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 6795 | case OMPC_linear: |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 6796 | case OMPC_aligned: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6797 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6798 | case OMPC_copyprivate: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 6799 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 6800 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 6801 | case OMPC_mergeable: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6802 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 6803 | case OMPC_flush: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 6804 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 6805 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 6806 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 6807 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 6808 | case OMPC_seq_cst: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 6809 | case OMPC_depend: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 6810 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 6811 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 6812 | case OMPC_map: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 6813 | case OMPC_nogroup: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 6814 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 6815 | case OMPC_defaultmap: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6816 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 6817 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 6818 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 6819 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 6820 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 6821 | case OMPC_is_device_ptr: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6822 | llvm_unreachable("Clause is not allowed."); |
| 6823 | } |
| 6824 | return Res; |
| 6825 | } |
| 6826 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 6827 | // An OpenMP directive such as 'target parallel' has two captured regions: |
| 6828 | // for the 'target' and 'parallel' respectively. This function returns |
| 6829 | // the region in which to capture expressions associated with a clause. |
| 6830 | // A return value of OMPD_unknown signifies that the expression should not |
| 6831 | // be captured. |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6832 | static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( |
| 6833 | OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, |
| 6834 | OpenMPDirectiveKind NameModifier = OMPD_unknown) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 6835 | OpenMPDirectiveKind CaptureRegion = OMPD_unknown; |
| 6836 | |
| 6837 | switch (CKind) { |
| 6838 | case OMPC_if: |
| 6839 | switch (DKind) { |
| 6840 | case OMPD_target_parallel: |
| 6841 | // If this clause applies to the nested 'parallel' region, capture within |
| 6842 | // the 'target' region, otherwise do not capture. |
| 6843 | if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) |
| 6844 | CaptureRegion = OMPD_target; |
| 6845 | break; |
| 6846 | case OMPD_cancel: |
| 6847 | case OMPD_parallel: |
| 6848 | case OMPD_parallel_sections: |
| 6849 | case OMPD_parallel_for: |
| 6850 | case OMPD_parallel_for_simd: |
| 6851 | case OMPD_target: |
| 6852 | case OMPD_target_simd: |
| 6853 | case OMPD_target_parallel_for: |
| 6854 | case OMPD_target_parallel_for_simd: |
| 6855 | case OMPD_target_teams: |
| 6856 | case OMPD_target_teams_distribute: |
| 6857 | case OMPD_target_teams_distribute_simd: |
| 6858 | case OMPD_target_teams_distribute_parallel_for: |
| 6859 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 6860 | case OMPD_teams_distribute_parallel_for: |
| 6861 | case OMPD_teams_distribute_parallel_for_simd: |
| 6862 | case OMPD_distribute_parallel_for: |
| 6863 | case OMPD_distribute_parallel_for_simd: |
| 6864 | case OMPD_task: |
| 6865 | case OMPD_taskloop: |
| 6866 | case OMPD_taskloop_simd: |
| 6867 | case OMPD_target_data: |
| 6868 | case OMPD_target_enter_data: |
| 6869 | case OMPD_target_exit_data: |
| 6870 | case OMPD_target_update: |
| 6871 | // Do not capture if-clause expressions. |
| 6872 | break; |
| 6873 | case OMPD_threadprivate: |
| 6874 | case OMPD_taskyield: |
| 6875 | case OMPD_barrier: |
| 6876 | case OMPD_taskwait: |
| 6877 | case OMPD_cancellation_point: |
| 6878 | case OMPD_flush: |
| 6879 | case OMPD_declare_reduction: |
| 6880 | case OMPD_declare_simd: |
| 6881 | case OMPD_declare_target: |
| 6882 | case OMPD_end_declare_target: |
| 6883 | case OMPD_teams: |
| 6884 | case OMPD_simd: |
| 6885 | case OMPD_for: |
| 6886 | case OMPD_for_simd: |
| 6887 | case OMPD_sections: |
| 6888 | case OMPD_section: |
| 6889 | case OMPD_single: |
| 6890 | case OMPD_master: |
| 6891 | case OMPD_critical: |
| 6892 | case OMPD_taskgroup: |
| 6893 | case OMPD_distribute: |
| 6894 | case OMPD_ordered: |
| 6895 | case OMPD_atomic: |
| 6896 | case OMPD_distribute_simd: |
| 6897 | case OMPD_teams_distribute: |
| 6898 | case OMPD_teams_distribute_simd: |
| 6899 | llvm_unreachable("Unexpected OpenMP directive with if-clause"); |
| 6900 | case OMPD_unknown: |
| 6901 | llvm_unreachable("Unknown OpenMP directive"); |
| 6902 | } |
| 6903 | break; |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6904 | case OMPC_num_threads: |
| 6905 | switch (DKind) { |
| 6906 | case OMPD_target_parallel: |
| 6907 | CaptureRegion = OMPD_target; |
| 6908 | break; |
| 6909 | case OMPD_cancel: |
| 6910 | case OMPD_parallel: |
| 6911 | case OMPD_parallel_sections: |
| 6912 | case OMPD_parallel_for: |
| 6913 | case OMPD_parallel_for_simd: |
| 6914 | case OMPD_target: |
| 6915 | case OMPD_target_simd: |
| 6916 | case OMPD_target_parallel_for: |
| 6917 | case OMPD_target_parallel_for_simd: |
| 6918 | case OMPD_target_teams: |
| 6919 | case OMPD_target_teams_distribute: |
| 6920 | case OMPD_target_teams_distribute_simd: |
| 6921 | case OMPD_target_teams_distribute_parallel_for: |
| 6922 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 6923 | case OMPD_teams_distribute_parallel_for: |
| 6924 | case OMPD_teams_distribute_parallel_for_simd: |
| 6925 | case OMPD_distribute_parallel_for: |
| 6926 | case OMPD_distribute_parallel_for_simd: |
| 6927 | case OMPD_task: |
| 6928 | case OMPD_taskloop: |
| 6929 | case OMPD_taskloop_simd: |
| 6930 | case OMPD_target_data: |
| 6931 | case OMPD_target_enter_data: |
| 6932 | case OMPD_target_exit_data: |
| 6933 | case OMPD_target_update: |
| 6934 | // Do not capture num_threads-clause expressions. |
| 6935 | break; |
| 6936 | case OMPD_threadprivate: |
| 6937 | case OMPD_taskyield: |
| 6938 | case OMPD_barrier: |
| 6939 | case OMPD_taskwait: |
| 6940 | case OMPD_cancellation_point: |
| 6941 | case OMPD_flush: |
| 6942 | case OMPD_declare_reduction: |
| 6943 | case OMPD_declare_simd: |
| 6944 | case OMPD_declare_target: |
| 6945 | case OMPD_end_declare_target: |
| 6946 | case OMPD_teams: |
| 6947 | case OMPD_simd: |
| 6948 | case OMPD_for: |
| 6949 | case OMPD_for_simd: |
| 6950 | case OMPD_sections: |
| 6951 | case OMPD_section: |
| 6952 | case OMPD_single: |
| 6953 | case OMPD_master: |
| 6954 | case OMPD_critical: |
| 6955 | case OMPD_taskgroup: |
| 6956 | case OMPD_distribute: |
| 6957 | case OMPD_ordered: |
| 6958 | case OMPD_atomic: |
| 6959 | case OMPD_distribute_simd: |
| 6960 | case OMPD_teams_distribute: |
| 6961 | case OMPD_teams_distribute_simd: |
| 6962 | llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); |
| 6963 | case OMPD_unknown: |
| 6964 | llvm_unreachable("Unknown OpenMP directive"); |
| 6965 | } |
| 6966 | break; |
Arpith Chacko Jacob | bc12634 | 2017-01-25 11:28:18 +0000 | [diff] [blame] | 6967 | case OMPC_num_teams: |
| 6968 | switch (DKind) { |
| 6969 | case OMPD_target_teams: |
| 6970 | CaptureRegion = OMPD_target; |
| 6971 | break; |
| 6972 | case OMPD_cancel: |
| 6973 | case OMPD_parallel: |
| 6974 | case OMPD_parallel_sections: |
| 6975 | case OMPD_parallel_for: |
| 6976 | case OMPD_parallel_for_simd: |
| 6977 | case OMPD_target: |
| 6978 | case OMPD_target_simd: |
| 6979 | case OMPD_target_parallel: |
| 6980 | case OMPD_target_parallel_for: |
| 6981 | case OMPD_target_parallel_for_simd: |
| 6982 | case OMPD_target_teams_distribute: |
| 6983 | case OMPD_target_teams_distribute_simd: |
| 6984 | case OMPD_target_teams_distribute_parallel_for: |
| 6985 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 6986 | case OMPD_teams_distribute_parallel_for: |
| 6987 | case OMPD_teams_distribute_parallel_for_simd: |
| 6988 | case OMPD_distribute_parallel_for: |
| 6989 | case OMPD_distribute_parallel_for_simd: |
| 6990 | case OMPD_task: |
| 6991 | case OMPD_taskloop: |
| 6992 | case OMPD_taskloop_simd: |
| 6993 | case OMPD_target_data: |
| 6994 | case OMPD_target_enter_data: |
| 6995 | case OMPD_target_exit_data: |
| 6996 | case OMPD_target_update: |
| 6997 | case OMPD_teams: |
| 6998 | case OMPD_teams_distribute: |
| 6999 | case OMPD_teams_distribute_simd: |
| 7000 | // Do not capture num_teams-clause expressions. |
| 7001 | break; |
| 7002 | case OMPD_threadprivate: |
| 7003 | case OMPD_taskyield: |
| 7004 | case OMPD_barrier: |
| 7005 | case OMPD_taskwait: |
| 7006 | case OMPD_cancellation_point: |
| 7007 | case OMPD_flush: |
| 7008 | case OMPD_declare_reduction: |
| 7009 | case OMPD_declare_simd: |
| 7010 | case OMPD_declare_target: |
| 7011 | case OMPD_end_declare_target: |
| 7012 | case OMPD_simd: |
| 7013 | case OMPD_for: |
| 7014 | case OMPD_for_simd: |
| 7015 | case OMPD_sections: |
| 7016 | case OMPD_section: |
| 7017 | case OMPD_single: |
| 7018 | case OMPD_master: |
| 7019 | case OMPD_critical: |
| 7020 | case OMPD_taskgroup: |
| 7021 | case OMPD_distribute: |
| 7022 | case OMPD_ordered: |
| 7023 | case OMPD_atomic: |
| 7024 | case OMPD_distribute_simd: |
| 7025 | llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); |
| 7026 | case OMPD_unknown: |
| 7027 | llvm_unreachable("Unknown OpenMP directive"); |
| 7028 | } |
| 7029 | break; |
Arpith Chacko Jacob | 7ecc0b7 | 2017-01-25 11:44:35 +0000 | [diff] [blame] | 7030 | case OMPC_thread_limit: |
| 7031 | switch (DKind) { |
| 7032 | case OMPD_target_teams: |
| 7033 | CaptureRegion = OMPD_target; |
| 7034 | break; |
| 7035 | case OMPD_cancel: |
| 7036 | case OMPD_parallel: |
| 7037 | case OMPD_parallel_sections: |
| 7038 | case OMPD_parallel_for: |
| 7039 | case OMPD_parallel_for_simd: |
| 7040 | case OMPD_target: |
| 7041 | case OMPD_target_simd: |
| 7042 | case OMPD_target_parallel: |
| 7043 | case OMPD_target_parallel_for: |
| 7044 | case OMPD_target_parallel_for_simd: |
| 7045 | case OMPD_target_teams_distribute: |
| 7046 | case OMPD_target_teams_distribute_simd: |
| 7047 | case OMPD_target_teams_distribute_parallel_for: |
| 7048 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 7049 | case OMPD_teams_distribute_parallel_for: |
| 7050 | case OMPD_teams_distribute_parallel_for_simd: |
| 7051 | case OMPD_distribute_parallel_for: |
| 7052 | case OMPD_distribute_parallel_for_simd: |
| 7053 | case OMPD_task: |
| 7054 | case OMPD_taskloop: |
| 7055 | case OMPD_taskloop_simd: |
| 7056 | case OMPD_target_data: |
| 7057 | case OMPD_target_enter_data: |
| 7058 | case OMPD_target_exit_data: |
| 7059 | case OMPD_target_update: |
| 7060 | case OMPD_teams: |
| 7061 | case OMPD_teams_distribute: |
| 7062 | case OMPD_teams_distribute_simd: |
| 7063 | // Do not capture thread_limit-clause expressions. |
| 7064 | break; |
| 7065 | case OMPD_threadprivate: |
| 7066 | case OMPD_taskyield: |
| 7067 | case OMPD_barrier: |
| 7068 | case OMPD_taskwait: |
| 7069 | case OMPD_cancellation_point: |
| 7070 | case OMPD_flush: |
| 7071 | case OMPD_declare_reduction: |
| 7072 | case OMPD_declare_simd: |
| 7073 | case OMPD_declare_target: |
| 7074 | case OMPD_end_declare_target: |
| 7075 | case OMPD_simd: |
| 7076 | case OMPD_for: |
| 7077 | case OMPD_for_simd: |
| 7078 | case OMPD_sections: |
| 7079 | case OMPD_section: |
| 7080 | case OMPD_single: |
| 7081 | case OMPD_master: |
| 7082 | case OMPD_critical: |
| 7083 | case OMPD_taskgroup: |
| 7084 | case OMPD_distribute: |
| 7085 | case OMPD_ordered: |
| 7086 | case OMPD_atomic: |
| 7087 | case OMPD_distribute_simd: |
| 7088 | llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); |
| 7089 | case OMPD_unknown: |
| 7090 | llvm_unreachable("Unknown OpenMP directive"); |
| 7091 | } |
| 7092 | break; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 7093 | case OMPC_schedule: |
| 7094 | case OMPC_dist_schedule: |
| 7095 | case OMPC_firstprivate: |
| 7096 | case OMPC_lastprivate: |
| 7097 | case OMPC_reduction: |
| 7098 | case OMPC_linear: |
| 7099 | case OMPC_default: |
| 7100 | case OMPC_proc_bind: |
| 7101 | case OMPC_final: |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 7102 | case OMPC_safelen: |
| 7103 | case OMPC_simdlen: |
| 7104 | case OMPC_collapse: |
| 7105 | case OMPC_private: |
| 7106 | case OMPC_shared: |
| 7107 | case OMPC_aligned: |
| 7108 | case OMPC_copyin: |
| 7109 | case OMPC_copyprivate: |
| 7110 | case OMPC_ordered: |
| 7111 | case OMPC_nowait: |
| 7112 | case OMPC_untied: |
| 7113 | case OMPC_mergeable: |
| 7114 | case OMPC_threadprivate: |
| 7115 | case OMPC_flush: |
| 7116 | case OMPC_read: |
| 7117 | case OMPC_write: |
| 7118 | case OMPC_update: |
| 7119 | case OMPC_capture: |
| 7120 | case OMPC_seq_cst: |
| 7121 | case OMPC_depend: |
| 7122 | case OMPC_device: |
| 7123 | case OMPC_threads: |
| 7124 | case OMPC_simd: |
| 7125 | case OMPC_map: |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 7126 | case OMPC_priority: |
| 7127 | case OMPC_grainsize: |
| 7128 | case OMPC_nogroup: |
| 7129 | case OMPC_num_tasks: |
| 7130 | case OMPC_hint: |
| 7131 | case OMPC_defaultmap: |
| 7132 | case OMPC_unknown: |
| 7133 | case OMPC_uniform: |
| 7134 | case OMPC_to: |
| 7135 | case OMPC_from: |
| 7136 | case OMPC_use_device_ptr: |
| 7137 | case OMPC_is_device_ptr: |
| 7138 | llvm_unreachable("Unexpected OpenMP clause."); |
| 7139 | } |
| 7140 | return CaptureRegion; |
| 7141 | } |
| 7142 | |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 7143 | OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, |
| 7144 | Expr *Condition, SourceLocation StartLoc, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7145 | SourceLocation LParenLoc, |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 7146 | SourceLocation NameModifierLoc, |
| 7147 | SourceLocation ColonLoc, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7148 | SourceLocation EndLoc) { |
| 7149 | Expr *ValExpr = Condition; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 7150 | Stmt *HelperValStmt = nullptr; |
| 7151 | OpenMPDirectiveKind CaptureRegion = OMPD_unknown; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7152 | if (!Condition->isValueDependent() && !Condition->isTypeDependent() && |
| 7153 | !Condition->isInstantiationDependent() && |
| 7154 | !Condition->containsUnexpandedParameterPack()) { |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 7155 | ExprResult Val = CheckBooleanCondition(StartLoc, Condition); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7156 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 7157 | return nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7158 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 7159 | ValExpr = MakeFullExpr(Val.get()).get(); |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 7160 | |
| 7161 | OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); |
| 7162 | CaptureRegion = |
| 7163 | getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); |
| 7164 | if (CaptureRegion != OMPD_unknown) { |
| 7165 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
| 7166 | ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); |
| 7167 | HelperValStmt = buildPreInits(Context, Captures); |
| 7168 | } |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 7171 | return new (Context) |
| 7172 | OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, |
| 7173 | LParenLoc, NameModifierLoc, ColonLoc, EndLoc); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7174 | } |
| 7175 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 7176 | OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, |
| 7177 | SourceLocation StartLoc, |
| 7178 | SourceLocation LParenLoc, |
| 7179 | SourceLocation EndLoc) { |
| 7180 | Expr *ValExpr = Condition; |
| 7181 | if (!Condition->isValueDependent() && !Condition->isTypeDependent() && |
| 7182 | !Condition->isInstantiationDependent() && |
| 7183 | !Condition->containsUnexpandedParameterPack()) { |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 7184 | ExprResult Val = CheckBooleanCondition(StartLoc, Condition); |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 7185 | if (Val.isInvalid()) |
| 7186 | return nullptr; |
| 7187 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 7188 | ValExpr = MakeFullExpr(Val.get()).get(); |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 7189 | } |
| 7190 | |
| 7191 | return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 7192 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 7193 | ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, |
| 7194 | Expr *Op) { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7195 | if (!Op) |
| 7196 | return ExprError(); |
| 7197 | |
| 7198 | class IntConvertDiagnoser : public ICEConvertDiagnoser { |
| 7199 | public: |
| 7200 | IntConvertDiagnoser() |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7201 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 7202 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 7203 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7204 | return S.Diag(Loc, diag::err_omp_not_integral) << T; |
| 7205 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7206 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
| 7207 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7208 | return S.Diag(Loc, diag::err_omp_incomplete_type) << T; |
| 7209 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7210 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
| 7211 | QualType T, |
| 7212 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7213 | return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; |
| 7214 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7215 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
| 7216 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7217 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7218 | << ConvTy->isEnumeralType() << ConvTy; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7219 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7220 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 7221 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7222 | return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; |
| 7223 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7224 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
| 7225 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7226 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7227 | << ConvTy->isEnumeralType() << ConvTy; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7228 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7229 | SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, |
| 7230 | QualType) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7231 | llvm_unreachable("conversion functions are permitted"); |
| 7232 | } |
| 7233 | } ConvertDiagnoser; |
| 7234 | return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); |
| 7235 | } |
| 7236 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7237 | static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7238 | OpenMPClauseKind CKind, |
| 7239 | bool StrictlyPositive) { |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7240 | if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && |
| 7241 | !ValExpr->isInstantiationDependent()) { |
| 7242 | SourceLocation Loc = ValExpr->getExprLoc(); |
| 7243 | ExprResult Value = |
| 7244 | SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); |
| 7245 | if (Value.isInvalid()) |
| 7246 | return false; |
| 7247 | |
| 7248 | ValExpr = Value.get(); |
| 7249 | // The expression must evaluate to a non-negative integer value. |
| 7250 | llvm::APSInt Result; |
| 7251 | if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7252 | Result.isSigned() && |
| 7253 | !((!StrictlyPositive && Result.isNonNegative()) || |
| 7254 | (StrictlyPositive && Result.isStrictlyPositive()))) { |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7255 | SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7256 | << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) |
| 7257 | << ValExpr->getSourceRange(); |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7258 | return false; |
| 7259 | } |
| 7260 | } |
| 7261 | return true; |
| 7262 | } |
| 7263 | |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7264 | OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, |
| 7265 | SourceLocation StartLoc, |
| 7266 | SourceLocation LParenLoc, |
| 7267 | SourceLocation EndLoc) { |
| 7268 | Expr *ValExpr = NumThreads; |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 7269 | Stmt *HelperValStmt = nullptr; |
| 7270 | OpenMPDirectiveKind CaptureRegion = OMPD_unknown; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7271 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7272 | // OpenMP [2.5, Restrictions] |
| 7273 | // The num_threads expression must evaluate to a positive integer value. |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7274 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, |
| 7275 | /*StrictlyPositive=*/true)) |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7276 | return nullptr; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7277 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 7278 | OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); |
| 7279 | CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); |
| 7280 | if (CaptureRegion != OMPD_unknown) { |
| 7281 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
| 7282 | ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); |
| 7283 | HelperValStmt = buildPreInits(Context, Captures); |
| 7284 | } |
| 7285 | |
| 7286 | return new (Context) OMPNumThreadsClause( |
| 7287 | ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7288 | } |
| 7289 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7290 | ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 7291 | OpenMPClauseKind CKind, |
| 7292 | bool StrictlyPositive) { |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7293 | if (!E) |
| 7294 | return ExprError(); |
| 7295 | if (E->isValueDependent() || E->isTypeDependent() || |
| 7296 | E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7297 | return E; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7298 | llvm::APSInt Result; |
| 7299 | ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); |
| 7300 | if (ICE.isInvalid()) |
| 7301 | return ExprError(); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 7302 | if ((StrictlyPositive && !Result.isStrictlyPositive()) || |
| 7303 | (!StrictlyPositive && !Result.isNonNegative())) { |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7304 | Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 7305 | << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) |
| 7306 | << E->getSourceRange(); |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7307 | return ExprError(); |
| 7308 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 7309 | if (CKind == OMPC_aligned && !Result.isPowerOf2()) { |
| 7310 | Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) |
| 7311 | << E->getSourceRange(); |
| 7312 | return ExprError(); |
| 7313 | } |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 7314 | if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) |
| 7315 | DSAStack->setAssociatedLoops(Result.getExtValue()); |
Alexey Bataev | 7b6bc88 | 2015-11-26 07:50:39 +0000 | [diff] [blame] | 7316 | else if (CKind == OMPC_ordered) |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 7317 | DSAStack->setAssociatedLoops(Result.getExtValue()); |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7318 | return ICE; |
| 7319 | } |
| 7320 | |
| 7321 | OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, |
| 7322 | SourceLocation LParenLoc, |
| 7323 | SourceLocation EndLoc) { |
| 7324 | // OpenMP [2.8.1, simd construct, Description] |
| 7325 | // The parameter of the safelen clause must be a constant |
| 7326 | // positive integer expression. |
| 7327 | ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); |
| 7328 | if (Safelen.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 7329 | return nullptr; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7330 | return new (Context) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7331 | OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7332 | } |
| 7333 | |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 7334 | OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, |
| 7335 | SourceLocation LParenLoc, |
| 7336 | SourceLocation EndLoc) { |
| 7337 | // OpenMP [2.8.1, simd construct, Description] |
| 7338 | // The parameter of the simdlen clause must be a constant |
| 7339 | // positive integer expression. |
| 7340 | ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); |
| 7341 | if (Simdlen.isInvalid()) |
| 7342 | return nullptr; |
| 7343 | return new (Context) |
| 7344 | OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); |
| 7345 | } |
| 7346 | |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 7347 | OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, |
| 7348 | SourceLocation StartLoc, |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 7349 | SourceLocation LParenLoc, |
| 7350 | SourceLocation EndLoc) { |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 7351 | // OpenMP [2.7.1, loop construct, Description] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 7352 | // OpenMP [2.8.1, simd construct, Description] |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 7353 | // OpenMP [2.9.6, distribute construct, Description] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 7354 | // The parameter of the collapse clause must be a constant |
| 7355 | // positive integer expression. |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 7356 | ExprResult NumForLoopsResult = |
| 7357 | VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); |
| 7358 | if (NumForLoopsResult.isInvalid()) |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 7359 | return nullptr; |
| 7360 | return new (Context) |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 7361 | OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 7362 | } |
| 7363 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 7364 | OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, |
| 7365 | SourceLocation EndLoc, |
| 7366 | SourceLocation LParenLoc, |
| 7367 | Expr *NumForLoops) { |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 7368 | // OpenMP [2.7.1, loop construct, Description] |
| 7369 | // OpenMP [2.8.1, simd construct, Description] |
| 7370 | // OpenMP [2.9.6, distribute construct, Description] |
| 7371 | // The parameter of the ordered clause must be a constant |
| 7372 | // positive integer expression if any. |
| 7373 | if (NumForLoops && LParenLoc.isValid()) { |
| 7374 | ExprResult NumForLoopsResult = |
| 7375 | VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); |
| 7376 | if (NumForLoopsResult.isInvalid()) |
| 7377 | return nullptr; |
| 7378 | NumForLoops = NumForLoopsResult.get(); |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 7379 | } else |
| 7380 | NumForLoops = nullptr; |
| 7381 | DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops); |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 7382 | return new (Context) |
| 7383 | OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc); |
| 7384 | } |
| 7385 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7386 | OMPClause *Sema::ActOnOpenMPSimpleClause( |
| 7387 | OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, |
| 7388 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 7389 | OMPClause *Res = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7390 | switch (Kind) { |
| 7391 | case OMPC_default: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7392 | Res = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7393 | ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), |
| 7394 | ArgumentLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7395 | break; |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 7396 | case OMPC_proc_bind: |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7397 | Res = ActOnOpenMPProcBindClause( |
| 7398 | static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, |
| 7399 | LParenLoc, EndLoc); |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 7400 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7401 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 7402 | case OMPC_final: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7403 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7404 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 7405 | case OMPC_simdlen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 7406 | case OMPC_collapse: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7407 | case OMPC_schedule: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7408 | case OMPC_private: |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7409 | case OMPC_firstprivate: |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7410 | case OMPC_lastprivate: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7411 | case OMPC_shared: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 7412 | case OMPC_reduction: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 7413 | case OMPC_linear: |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 7414 | case OMPC_aligned: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 7415 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 7416 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7417 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7418 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 7419 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 7420 | case OMPC_mergeable: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7421 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 7422 | case OMPC_flush: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7423 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 7424 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 7425 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 7426 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 7427 | case OMPC_seq_cst: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7428 | case OMPC_depend: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 7429 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 7430 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 7431 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 7432 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 7433 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7434 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7435 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 7436 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 7437 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 7438 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 7439 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 7440 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 7441 | case OMPC_defaultmap: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7442 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 7443 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 7444 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 7445 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 7446 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 7447 | case OMPC_is_device_ptr: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7448 | llvm_unreachable("Clause is not allowed."); |
| 7449 | } |
| 7450 | return Res; |
| 7451 | } |
| 7452 | |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7453 | static std::string |
| 7454 | getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, |
| 7455 | ArrayRef<unsigned> Exclude = llvm::None) { |
| 7456 | std::string Values; |
| 7457 | unsigned Bound = Last >= 2 ? Last - 2 : 0; |
| 7458 | unsigned Skipped = Exclude.size(); |
| 7459 | auto S = Exclude.begin(), E = Exclude.end(); |
| 7460 | for (unsigned i = First; i < Last; ++i) { |
| 7461 | if (std::find(S, E, i) != E) { |
| 7462 | --Skipped; |
| 7463 | continue; |
| 7464 | } |
| 7465 | Values += "'"; |
| 7466 | Values += getOpenMPSimpleClauseTypeName(K, i); |
| 7467 | Values += "'"; |
| 7468 | if (i == Bound - Skipped) |
| 7469 | Values += " or "; |
| 7470 | else if (i != Bound + 1 - Skipped) |
| 7471 | Values += ", "; |
| 7472 | } |
| 7473 | return Values; |
| 7474 | } |
| 7475 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7476 | OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, |
| 7477 | SourceLocation KindKwLoc, |
| 7478 | SourceLocation StartLoc, |
| 7479 | SourceLocation LParenLoc, |
| 7480 | SourceLocation EndLoc) { |
| 7481 | if (Kind == OMPC_DEFAULT_unknown) { |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 7482 | static_assert(OMPC_DEFAULT_unknown > 0, |
| 7483 | "OMPC_DEFAULT_unknown not greater than 0"); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7484 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7485 | << getListOfPossibleValues(OMPC_default, /*First=*/0, |
| 7486 | /*Last=*/OMPC_DEFAULT_unknown) |
| 7487 | << getOpenMPClauseName(OMPC_default); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 7488 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7489 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7490 | switch (Kind) { |
| 7491 | case OMPC_DEFAULT_none: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 7492 | DSAStack->setDefaultDSANone(KindKwLoc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7493 | break; |
| 7494 | case OMPC_DEFAULT_shared: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 7495 | DSAStack->setDefaultDSAShared(KindKwLoc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7496 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7497 | case OMPC_DEFAULT_unknown: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7498 | llvm_unreachable("Clause kind is not allowed."); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7499 | break; |
| 7500 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7501 | return new (Context) |
| 7502 | OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7503 | } |
| 7504 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 7505 | OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, |
| 7506 | SourceLocation KindKwLoc, |
| 7507 | SourceLocation StartLoc, |
| 7508 | SourceLocation LParenLoc, |
| 7509 | SourceLocation EndLoc) { |
| 7510 | if (Kind == OMPC_PROC_BIND_unknown) { |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 7511 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7512 | << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, |
| 7513 | /*Last=*/OMPC_PROC_BIND_unknown) |
| 7514 | << getOpenMPClauseName(OMPC_proc_bind); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 7515 | return nullptr; |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 7516 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7517 | return new (Context) |
| 7518 | OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 7519 | } |
| 7520 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7521 | OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7522 | OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7523 | SourceLocation StartLoc, SourceLocation LParenLoc, |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7524 | ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7525 | SourceLocation EndLoc) { |
| 7526 | OMPClause *Res = nullptr; |
| 7527 | switch (Kind) { |
| 7528 | case OMPC_schedule: |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7529 | enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; |
| 7530 | assert(Argument.size() == NumberOfElements && |
| 7531 | ArgumentLoc.size() == NumberOfElements); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7532 | Res = ActOnOpenMPScheduleClause( |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7533 | static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), |
| 7534 | static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), |
| 7535 | static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, |
| 7536 | StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], |
| 7537 | ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7538 | break; |
| 7539 | case OMPC_if: |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7540 | assert(Argument.size() == 1 && ArgumentLoc.size() == 1); |
| 7541 | Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), |
| 7542 | Expr, StartLoc, LParenLoc, ArgumentLoc.back(), |
| 7543 | DelimLoc, EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 7544 | break; |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 7545 | case OMPC_dist_schedule: |
| 7546 | Res = ActOnOpenMPDistScheduleClause( |
| 7547 | static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, |
| 7548 | StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); |
| 7549 | break; |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 7550 | case OMPC_defaultmap: |
| 7551 | enum { Modifier, DefaultmapKind }; |
| 7552 | Res = ActOnOpenMPDefaultmapClause( |
| 7553 | static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), |
| 7554 | static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 7555 | StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], |
| 7556 | EndLoc); |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 7557 | break; |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 7558 | case OMPC_final: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7559 | case OMPC_num_threads: |
| 7560 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 7561 | case OMPC_simdlen: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7562 | case OMPC_collapse: |
| 7563 | case OMPC_default: |
| 7564 | case OMPC_proc_bind: |
| 7565 | case OMPC_private: |
| 7566 | case OMPC_firstprivate: |
| 7567 | case OMPC_lastprivate: |
| 7568 | case OMPC_shared: |
| 7569 | case OMPC_reduction: |
| 7570 | case OMPC_linear: |
| 7571 | case OMPC_aligned: |
| 7572 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 7573 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7574 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7575 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 7576 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 7577 | case OMPC_mergeable: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7578 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 7579 | case OMPC_flush: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7580 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 7581 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 7582 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 7583 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 7584 | case OMPC_seq_cst: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7585 | case OMPC_depend: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 7586 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 7587 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 7588 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 7589 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 7590 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7591 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7592 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 7593 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 7594 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 7595 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 7596 | case OMPC_hint: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7597 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 7598 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 7599 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 7600 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 7601 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 7602 | case OMPC_is_device_ptr: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7603 | llvm_unreachable("Clause is not allowed."); |
| 7604 | } |
| 7605 | return Res; |
| 7606 | } |
| 7607 | |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7608 | static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, |
| 7609 | OpenMPScheduleClauseModifier M2, |
| 7610 | SourceLocation M1Loc, SourceLocation M2Loc) { |
| 7611 | if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { |
| 7612 | SmallVector<unsigned, 2> Excluded; |
| 7613 | if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) |
| 7614 | Excluded.push_back(M2); |
| 7615 | if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) |
| 7616 | Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); |
| 7617 | if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) |
| 7618 | Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); |
| 7619 | S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) |
| 7620 | << getListOfPossibleValues(OMPC_schedule, |
| 7621 | /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, |
| 7622 | /*Last=*/OMPC_SCHEDULE_MODIFIER_last, |
| 7623 | Excluded) |
| 7624 | << getOpenMPClauseName(OMPC_schedule); |
| 7625 | return true; |
| 7626 | } |
| 7627 | return false; |
| 7628 | } |
| 7629 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7630 | OMPClause *Sema::ActOnOpenMPScheduleClause( |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7631 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7632 | OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7633 | SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, |
| 7634 | SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { |
| 7635 | if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || |
| 7636 | checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) |
| 7637 | return nullptr; |
| 7638 | // OpenMP, 2.7.1, Loop Construct, Restrictions |
| 7639 | // Either the monotonic modifier or the nonmonotonic modifier can be specified |
| 7640 | // but not both. |
| 7641 | if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || |
| 7642 | (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && |
| 7643 | M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || |
| 7644 | (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && |
| 7645 | M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { |
| 7646 | Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) |
| 7647 | << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) |
| 7648 | << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); |
| 7649 | return nullptr; |
| 7650 | } |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7651 | if (Kind == OMPC_SCHEDULE_unknown) { |
| 7652 | std::string Values; |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7653 | if (M1Loc.isInvalid() && M2Loc.isInvalid()) { |
| 7654 | unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; |
| 7655 | Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, |
| 7656 | /*Last=*/OMPC_SCHEDULE_MODIFIER_last, |
| 7657 | Exclude); |
| 7658 | } else { |
| 7659 | Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, |
| 7660 | /*Last=*/OMPC_SCHEDULE_unknown); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7661 | } |
| 7662 | Diag(KindLoc, diag::err_omp_unexpected_clause_value) |
| 7663 | << Values << getOpenMPClauseName(OMPC_schedule); |
| 7664 | return nullptr; |
| 7665 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7666 | // OpenMP, 2.7.1, Loop Construct, Restrictions |
| 7667 | // The nonmonotonic modifier can only be specified with schedule(dynamic) or |
| 7668 | // schedule(guided). |
| 7669 | if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || |
| 7670 | M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && |
| 7671 | Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { |
| 7672 | Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, |
| 7673 | diag::err_omp_schedule_nonmonotonic_static); |
| 7674 | return nullptr; |
| 7675 | } |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7676 | Expr *ValExpr = ChunkSize; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 7677 | Stmt *HelperValStmt = nullptr; |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7678 | if (ChunkSize) { |
| 7679 | if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && |
| 7680 | !ChunkSize->isInstantiationDependent() && |
| 7681 | !ChunkSize->containsUnexpandedParameterPack()) { |
| 7682 | SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); |
| 7683 | ExprResult Val = |
| 7684 | PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); |
| 7685 | if (Val.isInvalid()) |
| 7686 | return nullptr; |
| 7687 | |
| 7688 | ValExpr = Val.get(); |
| 7689 | |
| 7690 | // OpenMP [2.7.1, Restrictions] |
| 7691 | // chunk_size must be a loop invariant integer expression with a positive |
| 7692 | // value. |
| 7693 | llvm::APSInt Result; |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 7694 | if (ValExpr->isIntegerConstantExpr(Result, Context)) { |
| 7695 | if (Result.isSigned() && !Result.isStrictlyPositive()) { |
| 7696 | Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7697 | << "schedule" << 1 << ChunkSize->getSourceRange(); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 7698 | return nullptr; |
| 7699 | } |
Alexey Bataev | b46cdea | 2016-06-15 11:20:48 +0000 | [diff] [blame] | 7700 | } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && |
| 7701 | !CurContext->isDependentContext()) { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 7702 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
| 7703 | ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); |
| 7704 | HelperValStmt = buildPreInits(Context, Captures); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7705 | } |
| 7706 | } |
| 7707 | } |
| 7708 | |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7709 | return new (Context) |
| 7710 | OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 7711 | ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7712 | } |
| 7713 | |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7714 | OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, |
| 7715 | SourceLocation StartLoc, |
| 7716 | SourceLocation EndLoc) { |
| 7717 | OMPClause *Res = nullptr; |
| 7718 | switch (Kind) { |
| 7719 | case OMPC_ordered: |
| 7720 | Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); |
| 7721 | break; |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7722 | case OMPC_nowait: |
| 7723 | Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); |
| 7724 | break; |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 7725 | case OMPC_untied: |
| 7726 | Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); |
| 7727 | break; |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 7728 | case OMPC_mergeable: |
| 7729 | Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); |
| 7730 | break; |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7731 | case OMPC_read: |
| 7732 | Res = ActOnOpenMPReadClause(StartLoc, EndLoc); |
| 7733 | break; |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 7734 | case OMPC_write: |
| 7735 | Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); |
| 7736 | break; |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 7737 | case OMPC_update: |
| 7738 | Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); |
| 7739 | break; |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 7740 | case OMPC_capture: |
| 7741 | Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); |
| 7742 | break; |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 7743 | case OMPC_seq_cst: |
| 7744 | Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); |
| 7745 | break; |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 7746 | case OMPC_threads: |
| 7747 | Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); |
| 7748 | break; |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 7749 | case OMPC_simd: |
| 7750 | Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); |
| 7751 | break; |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 7752 | case OMPC_nogroup: |
| 7753 | Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); |
| 7754 | break; |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7755 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 7756 | case OMPC_final: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7757 | case OMPC_num_threads: |
| 7758 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 7759 | case OMPC_simdlen: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7760 | case OMPC_collapse: |
| 7761 | case OMPC_schedule: |
| 7762 | case OMPC_private: |
| 7763 | case OMPC_firstprivate: |
| 7764 | case OMPC_lastprivate: |
| 7765 | case OMPC_shared: |
| 7766 | case OMPC_reduction: |
| 7767 | case OMPC_linear: |
| 7768 | case OMPC_aligned: |
| 7769 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 7770 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7771 | case OMPC_default: |
| 7772 | case OMPC_proc_bind: |
| 7773 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 7774 | case OMPC_flush: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7775 | case OMPC_depend: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 7776 | case OMPC_device: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 7777 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 7778 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7779 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7780 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 7781 | case OMPC_grainsize: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 7782 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 7783 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 7784 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 7785 | case OMPC_defaultmap: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7786 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 7787 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 7788 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 7789 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 7790 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 7791 | case OMPC_is_device_ptr: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7792 | llvm_unreachable("Clause is not allowed."); |
| 7793 | } |
| 7794 | return Res; |
| 7795 | } |
| 7796 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7797 | OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, |
| 7798 | SourceLocation EndLoc) { |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 7799 | DSAStack->setNowaitRegion(); |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7800 | return new (Context) OMPNowaitClause(StartLoc, EndLoc); |
| 7801 | } |
| 7802 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 7803 | OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, |
| 7804 | SourceLocation EndLoc) { |
| 7805 | return new (Context) OMPUntiedClause(StartLoc, EndLoc); |
| 7806 | } |
| 7807 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 7808 | OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, |
| 7809 | SourceLocation EndLoc) { |
| 7810 | return new (Context) OMPMergeableClause(StartLoc, EndLoc); |
| 7811 | } |
| 7812 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7813 | OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, |
| 7814 | SourceLocation EndLoc) { |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7815 | return new (Context) OMPReadClause(StartLoc, EndLoc); |
| 7816 | } |
| 7817 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 7818 | OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, |
| 7819 | SourceLocation EndLoc) { |
| 7820 | return new (Context) OMPWriteClause(StartLoc, EndLoc); |
| 7821 | } |
| 7822 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 7823 | OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, |
| 7824 | SourceLocation EndLoc) { |
| 7825 | return new (Context) OMPUpdateClause(StartLoc, EndLoc); |
| 7826 | } |
| 7827 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 7828 | OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, |
| 7829 | SourceLocation EndLoc) { |
| 7830 | return new (Context) OMPCaptureClause(StartLoc, EndLoc); |
| 7831 | } |
| 7832 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 7833 | OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, |
| 7834 | SourceLocation EndLoc) { |
| 7835 | return new (Context) OMPSeqCstClause(StartLoc, EndLoc); |
| 7836 | } |
| 7837 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 7838 | OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, |
| 7839 | SourceLocation EndLoc) { |
| 7840 | return new (Context) OMPThreadsClause(StartLoc, EndLoc); |
| 7841 | } |
| 7842 | |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 7843 | OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, |
| 7844 | SourceLocation EndLoc) { |
| 7845 | return new (Context) OMPSIMDClause(StartLoc, EndLoc); |
| 7846 | } |
| 7847 | |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 7848 | OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, |
| 7849 | SourceLocation EndLoc) { |
| 7850 | return new (Context) OMPNogroupClause(StartLoc, EndLoc); |
| 7851 | } |
| 7852 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 7853 | OMPClause *Sema::ActOnOpenMPVarListClause( |
| 7854 | OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, |
| 7855 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, |
| 7856 | SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7857 | const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, |
Samuel Antao | 23abd72 | 2016-01-19 20:40:49 +0000 | [diff] [blame] | 7858 | OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, |
| 7859 | OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, |
| 7860 | SourceLocation DepLinMapLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 7861 | OMPClause *Res = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7862 | switch (Kind) { |
| 7863 | case OMPC_private: |
| 7864 | Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7865 | break; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7866 | case OMPC_firstprivate: |
| 7867 | Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7868 | break; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7869 | case OMPC_lastprivate: |
| 7870 | Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7871 | break; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7872 | case OMPC_shared: |
| 7873 | Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7874 | break; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 7875 | case OMPC_reduction: |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 7876 | Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, |
| 7877 | EndLoc, ReductionIdScopeSpec, ReductionId); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 7878 | break; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 7879 | case OMPC_linear: |
| 7880 | Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 7881 | LinKind, DepLinMapLoc, ColonLoc, EndLoc); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 7882 | break; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 7883 | case OMPC_aligned: |
| 7884 | Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, |
| 7885 | ColonLoc, EndLoc); |
| 7886 | break; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 7887 | case OMPC_copyin: |
| 7888 | Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7889 | break; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 7890 | case OMPC_copyprivate: |
| 7891 | Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7892 | break; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 7893 | case OMPC_flush: |
| 7894 | Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7895 | break; |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7896 | case OMPC_depend: |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 7897 | Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 7898 | StartLoc, LParenLoc, EndLoc); |
| 7899 | break; |
| 7900 | case OMPC_map: |
Samuel Antao | 23abd72 | 2016-01-19 20:40:49 +0000 | [diff] [blame] | 7901 | Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit, |
| 7902 | DepLinMapLoc, ColonLoc, VarList, StartLoc, |
| 7903 | LParenLoc, EndLoc); |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7904 | break; |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 7905 | case OMPC_to: |
| 7906 | Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7907 | break; |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 7908 | case OMPC_from: |
| 7909 | Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7910 | break; |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 7911 | case OMPC_use_device_ptr: |
| 7912 | Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7913 | break; |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 7914 | case OMPC_is_device_ptr: |
| 7915 | Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7916 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7917 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 7918 | case OMPC_final: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7919 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7920 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 7921 | case OMPC_simdlen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 7922 | case OMPC_collapse: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7923 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 7924 | case OMPC_proc_bind: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7925 | case OMPC_schedule: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7926 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7927 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 7928 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 7929 | case OMPC_mergeable: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7930 | case OMPC_threadprivate: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7931 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 7932 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 7933 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 7934 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 7935 | case OMPC_seq_cst: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 7936 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 7937 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 7938 | case OMPC_simd: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 7939 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7940 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7941 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 7942 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 7943 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 7944 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 7945 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 7946 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 7947 | case OMPC_defaultmap: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7948 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 7949 | case OMPC_uniform: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7950 | llvm_unreachable("Clause is not allowed."); |
| 7951 | } |
| 7952 | return Res; |
| 7953 | } |
| 7954 | |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 7955 | ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 7956 | ExprObjectKind OK, SourceLocation Loc) { |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 7957 | ExprResult Res = BuildDeclRefExpr( |
| 7958 | Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); |
| 7959 | if (!Res.isUsable()) |
| 7960 | return ExprError(); |
| 7961 | if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { |
| 7962 | Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); |
| 7963 | if (!Res.isUsable()) |
| 7964 | return ExprError(); |
| 7965 | } |
| 7966 | if (VK != VK_LValue && Res.get()->isGLValue()) { |
| 7967 | Res = DefaultLvalueConversion(Res.get()); |
| 7968 | if (!Res.isUsable()) |
| 7969 | return ExprError(); |
| 7970 | } |
| 7971 | return Res; |
| 7972 | } |
| 7973 | |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7974 | static std::pair<ValueDecl *, bool> |
| 7975 | getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, |
| 7976 | SourceRange &ERange, bool AllowArraySection = false) { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7977 | if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || |
| 7978 | RefExpr->containsUnexpandedParameterPack()) |
| 7979 | return std::make_pair(nullptr, true); |
| 7980 | |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7981 | // OpenMP [3.1, C/C++] |
| 7982 | // A list item is a variable name. |
| 7983 | // OpenMP [2.9.3.3, Restrictions, p.1] |
| 7984 | // A variable that is part of another variable (as an array or |
| 7985 | // structure element) cannot appear in a private clause. |
| 7986 | RefExpr = RefExpr->IgnoreParens(); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7987 | enum { |
| 7988 | NoArrayExpr = -1, |
| 7989 | ArraySubscript = 0, |
| 7990 | OMPArraySection = 1 |
| 7991 | } IsArrayExpr = NoArrayExpr; |
| 7992 | if (AllowArraySection) { |
| 7993 | if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { |
| 7994 | auto *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 7995 | while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 7996 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 7997 | RefExpr = Base; |
| 7998 | IsArrayExpr = ArraySubscript; |
| 7999 | } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { |
| 8000 | auto *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 8001 | while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
| 8002 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
| 8003 | while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 8004 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 8005 | RefExpr = Base; |
| 8006 | IsArrayExpr = OMPArraySection; |
| 8007 | } |
| 8008 | } |
| 8009 | ELoc = RefExpr->getExprLoc(); |
| 8010 | ERange = RefExpr->getSourceRange(); |
| 8011 | RefExpr = RefExpr->IgnoreParenImpCasts(); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8012 | auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
| 8013 | auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); |
| 8014 | if ((!DE || !isa<VarDecl>(DE->getDecl())) && |
| 8015 | (S.getCurrentThisType().isNull() || !ME || |
| 8016 | !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || |
| 8017 | !isa<FieldDecl>(ME->getMemberDecl()))) { |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8018 | if (IsArrayExpr != NoArrayExpr) |
| 8019 | S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr |
| 8020 | << ERange; |
| 8021 | else { |
| 8022 | S.Diag(ELoc, |
| 8023 | AllowArraySection |
| 8024 | ? diag::err_omp_expected_var_name_member_expr_or_array_item |
| 8025 | : diag::err_omp_expected_var_name_member_expr) |
| 8026 | << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; |
| 8027 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8028 | return std::make_pair(nullptr, false); |
| 8029 | } |
| 8030 | return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false); |
| 8031 | } |
| 8032 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8033 | OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, |
| 8034 | SourceLocation StartLoc, |
| 8035 | SourceLocation LParenLoc, |
| 8036 | SourceLocation EndLoc) { |
| 8037 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 8038 | SmallVector<Expr *, 8> PrivateCopies; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8039 | for (auto &RefExpr : VarList) { |
| 8040 | assert(RefExpr && "NULL expr in OpenMP private clause."); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8041 | SourceLocation ELoc; |
| 8042 | SourceRange ERange; |
| 8043 | Expr *SimpleRefExpr = RefExpr; |
| 8044 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8045 | if (Res.second) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8046 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8047 | Vars.push_back(RefExpr); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 8048 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8049 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8050 | ValueDecl *D = Res.first; |
| 8051 | if (!D) |
| 8052 | continue; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8053 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 8054 | QualType Type = D->getType(); |
| 8055 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8056 | |
| 8057 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 8058 | // A variable that appears in a private clause must not have an incomplete |
| 8059 | // type or a reference type. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 8060 | if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8061 | continue; |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 8062 | Type = Type.getNonReferenceType(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8063 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8064 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 8065 | // in a Construct] |
| 8066 | // Variables with the predetermined data-sharing attributes may not be |
| 8067 | // listed in data-sharing attributes clauses, except for the cases |
| 8068 | // listed below. For these exceptions only, listing a predetermined |
| 8069 | // variable in a data-sharing attribute clause is allowed and overrides |
| 8070 | // the variable's predetermined data-sharing attributes. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 8071 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8072 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8073 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 8074 | << getOpenMPClauseName(OMPC_private); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 8075 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8076 | continue; |
| 8077 | } |
| 8078 | |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 8079 | auto CurrDir = DSAStack->getCurrentDirective(); |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8080 | // Variably modified types are not supported for tasks. |
Alexey Bataev | 5129d3a | 2015-05-21 09:47:46 +0000 | [diff] [blame] | 8081 | if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 8082 | isOpenMPTaskingDirective(CurrDir)) { |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8083 | Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) |
| 8084 | << getOpenMPClauseName(OMPC_private) << Type |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 8085 | << getOpenMPDirectiveName(CurrDir); |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8086 | bool IsDecl = |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 8087 | !VD || |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8088 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 8089 | Diag(D->getLocation(), |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8090 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 8091 | << D; |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8092 | continue; |
| 8093 | } |
| 8094 | |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 8095 | // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] |
| 8096 | // A list item cannot appear in both a map clause and a data-sharing |
| 8097 | // attribute clause on the same construct |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 8098 | if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel || |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 8099 | CurrDir == OMPD_target_teams || |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 8100 | CurrDir == OMPD_target_teams_distribute || |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 8101 | CurrDir == OMPD_target_teams_distribute_parallel_for || |
Kelvin Li | c4bfc6f | 2017-01-10 04:26:44 +0000 | [diff] [blame] | 8102 | CurrDir == OMPD_target_teams_distribute_parallel_for_simd || |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 8103 | CurrDir == OMPD_target_teams_distribute_simd || |
Kelvin Li | 4101032 | 2017-01-10 05:15:35 +0000 | [diff] [blame] | 8104 | CurrDir == OMPD_target_parallel_for_simd || |
| 8105 | CurrDir == OMPD_target_parallel_for) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8106 | OpenMPClauseKind ConflictKind; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 8107 | if (DSAStack->checkMappableExprComponentListsForDecl( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 8108 | VD, /*CurrentRegionOnly=*/true, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8109 | [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| 8110 | OpenMPClauseKind WhereFoundClauseKind) -> bool { |
| 8111 | ConflictKind = WhereFoundClauseKind; |
| 8112 | return true; |
| 8113 | })) { |
| 8114 | Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 8115 | << getOpenMPClauseName(OMPC_private) |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8116 | << getOpenMPClauseName(ConflictKind) |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 8117 | << getOpenMPDirectiveName(CurrDir); |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 8118 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
| 8119 | continue; |
| 8120 | } |
| 8121 | } |
| 8122 | |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8123 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] |
| 8124 | // A variable of class type (or array thereof) that appears in a private |
| 8125 | // clause requires an accessible, unambiguous default constructor for the |
| 8126 | // class type. |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 8127 | // Generate helper private variable and initialize it with the default |
| 8128 | // value. The address of the original variable is replaced by the address of |
| 8129 | // the new private variable in CodeGen. This new variable is not added to |
| 8130 | // IdResolver, so the code in the OpenMP region uses original variable for |
| 8131 | // proper diagnostics. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8132 | Type = Type.getUnqualifiedType(); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 8133 | auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 8134 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 8135 | ActOnUninitializedDecl(VDPrivate); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 8136 | if (VDPrivate->isInvalidDecl()) |
| 8137 | continue; |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8138 | auto VDPrivateRefExpr = buildDeclRefExpr( |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 8139 | *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 8140 | |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 8141 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8142 | if (!VD && !CurContext->isDependentContext()) |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8143 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 8144 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8145 | Vars.push_back((VD || CurContext->isDependentContext()) |
| 8146 | ? RefExpr->IgnoreParens() |
| 8147 | : Ref); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 8148 | PrivateCopies.push_back(VDPrivateRefExpr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8149 | } |
| 8150 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8151 | if (Vars.empty()) |
| 8152 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8153 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 8154 | return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, |
| 8155 | PrivateCopies); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 8156 | } |
| 8157 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8158 | namespace { |
| 8159 | class DiagsUninitializedSeveretyRAII { |
| 8160 | private: |
| 8161 | DiagnosticsEngine &Diags; |
| 8162 | SourceLocation SavedLoc; |
| 8163 | bool IsIgnored; |
| 8164 | |
| 8165 | public: |
| 8166 | DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, |
| 8167 | bool IsIgnored) |
| 8168 | : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { |
| 8169 | if (!IsIgnored) { |
| 8170 | Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, |
| 8171 | /*Map*/ diag::Severity::Ignored, Loc); |
| 8172 | } |
| 8173 | } |
| 8174 | ~DiagsUninitializedSeveretyRAII() { |
| 8175 | if (!IsIgnored) |
| 8176 | Diags.popMappings(SavedLoc); |
| 8177 | } |
| 8178 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8179 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8180 | |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8181 | OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, |
| 8182 | SourceLocation StartLoc, |
| 8183 | SourceLocation LParenLoc, |
| 8184 | SourceLocation EndLoc) { |
| 8185 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8186 | SmallVector<Expr *, 8> PrivateCopies; |
| 8187 | SmallVector<Expr *, 8> Inits; |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 8188 | SmallVector<Decl *, 4> ExprCaptures; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 8189 | bool IsImplicitClause = |
| 8190 | StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); |
| 8191 | auto ImplicitClauseLoc = DSAStack->getConstructLoc(); |
| 8192 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8193 | for (auto &RefExpr : VarList) { |
| 8194 | assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8195 | SourceLocation ELoc; |
| 8196 | SourceRange ERange; |
| 8197 | Expr *SimpleRefExpr = RefExpr; |
| 8198 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8199 | if (Res.second) { |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8200 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8201 | Vars.push_back(RefExpr); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8202 | PrivateCopies.push_back(nullptr); |
| 8203 | Inits.push_back(nullptr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8204 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8205 | ValueDecl *D = Res.first; |
| 8206 | if (!D) |
| 8207 | continue; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8208 | |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8209 | ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8210 | QualType Type = D->getType(); |
| 8211 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8212 | |
| 8213 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 8214 | // A variable that appears in a private clause must not have an incomplete |
| 8215 | // type or a reference type. |
| 8216 | if (RequireCompleteType(ELoc, Type, |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8217 | diag::err_omp_firstprivate_incomplete_type)) |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8218 | continue; |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 8219 | Type = Type.getNonReferenceType(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8220 | |
| 8221 | // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] |
| 8222 | // 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] | 8223 | // clause requires an accessible, unambiguous copy constructor for the |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8224 | // class type. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8225 | auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8226 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 8227 | // If an implicit firstprivate variable found it was checked already. |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8228 | DSAStackTy::DSAVarData TopDVar; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 8229 | if (!IsImplicitClause) { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8230 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8231 | TopDVar = DVar; |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8232 | bool IsConstant = ElemType.isConstant(Context); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8233 | // OpenMP [2.4.13, Data-sharing Attribute Clauses] |
| 8234 | // A list item that specifies a given variable may not appear in more |
| 8235 | // than one clause on the same directive, except that a variable may be |
| 8236 | // specified in both firstprivate and lastprivate clauses. |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8237 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8238 | DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8239 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8240 | << getOpenMPClauseName(DVar.CKind) |
| 8241 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8242 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8243 | continue; |
| 8244 | } |
| 8245 | |
| 8246 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 8247 | // in a Construct] |
| 8248 | // Variables with the predetermined data-sharing attributes may not be |
| 8249 | // listed in data-sharing attributes clauses, except for the cases |
| 8250 | // listed below. For these exceptions only, listing a predetermined |
| 8251 | // variable in a data-sharing attribute clause is allowed and overrides |
| 8252 | // the variable's predetermined data-sharing attributes. |
| 8253 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 8254 | // in a Construct, C/C++, p.2] |
| 8255 | // Variables with const-qualified type having no mutable member may be |
| 8256 | // listed in a firstprivate clause, even if they are static data members. |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8257 | if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8258 | DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { |
| 8259 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8260 | << getOpenMPClauseName(DVar.CKind) |
| 8261 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8262 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8263 | continue; |
| 8264 | } |
| 8265 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8266 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8267 | // OpenMP [2.9.3.4, Restrictions, p.2] |
| 8268 | // A list item that is private within a parallel region must not appear |
| 8269 | // in a firstprivate clause on a worksharing construct if any of the |
| 8270 | // worksharing regions arising from the worksharing construct ever bind |
| 8271 | // to any of the parallel regions arising from the parallel construct. |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 8272 | if (isOpenMPWorksharingDirective(CurrDir) && |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 8273 | !isOpenMPParallelDirective(CurrDir) && |
| 8274 | !isOpenMPTeamsDirective(CurrDir)) { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8275 | DVar = DSAStack->getImplicitDSA(D, true); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 8276 | if (DVar.CKind != OMPC_shared && |
| 8277 | (isOpenMPParallelDirective(DVar.DKind) || |
| 8278 | DVar.DKind == OMPD_unknown)) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8279 | Diag(ELoc, diag::err_omp_required_access) |
| 8280 | << getOpenMPClauseName(OMPC_firstprivate) |
| 8281 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8282 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8283 | continue; |
| 8284 | } |
| 8285 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8286 | // OpenMP [2.9.3.4, Restrictions, p.3] |
| 8287 | // A list item that appears in a reduction clause of a parallel construct |
| 8288 | // must not appear in a firstprivate clause on a worksharing or task |
| 8289 | // construct if any of the worksharing or task regions arising from the |
| 8290 | // worksharing or task construct ever bind to any of the parallel regions |
| 8291 | // arising from the parallel construct. |
| 8292 | // OpenMP [2.9.3.4, Restrictions, p.4] |
| 8293 | // A list item that appears in a reduction clause in worksharing |
| 8294 | // construct must not appear in a firstprivate clause in a task construct |
| 8295 | // encountered during execution of any of the worksharing regions arising |
| 8296 | // from the worksharing construct. |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 8297 | if (isOpenMPTaskingDirective(CurrDir)) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 8298 | DVar = DSAStack->hasInnermostDSA( |
| 8299 | D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, |
| 8300 | [](OpenMPDirectiveKind K) -> bool { |
| 8301 | return isOpenMPParallelDirective(K) || |
| 8302 | isOpenMPWorksharingDirective(K); |
| 8303 | }, |
| 8304 | false); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 8305 | if (DVar.CKind == OMPC_reduction && |
| 8306 | (isOpenMPParallelDirective(DVar.DKind) || |
| 8307 | isOpenMPWorksharingDirective(DVar.DKind))) { |
| 8308 | Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) |
| 8309 | << getOpenMPDirectiveName(DVar.DKind); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8310 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 8311 | continue; |
| 8312 | } |
| 8313 | } |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 8314 | |
| 8315 | // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] |
| 8316 | // A list item that is private within a teams region must not appear in a |
| 8317 | // firstprivate clause on a distribute construct if any of the distribute |
| 8318 | // regions arising from the distribute construct ever bind to any of the |
| 8319 | // teams regions arising from the teams construct. |
| 8320 | // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] |
| 8321 | // A list item that appears in a reduction clause of a teams construct |
| 8322 | // must not appear in a firstprivate clause on a distribute construct if |
| 8323 | // any of the distribute regions arising from the distribute construct |
| 8324 | // ever bind to any of the teams regions arising from the teams construct. |
| 8325 | // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] |
| 8326 | // A list item may appear in a firstprivate or lastprivate clause but not |
| 8327 | // both. |
| 8328 | if (CurrDir == OMPD_distribute) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 8329 | DVar = DSAStack->hasInnermostDSA( |
| 8330 | D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; }, |
| 8331 | [](OpenMPDirectiveKind K) -> bool { |
| 8332 | return isOpenMPTeamsDirective(K); |
| 8333 | }, |
| 8334 | false); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 8335 | if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) { |
| 8336 | Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8337 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 8338 | continue; |
| 8339 | } |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 8340 | DVar = DSAStack->hasInnermostDSA( |
| 8341 | D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, |
| 8342 | [](OpenMPDirectiveKind K) -> bool { |
| 8343 | return isOpenMPTeamsDirective(K); |
| 8344 | }, |
| 8345 | false); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 8346 | if (DVar.CKind == OMPC_reduction && |
| 8347 | isOpenMPTeamsDirective(DVar.DKind)) { |
| 8348 | Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8349 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 8350 | continue; |
| 8351 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8352 | DVar = DSAStack->getTopDSA(D, false); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 8353 | if (DVar.CKind == OMPC_lastprivate) { |
| 8354 | Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8355 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 8356 | continue; |
| 8357 | } |
| 8358 | } |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 8359 | // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] |
| 8360 | // A list item cannot appear in both a map clause and a data-sharing |
| 8361 | // attribute clause on the same construct |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 8362 | if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel || |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 8363 | CurrDir == OMPD_target_teams || |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 8364 | CurrDir == OMPD_target_teams_distribute || |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 8365 | CurrDir == OMPD_target_teams_distribute_parallel_for || |
Kelvin Li | c4bfc6f | 2017-01-10 04:26:44 +0000 | [diff] [blame] | 8366 | CurrDir == OMPD_target_teams_distribute_parallel_for_simd || |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 8367 | CurrDir == OMPD_target_teams_distribute_simd || |
Kelvin Li | 4101032 | 2017-01-10 05:15:35 +0000 | [diff] [blame] | 8368 | CurrDir == OMPD_target_parallel_for_simd || |
| 8369 | CurrDir == OMPD_target_parallel_for) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8370 | OpenMPClauseKind ConflictKind; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 8371 | if (DSAStack->checkMappableExprComponentListsForDecl( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 8372 | VD, /*CurrentRegionOnly=*/true, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8373 | [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| 8374 | OpenMPClauseKind WhereFoundClauseKind) -> bool { |
| 8375 | ConflictKind = WhereFoundClauseKind; |
| 8376 | return true; |
| 8377 | })) { |
| 8378 | Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 8379 | << getOpenMPClauseName(OMPC_firstprivate) |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8380 | << getOpenMPClauseName(ConflictKind) |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 8381 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 8382 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
| 8383 | continue; |
| 8384 | } |
| 8385 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8386 | } |
| 8387 | |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8388 | // Variably modified types are not supported for tasks. |
Alexey Bataev | 5129d3a | 2015-05-21 09:47:46 +0000 | [diff] [blame] | 8389 | if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 8390 | isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8391 | Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) |
| 8392 | << getOpenMPClauseName(OMPC_firstprivate) << Type |
| 8393 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 8394 | bool IsDecl = |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8395 | !VD || |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8396 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8397 | Diag(D->getLocation(), |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8398 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8399 | << D; |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 8400 | continue; |
| 8401 | } |
| 8402 | |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8403 | Type = Type.getUnqualifiedType(); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8404 | auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 8405 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8406 | // Generate helper private variable and initialize it with the value of the |
| 8407 | // original variable. The address of the original variable is replaced by |
| 8408 | // the address of the new private variable in the CodeGen. This new variable |
| 8409 | // is not added to IdResolver, so the code in the OpenMP region uses |
| 8410 | // original variable for proper diagnostics and variable capturing. |
| 8411 | Expr *VDInitRefExpr = nullptr; |
| 8412 | // For arrays generate initializer for single element and replace it by the |
| 8413 | // original array element in CodeGen. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8414 | if (Type->isArrayType()) { |
| 8415 | auto VDInit = |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8416 | buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8417 | VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8418 | auto Init = DefaultLvalueConversion(VDInitRefExpr).get(); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8419 | ElemType = ElemType.getUnqualifiedType(); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8420 | auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 8421 | ".firstprivate.temp"); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 8422 | InitializedEntity Entity = |
| 8423 | InitializedEntity::InitializeVariable(VDInitTemp); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8424 | InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); |
| 8425 | |
| 8426 | InitializationSequence InitSeq(*this, Entity, Kind, Init); |
| 8427 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); |
| 8428 | if (Result.isInvalid()) |
| 8429 | VDPrivate->setInvalidDecl(); |
| 8430 | else |
| 8431 | VDPrivate->setInit(Result.getAs<Expr>()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8432 | // Remove temp variable declaration. |
| 8433 | Context.Deallocate(VDInitTemp); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8434 | } else { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8435 | auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, |
| 8436 | ".firstprivate.temp"); |
| 8437 | VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), |
| 8438 | RefExpr->getExprLoc()); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 8439 | AddInitializerToDecl(VDPrivate, |
| 8440 | DefaultLvalueConversion(VDInitRefExpr).get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 8441 | /*DirectInit=*/false); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8442 | } |
| 8443 | if (VDPrivate->isInvalidDecl()) { |
| 8444 | if (IsImplicitClause) { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8445 | Diag(RefExpr->getExprLoc(), |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8446 | diag::note_omp_task_predetermined_firstprivate_here); |
| 8447 | } |
| 8448 | continue; |
| 8449 | } |
| 8450 | CurContext->addDecl(VDPrivate); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 8451 | auto VDPrivateRefExpr = buildDeclRefExpr( |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8452 | *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), |
| 8453 | RefExpr->getExprLoc()); |
| 8454 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8455 | if (!VD && !CurContext->isDependentContext()) { |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8456 | if (TopDVar.CKind == OMPC_lastprivate) |
| 8457 | Ref = TopDVar.PrivateCopy; |
| 8458 | else { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8459 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8460 | if (!IsOpenMPCapturedDecl(D)) |
| 8461 | ExprCaptures.push_back(Ref->getDecl()); |
| 8462 | } |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 8463 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 8464 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8465 | Vars.push_back((VD || CurContext->isDependentContext()) |
| 8466 | ? RefExpr->IgnoreParens() |
| 8467 | : Ref); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 8468 | PrivateCopies.push_back(VDPrivateRefExpr); |
| 8469 | Inits.push_back(VDInitRefExpr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8470 | } |
| 8471 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8472 | if (Vars.empty()) |
| 8473 | return nullptr; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8474 | |
| 8475 | return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 8476 | Vars, PrivateCopies, Inits, |
| 8477 | buildPreInits(Context, ExprCaptures)); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 8478 | } |
| 8479 | |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8480 | OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, |
| 8481 | SourceLocation StartLoc, |
| 8482 | SourceLocation LParenLoc, |
| 8483 | SourceLocation EndLoc) { |
| 8484 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 8485 | SmallVector<Expr *, 8> SrcExprs; |
| 8486 | SmallVector<Expr *, 8> DstExprs; |
| 8487 | SmallVector<Expr *, 8> AssignmentOps; |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8488 | SmallVector<Decl *, 4> ExprCaptures; |
| 8489 | SmallVector<Expr *, 4> ExprPostUpdates; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8490 | for (auto &RefExpr : VarList) { |
| 8491 | assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8492 | SourceLocation ELoc; |
| 8493 | SourceRange ERange; |
| 8494 | Expr *SimpleRefExpr = RefExpr; |
| 8495 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8496 | if (Res.second) { |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8497 | // It will be analyzed later. |
| 8498 | Vars.push_back(RefExpr); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 8499 | SrcExprs.push_back(nullptr); |
| 8500 | DstExprs.push_back(nullptr); |
| 8501 | AssignmentOps.push_back(nullptr); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8502 | } |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8503 | ValueDecl *D = Res.first; |
| 8504 | if (!D) |
| 8505 | continue; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8506 | |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8507 | QualType Type = D->getType(); |
| 8508 | auto *VD = dyn_cast<VarDecl>(D); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8509 | |
| 8510 | // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] |
| 8511 | // A variable that appears in a lastprivate clause must not have an |
| 8512 | // incomplete type or a reference type. |
| 8513 | if (RequireCompleteType(ELoc, Type, |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8514 | diag::err_omp_lastprivate_incomplete_type)) |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8515 | continue; |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 8516 | Type = Type.getNonReferenceType(); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8517 | |
| 8518 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 8519 | // in a Construct] |
| 8520 | // Variables with the predetermined data-sharing attributes may not be |
| 8521 | // listed in data-sharing attributes clauses, except for the cases |
| 8522 | // listed below. |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8523 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8524 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && |
| 8525 | DVar.CKind != OMPC_firstprivate && |
| 8526 | (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { |
| 8527 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 8528 | << getOpenMPClauseName(DVar.CKind) |
| 8529 | << getOpenMPClauseName(OMPC_lastprivate); |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8530 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8531 | continue; |
| 8532 | } |
| 8533 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8534 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
| 8535 | // OpenMP [2.14.3.5, Restrictions, p.2] |
| 8536 | // A list item that is private within a parallel region, or that appears in |
| 8537 | // the reduction clause of a parallel construct, must not appear in a |
| 8538 | // lastprivate clause on a worksharing construct if any of the corresponding |
| 8539 | // worksharing regions ever binds to any of the corresponding parallel |
| 8540 | // regions. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 8541 | DSAStackTy::DSAVarData TopDVar = DVar; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 8542 | if (isOpenMPWorksharingDirective(CurrDir) && |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 8543 | !isOpenMPParallelDirective(CurrDir) && |
| 8544 | !isOpenMPTeamsDirective(CurrDir)) { |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8545 | DVar = DSAStack->getImplicitDSA(D, true); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8546 | if (DVar.CKind != OMPC_shared) { |
| 8547 | Diag(ELoc, diag::err_omp_required_access) |
| 8548 | << getOpenMPClauseName(OMPC_lastprivate) |
| 8549 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8550 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8551 | continue; |
| 8552 | } |
| 8553 | } |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8554 | |
| 8555 | // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] |
| 8556 | // A list item may appear in a firstprivate or lastprivate clause but not |
| 8557 | // both. |
| 8558 | if (CurrDir == OMPD_distribute) { |
| 8559 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
| 8560 | if (DVar.CKind == OMPC_firstprivate) { |
| 8561 | Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute); |
| 8562 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
| 8563 | continue; |
| 8564 | } |
| 8565 | } |
| 8566 | |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8567 | // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8568 | // A variable of class type (or array thereof) that appears in a |
| 8569 | // lastprivate clause requires an accessible, unambiguous default |
| 8570 | // constructor for the class type, unless the list item is also specified |
| 8571 | // in a firstprivate clause. |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8572 | // A variable of class type (or array thereof) that appears in a |
| 8573 | // lastprivate clause requires an accessible, unambiguous copy assignment |
| 8574 | // operator for the class type. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 8575 | Type = Context.getBaseElementType(Type).getNonReferenceType(); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8576 | auto *SrcVD = buildVarDecl(*this, ERange.getBegin(), |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 8577 | Type.getUnqualifiedType(), ".lastprivate.src", |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8578 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 8579 | auto *PseudoSrcExpr = |
| 8580 | buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 8581 | auto *DstVD = |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8582 | buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8583 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 8584 | auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 8585 | // For arrays generate assignment operation for single element and replace |
| 8586 | // it by the original array element in CodeGen. |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8587 | auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 8588 | PseudoDstExpr, PseudoSrcExpr); |
| 8589 | if (AssignmentOp.isInvalid()) |
| 8590 | continue; |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8591 | AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 8592 | /*DiscardedValue=*/true); |
| 8593 | if (AssignmentOp.isInvalid()) |
| 8594 | continue; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8595 | |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 8596 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8597 | if (!VD && !CurContext->isDependentContext()) { |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8598 | if (TopDVar.CKind == OMPC_firstprivate) |
| 8599 | Ref = TopDVar.PrivateCopy; |
| 8600 | else { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8601 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8602 | if (!IsOpenMPCapturedDecl(D)) |
| 8603 | ExprCaptures.push_back(Ref->getDecl()); |
| 8604 | } |
| 8605 | if (TopDVar.CKind == OMPC_firstprivate || |
| 8606 | (!IsOpenMPCapturedDecl(D) && |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8607 | Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8608 | ExprResult RefRes = DefaultLvalueConversion(Ref); |
| 8609 | if (!RefRes.isUsable()) |
| 8610 | continue; |
| 8611 | ExprResult PostUpdateRes = |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8612 | BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, |
| 8613 | RefRes.get()); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8614 | if (!PostUpdateRes.isUsable()) |
| 8615 | continue; |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 8616 | ExprPostUpdates.push_back( |
| 8617 | IgnoredValueConversions(PostUpdateRes.get()).get()); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8618 | } |
| 8619 | } |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 8620 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8621 | Vars.push_back((VD || CurContext->isDependentContext()) |
| 8622 | ? RefExpr->IgnoreParens() |
| 8623 | : Ref); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 8624 | SrcExprs.push_back(PseudoSrcExpr); |
| 8625 | DstExprs.push_back(PseudoDstExpr); |
| 8626 | AssignmentOps.push_back(AssignmentOp.get()); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8627 | } |
| 8628 | |
| 8629 | if (Vars.empty()) |
| 8630 | return nullptr; |
| 8631 | |
| 8632 | return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 8633 | Vars, SrcExprs, DstExprs, AssignmentOps, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 8634 | buildPreInits(Context, ExprCaptures), |
| 8635 | buildPostUpdate(*this, ExprPostUpdates)); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 8636 | } |
| 8637 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8638 | OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, |
| 8639 | SourceLocation StartLoc, |
| 8640 | SourceLocation LParenLoc, |
| 8641 | SourceLocation EndLoc) { |
| 8642 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8643 | for (auto &RefExpr : VarList) { |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8644 | assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8645 | SourceLocation ELoc; |
| 8646 | SourceRange ERange; |
| 8647 | Expr *SimpleRefExpr = RefExpr; |
| 8648 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8649 | if (Res.second) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8650 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8651 | Vars.push_back(RefExpr); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8652 | } |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8653 | ValueDecl *D = Res.first; |
| 8654 | if (!D) |
| 8655 | continue; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8656 | |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8657 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8658 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 8659 | // in a Construct] |
| 8660 | // Variables with the predetermined data-sharing attributes may not be |
| 8661 | // listed in data-sharing attributes clauses, except for the cases |
| 8662 | // listed below. For these exceptions only, listing a predetermined |
| 8663 | // variable in a data-sharing attribute clause is allowed and overrides |
| 8664 | // the variable's predetermined data-sharing attributes. |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8665 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8666 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && |
| 8667 | DVar.RefExpr) { |
| 8668 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 8669 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8670 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8671 | continue; |
| 8672 | } |
| 8673 | |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8674 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8675 | if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8676 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8677 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8678 | Vars.push_back((VD || !Ref || CurContext->isDependentContext()) |
| 8679 | ? RefExpr->IgnoreParens() |
| 8680 | : Ref); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8681 | } |
| 8682 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8683 | if (Vars.empty()) |
| 8684 | return nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8685 | |
| 8686 | return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 8687 | } |
| 8688 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8689 | namespace { |
| 8690 | class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { |
| 8691 | DSAStackTy *Stack; |
| 8692 | |
| 8693 | public: |
| 8694 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 8695 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 8696 | DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8697 | if (DVar.CKind == OMPC_shared && !DVar.RefExpr) |
| 8698 | return false; |
| 8699 | if (DVar.CKind != OMPC_unknown) |
| 8700 | return true; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 8701 | DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( |
| 8702 | VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, |
| 8703 | false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8704 | if (DVarPrivate.CKind != OMPC_unknown) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8705 | return true; |
| 8706 | return false; |
| 8707 | } |
| 8708 | return false; |
| 8709 | } |
| 8710 | bool VisitStmt(Stmt *S) { |
| 8711 | for (auto Child : S->children()) { |
| 8712 | if (Child && Visit(Child)) |
| 8713 | return true; |
| 8714 | } |
| 8715 | return false; |
| 8716 | } |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 8717 | explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8718 | }; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 8719 | } // namespace |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8720 | |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8721 | namespace { |
| 8722 | // Transform MemberExpression for specified FieldDecl of current class to |
| 8723 | // DeclRefExpr to specified OMPCapturedExprDecl. |
| 8724 | class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { |
| 8725 | typedef TreeTransform<TransformExprToCaptures> BaseTransform; |
| 8726 | ValueDecl *Field; |
| 8727 | DeclRefExpr *CapturedExpr; |
| 8728 | |
| 8729 | public: |
| 8730 | TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) |
| 8731 | : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} |
| 8732 | |
| 8733 | ExprResult TransformMemberExpr(MemberExpr *E) { |
| 8734 | if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && |
| 8735 | E->getMemberDecl() == Field) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8736 | CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8737 | return CapturedExpr; |
| 8738 | } |
| 8739 | return BaseTransform::TransformMemberExpr(E); |
| 8740 | } |
| 8741 | DeclRefExpr *getCapturedExpr() { return CapturedExpr; } |
| 8742 | }; |
| 8743 | } // namespace |
| 8744 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8745 | template <typename T> |
| 8746 | static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups, |
| 8747 | const llvm::function_ref<T(ValueDecl *)> &Gen) { |
| 8748 | for (auto &Set : Lookups) { |
| 8749 | for (auto *D : Set) { |
| 8750 | if (auto Res = Gen(cast<ValueDecl>(D))) |
| 8751 | return Res; |
| 8752 | } |
| 8753 | } |
| 8754 | return T(); |
| 8755 | } |
| 8756 | |
| 8757 | static ExprResult |
| 8758 | buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, |
| 8759 | Scope *S, CXXScopeSpec &ReductionIdScopeSpec, |
| 8760 | const DeclarationNameInfo &ReductionId, QualType Ty, |
| 8761 | CXXCastPath &BasePath, Expr *UnresolvedReduction) { |
| 8762 | if (ReductionIdScopeSpec.isInvalid()) |
| 8763 | return ExprError(); |
| 8764 | SmallVector<UnresolvedSet<8>, 4> Lookups; |
| 8765 | if (S) { |
| 8766 | LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); |
| 8767 | Lookup.suppressDiagnostics(); |
| 8768 | while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { |
| 8769 | auto *D = Lookup.getRepresentativeDecl(); |
| 8770 | do { |
| 8771 | S = S->getParent(); |
| 8772 | } while (S && !S->isDeclScope(D)); |
| 8773 | if (S) |
| 8774 | S = S->getParent(); |
| 8775 | Lookups.push_back(UnresolvedSet<8>()); |
| 8776 | Lookups.back().append(Lookup.begin(), Lookup.end()); |
| 8777 | Lookup.clear(); |
| 8778 | } |
| 8779 | } else if (auto *ULE = |
| 8780 | cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { |
| 8781 | Lookups.push_back(UnresolvedSet<8>()); |
| 8782 | Decl *PrevD = nullptr; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 8783 | for (auto *D : ULE->decls()) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8784 | if (D == PrevD) |
| 8785 | Lookups.push_back(UnresolvedSet<8>()); |
| 8786 | else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) |
| 8787 | Lookups.back().addDecl(DRD); |
| 8788 | PrevD = D; |
| 8789 | } |
| 8790 | } |
| 8791 | if (Ty->isDependentType() || Ty->isInstantiationDependentType() || |
| 8792 | Ty->containsUnexpandedParameterPack() || |
| 8793 | filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool { |
| 8794 | return !D->isInvalidDecl() && |
| 8795 | (D->getType()->isDependentType() || |
| 8796 | D->getType()->isInstantiationDependentType() || |
| 8797 | D->getType()->containsUnexpandedParameterPack()); |
| 8798 | })) { |
| 8799 | UnresolvedSet<8> ResSet; |
| 8800 | for (auto &Set : Lookups) { |
| 8801 | ResSet.append(Set.begin(), Set.end()); |
| 8802 | // The last item marks the end of all declarations at the specified scope. |
| 8803 | ResSet.addDecl(Set[Set.size() - 1]); |
| 8804 | } |
| 8805 | return UnresolvedLookupExpr::Create( |
| 8806 | SemaRef.Context, /*NamingClass=*/nullptr, |
| 8807 | ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, |
| 8808 | /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); |
| 8809 | } |
| 8810 | if (auto *VD = filterLookupForUDR<ValueDecl *>( |
| 8811 | Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { |
| 8812 | if (!D->isInvalidDecl() && |
| 8813 | SemaRef.Context.hasSameType(D->getType(), Ty)) |
| 8814 | return D; |
| 8815 | return nullptr; |
| 8816 | })) |
| 8817 | return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); |
| 8818 | if (auto *VD = filterLookupForUDR<ValueDecl *>( |
| 8819 | Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { |
| 8820 | if (!D->isInvalidDecl() && |
| 8821 | SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && |
| 8822 | !Ty.isMoreQualifiedThan(D->getType())) |
| 8823 | return D; |
| 8824 | return nullptr; |
| 8825 | })) { |
| 8826 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8827 | /*DetectVirtual=*/false); |
| 8828 | if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { |
| 8829 | if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( |
| 8830 | VD->getType().getUnqualifiedType()))) { |
| 8831 | if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), |
| 8832 | /*DiagID=*/0) != |
| 8833 | Sema::AR_inaccessible) { |
| 8834 | SemaRef.BuildBasePathArray(Paths, BasePath); |
| 8835 | return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); |
| 8836 | } |
| 8837 | } |
| 8838 | } |
| 8839 | } |
| 8840 | if (ReductionIdScopeSpec.isSet()) { |
| 8841 | SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; |
| 8842 | return ExprError(); |
| 8843 | } |
| 8844 | return ExprEmpty(); |
| 8845 | } |
| 8846 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8847 | OMPClause *Sema::ActOnOpenMPReductionClause( |
| 8848 | ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, |
| 8849 | SourceLocation ColonLoc, SourceLocation EndLoc, |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8850 | CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, |
| 8851 | ArrayRef<Expr *> UnresolvedReductions) { |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8852 | auto DN = ReductionId.getName(); |
| 8853 | auto OOK = DN.getCXXOverloadedOperator(); |
| 8854 | BinaryOperatorKind BOK = BO_Comma; |
| 8855 | |
| 8856 | // OpenMP [2.14.3.6, reduction clause] |
| 8857 | // C |
| 8858 | // reduction-identifier is either an identifier or one of the following |
| 8859 | // operators: +, -, *, &, |, ^, && and || |
| 8860 | // C++ |
| 8861 | // reduction-identifier is either an id-expression or one of the following |
| 8862 | // operators: +, -, *, &, |, ^, && and || |
| 8863 | // FIXME: Only 'min' and 'max' identifiers are supported for now. |
| 8864 | switch (OOK) { |
| 8865 | case OO_Plus: |
| 8866 | case OO_Minus: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8867 | BOK = BO_Add; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8868 | break; |
| 8869 | case OO_Star: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8870 | BOK = BO_Mul; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8871 | break; |
| 8872 | case OO_Amp: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8873 | BOK = BO_And; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8874 | break; |
| 8875 | case OO_Pipe: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8876 | BOK = BO_Or; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8877 | break; |
| 8878 | case OO_Caret: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8879 | BOK = BO_Xor; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8880 | break; |
| 8881 | case OO_AmpAmp: |
| 8882 | BOK = BO_LAnd; |
| 8883 | break; |
| 8884 | case OO_PipePipe: |
| 8885 | BOK = BO_LOr; |
| 8886 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8887 | case OO_New: |
| 8888 | case OO_Delete: |
| 8889 | case OO_Array_New: |
| 8890 | case OO_Array_Delete: |
| 8891 | case OO_Slash: |
| 8892 | case OO_Percent: |
| 8893 | case OO_Tilde: |
| 8894 | case OO_Exclaim: |
| 8895 | case OO_Equal: |
| 8896 | case OO_Less: |
| 8897 | case OO_Greater: |
| 8898 | case OO_LessEqual: |
| 8899 | case OO_GreaterEqual: |
| 8900 | case OO_PlusEqual: |
| 8901 | case OO_MinusEqual: |
| 8902 | case OO_StarEqual: |
| 8903 | case OO_SlashEqual: |
| 8904 | case OO_PercentEqual: |
| 8905 | case OO_CaretEqual: |
| 8906 | case OO_AmpEqual: |
| 8907 | case OO_PipeEqual: |
| 8908 | case OO_LessLess: |
| 8909 | case OO_GreaterGreater: |
| 8910 | case OO_LessLessEqual: |
| 8911 | case OO_GreaterGreaterEqual: |
| 8912 | case OO_EqualEqual: |
| 8913 | case OO_ExclaimEqual: |
| 8914 | case OO_PlusPlus: |
| 8915 | case OO_MinusMinus: |
| 8916 | case OO_Comma: |
| 8917 | case OO_ArrowStar: |
| 8918 | case OO_Arrow: |
| 8919 | case OO_Call: |
| 8920 | case OO_Subscript: |
| 8921 | case OO_Conditional: |
Richard Smith | 9be594e | 2015-10-22 05:12:22 +0000 | [diff] [blame] | 8922 | case OO_Coawait: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8923 | case NUM_OVERLOADED_OPERATORS: |
| 8924 | llvm_unreachable("Unexpected reduction identifier"); |
| 8925 | case OO_None: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8926 | if (auto II = DN.getAsIdentifierInfo()) { |
| 8927 | if (II->isStr("max")) |
| 8928 | BOK = BO_GT; |
| 8929 | else if (II->isStr("min")) |
| 8930 | BOK = BO_LT; |
| 8931 | } |
| 8932 | break; |
| 8933 | } |
| 8934 | SourceRange ReductionIdRange; |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8935 | if (ReductionIdScopeSpec.isValid()) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8936 | ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8937 | ReductionIdRange.setEnd(ReductionId.getEndLoc()); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8938 | |
| 8939 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8940 | SmallVector<Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8941 | SmallVector<Expr *, 8> LHSs; |
| 8942 | SmallVector<Expr *, 8> RHSs; |
| 8943 | SmallVector<Expr *, 8> ReductionOps; |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8944 | SmallVector<Decl *, 4> ExprCaptures; |
| 8945 | SmallVector<Expr *, 4> ExprPostUpdates; |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8946 | auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); |
| 8947 | bool FirstIter = true; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8948 | for (auto RefExpr : VarList) { |
| 8949 | assert(RefExpr && "nullptr expr in OpenMP reduction clause."); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8950 | // OpenMP [2.1, C/C++] |
| 8951 | // A list item is a variable or array section, subject to the restrictions |
| 8952 | // specified in Section 2.4 on page 42 and in each of the sections |
| 8953 | // describing clauses and directives for which a list appears. |
| 8954 | // OpenMP [2.14.3.3, Restrictions, p.1] |
| 8955 | // A variable that is part of another variable (as an array or |
| 8956 | // structure element) cannot appear in a private clause. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8957 | if (!FirstIter && IR != ER) |
| 8958 | ++IR; |
| 8959 | FirstIter = false; |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8960 | SourceLocation ELoc; |
| 8961 | SourceRange ERange; |
| 8962 | Expr *SimpleRefExpr = RefExpr; |
| 8963 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, |
| 8964 | /*AllowArraySection=*/true); |
| 8965 | if (Res.second) { |
| 8966 | // It will be analyzed later. |
| 8967 | Vars.push_back(RefExpr); |
| 8968 | Privates.push_back(nullptr); |
| 8969 | LHSs.push_back(nullptr); |
| 8970 | RHSs.push_back(nullptr); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8971 | // Try to find 'declare reduction' corresponding construct before using |
| 8972 | // builtin/overloaded operators. |
| 8973 | QualType Type = Context.DependentTy; |
| 8974 | CXXCastPath BasePath; |
| 8975 | ExprResult DeclareReductionRef = buildDeclareReductionRef( |
| 8976 | *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec, |
| 8977 | ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); |
| 8978 | if (CurContext->isDependentContext() && |
| 8979 | (DeclareReductionRef.isUnset() || |
| 8980 | isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) |
| 8981 | ReductionOps.push_back(DeclareReductionRef.get()); |
| 8982 | else |
| 8983 | ReductionOps.push_back(nullptr); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8984 | } |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8985 | ValueDecl *D = Res.first; |
| 8986 | if (!D) |
| 8987 | continue; |
| 8988 | |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8989 | QualType Type; |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8990 | auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); |
| 8991 | auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); |
| 8992 | if (ASE) |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 8993 | Type = ASE->getType().getNonReferenceType(); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8994 | else if (OASE) { |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8995 | auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); |
| 8996 | if (auto *ATy = BaseType->getAsArrayTypeUnsafe()) |
| 8997 | Type = ATy->getElementType(); |
| 8998 | else |
| 8999 | Type = BaseType->getPointeeType(); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 9000 | Type = Type.getNonReferenceType(); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9001 | } else |
| 9002 | Type = Context.getBaseElementType(D->getType().getNonReferenceType()); |
| 9003 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 9004 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9005 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 9006 | // A variable that appears in a private clause must not have an incomplete |
| 9007 | // type or a reference type. |
| 9008 | if (RequireCompleteType(ELoc, Type, |
| 9009 | diag::err_omp_reduction_incomplete_type)) |
| 9010 | continue; |
| 9011 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9012 | // A list item that appears in a reduction clause must not be |
| 9013 | // const-qualified. |
| 9014 | if (Type.getNonReferenceType().isConstant(Context)) { |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 9015 | Diag(ELoc, diag::err_omp_const_reduction_list_item) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9016 | << getOpenMPClauseName(OMPC_reduction) << Type << ERange; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9017 | if (!ASE && !OASE) { |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9018 | bool IsDecl = !VD || |
| 9019 | VD->isThisDeclarationADefinition(Context) == |
| 9020 | VarDecl::DeclarationOnly; |
| 9021 | Diag(D->getLocation(), |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 9022 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9023 | << D; |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 9024 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9025 | continue; |
| 9026 | } |
| 9027 | // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] |
| 9028 | // If a list-item is a reference type then it must bind to the same object |
| 9029 | // for all threads of the team. |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9030 | if (!ASE && !OASE && VD) { |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 9031 | VarDecl *VDDef = VD->getDefinition(); |
David Sheinkman | 9258999 | 2016-10-04 14:41:36 +0000 | [diff] [blame] | 9032 | if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 9033 | DSARefChecker Check(DSAStack); |
| 9034 | if (Check.Visit(VDDef->getInit())) { |
| 9035 | Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; |
| 9036 | Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; |
| 9037 | continue; |
| 9038 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9039 | } |
| 9040 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9041 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9042 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 9043 | // in a Construct] |
| 9044 | // Variables with the predetermined data-sharing attributes may not be |
| 9045 | // listed in data-sharing attributes clauses, except for the cases |
| 9046 | // listed below. For these exceptions only, listing a predetermined |
| 9047 | // variable in a data-sharing attribute clause is allowed and overrides |
| 9048 | // the variable's predetermined data-sharing attributes. |
| 9049 | // OpenMP [2.14.3.6, Restrictions, p.3] |
| 9050 | // Any number of reduction clauses can be specified on the directive, |
| 9051 | // but a list item can appear only once in the reduction clauses for that |
| 9052 | // directive. |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 9053 | DSAStackTy::DSAVarData DVar; |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9054 | DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9055 | if (DVar.CKind == OMPC_reduction) { |
| 9056 | Diag(ELoc, diag::err_omp_once_referenced) |
| 9057 | << getOpenMPClauseName(OMPC_reduction); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9058 | if (DVar.RefExpr) |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9059 | Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9060 | } else if (DVar.CKind != OMPC_unknown) { |
| 9061 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 9062 | << getOpenMPClauseName(DVar.CKind) |
| 9063 | << getOpenMPClauseName(OMPC_reduction); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9064 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9065 | continue; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9066 | } |
| 9067 | |
| 9068 | // OpenMP [2.14.3.6, Restrictions, p.1] |
| 9069 | // A list item that appears in a reduction clause of a worksharing |
| 9070 | // construct must be shared in the parallel regions to which any of the |
| 9071 | // worksharing regions arising from the worksharing construct bind. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9072 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
| 9073 | if (isOpenMPWorksharingDirective(CurrDir) && |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 9074 | !isOpenMPParallelDirective(CurrDir) && |
| 9075 | !isOpenMPTeamsDirective(CurrDir)) { |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9076 | DVar = DSAStack->getImplicitDSA(D, true); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9077 | if (DVar.CKind != OMPC_shared) { |
| 9078 | Diag(ELoc, diag::err_omp_required_access) |
| 9079 | << getOpenMPClauseName(OMPC_reduction) |
| 9080 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9081 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9082 | continue; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 9083 | } |
| 9084 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9085 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9086 | // Try to find 'declare reduction' corresponding construct before using |
| 9087 | // builtin/overloaded operators. |
| 9088 | CXXCastPath BasePath; |
| 9089 | ExprResult DeclareReductionRef = buildDeclareReductionRef( |
| 9090 | *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec, |
| 9091 | ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); |
| 9092 | if (DeclareReductionRef.isInvalid()) |
| 9093 | continue; |
| 9094 | if (CurContext->isDependentContext() && |
| 9095 | (DeclareReductionRef.isUnset() || |
| 9096 | isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { |
| 9097 | Vars.push_back(RefExpr); |
| 9098 | Privates.push_back(nullptr); |
| 9099 | LHSs.push_back(nullptr); |
| 9100 | RHSs.push_back(nullptr); |
| 9101 | ReductionOps.push_back(DeclareReductionRef.get()); |
| 9102 | continue; |
| 9103 | } |
| 9104 | if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { |
| 9105 | // Not allowed reduction identifier is found. |
| 9106 | Diag(ReductionId.getLocStart(), |
| 9107 | diag::err_omp_unknown_reduction_identifier) |
| 9108 | << Type << ReductionIdRange; |
| 9109 | continue; |
| 9110 | } |
| 9111 | |
| 9112 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
| 9113 | // The type of a list item that appears in a reduction clause must be valid |
| 9114 | // for the reduction-identifier. For a max or min reduction in C, the type |
| 9115 | // of the list item must be an allowed arithmetic data type: char, int, |
| 9116 | // float, double, or _Bool, possibly modified with long, short, signed, or |
| 9117 | // unsigned. For a max or min reduction in C++, the type of the list item |
| 9118 | // must be an allowed arithmetic data type: char, wchar_t, int, float, |
| 9119 | // double, or bool, possibly modified with long, short, signed, or unsigned. |
| 9120 | if (DeclareReductionRef.isUnset()) { |
| 9121 | if ((BOK == BO_GT || BOK == BO_LT) && |
| 9122 | !(Type->isScalarType() || |
| 9123 | (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { |
| 9124 | Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) |
| 9125 | << getLangOpts().CPlusPlus; |
| 9126 | if (!ASE && !OASE) { |
| 9127 | bool IsDecl = !VD || |
| 9128 | VD->isThisDeclarationADefinition(Context) == |
| 9129 | VarDecl::DeclarationOnly; |
| 9130 | Diag(D->getLocation(), |
| 9131 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 9132 | << D; |
| 9133 | } |
| 9134 | continue; |
| 9135 | } |
| 9136 | if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && |
| 9137 | !getLangOpts().CPlusPlus && Type->isFloatingType()) { |
| 9138 | Diag(ELoc, diag::err_omp_clause_floating_type_arg); |
| 9139 | if (!ASE && !OASE) { |
| 9140 | bool IsDecl = !VD || |
| 9141 | VD->isThisDeclarationADefinition(Context) == |
| 9142 | VarDecl::DeclarationOnly; |
| 9143 | Diag(D->getLocation(), |
| 9144 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 9145 | << D; |
| 9146 | } |
| 9147 | continue; |
| 9148 | } |
| 9149 | } |
| 9150 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 9151 | Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9152 | auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs", |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9153 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 9154 | auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 9155 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9156 | auto PrivateTy = Type; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 9157 | if (OASE || |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9158 | (!ASE && |
| 9159 | D->getType().getNonReferenceType()->isVariablyModifiedType())) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 9160 | // For arrays/array sections only: |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9161 | // Create pseudo array type for private copy. The size for this array will |
| 9162 | // be generated during codegen. |
| 9163 | // For array subscripts or single variables Private Ty is the same as Type |
| 9164 | // (type of the variable or single array element). |
| 9165 | PrivateTy = Context.getVariableArrayType( |
| 9166 | Type, new (Context) OpaqueValueExpr(SourceLocation(), |
| 9167 | Context.getSizeType(), VK_RValue), |
| 9168 | ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9169 | } else if (!ASE && !OASE && |
| 9170 | Context.getAsArrayType(D->getType().getNonReferenceType())) |
| 9171 | PrivateTy = D->getType().getNonReferenceType(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9172 | // Private copy. |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9173 | auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(), |
| 9174 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 9175 | // Add initializer for private variable. |
| 9176 | Expr *Init = nullptr; |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9177 | auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc); |
| 9178 | auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc); |
| 9179 | if (DeclareReductionRef.isUsable()) { |
| 9180 | auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); |
| 9181 | auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); |
| 9182 | if (DRD->getInitializer()) { |
| 9183 | Init = DRDRef; |
| 9184 | RHSVD->setInit(DRDRef); |
| 9185 | RHSVD->setInitStyle(VarDecl::CallInit); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 9186 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9187 | } else { |
| 9188 | switch (BOK) { |
| 9189 | case BO_Add: |
| 9190 | case BO_Xor: |
| 9191 | case BO_Or: |
| 9192 | case BO_LOr: |
| 9193 | // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. |
| 9194 | if (Type->isScalarType() || Type->isAnyComplexType()) |
| 9195 | Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get(); |
| 9196 | break; |
| 9197 | case BO_Mul: |
| 9198 | case BO_LAnd: |
| 9199 | if (Type->isScalarType() || Type->isAnyComplexType()) { |
| 9200 | // '*' and '&&' reduction ops - initializer is '1'. |
| 9201 | Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get(); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9202 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9203 | break; |
| 9204 | case BO_And: { |
| 9205 | // '&' reduction op - initializer is '~0'. |
| 9206 | QualType OrigType = Type; |
| 9207 | if (auto *ComplexTy = OrigType->getAs<ComplexType>()) |
| 9208 | Type = ComplexTy->getElementType(); |
| 9209 | if (Type->isRealFloatingType()) { |
| 9210 | llvm::APFloat InitValue = |
| 9211 | llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), |
| 9212 | /*isIEEE=*/true); |
| 9213 | Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, |
| 9214 | Type, ELoc); |
| 9215 | } else if (Type->isScalarType()) { |
| 9216 | auto Size = Context.getTypeSize(Type); |
| 9217 | QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); |
| 9218 | llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); |
| 9219 | Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); |
| 9220 | } |
| 9221 | if (Init && OrigType->isAnyComplexType()) { |
| 9222 | // Init = 0xFFFF + 0xFFFFi; |
| 9223 | auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); |
| 9224 | Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); |
| 9225 | } |
| 9226 | Type = OrigType; |
| 9227 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 9228 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9229 | case BO_LT: |
| 9230 | case BO_GT: { |
| 9231 | // 'min' reduction op - initializer is 'Largest representable number in |
| 9232 | // the reduction list item type'. |
| 9233 | // 'max' reduction op - initializer is 'Least representable number in |
| 9234 | // the reduction list item type'. |
| 9235 | if (Type->isIntegerType() || Type->isPointerType()) { |
| 9236 | bool IsSigned = Type->hasSignedIntegerRepresentation(); |
| 9237 | auto Size = Context.getTypeSize(Type); |
| 9238 | QualType IntTy = |
| 9239 | Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); |
| 9240 | llvm::APInt InitValue = |
| 9241 | (BOK != BO_LT) |
| 9242 | ? IsSigned ? llvm::APInt::getSignedMinValue(Size) |
| 9243 | : llvm::APInt::getMinValue(Size) |
| 9244 | : IsSigned ? llvm::APInt::getSignedMaxValue(Size) |
| 9245 | : llvm::APInt::getMaxValue(Size); |
| 9246 | Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); |
| 9247 | if (Type->isPointerType()) { |
| 9248 | // Cast to pointer type. |
| 9249 | auto CastExpr = BuildCStyleCastExpr( |
| 9250 | SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc), |
| 9251 | SourceLocation(), Init); |
| 9252 | if (CastExpr.isInvalid()) |
| 9253 | continue; |
| 9254 | Init = CastExpr.get(); |
| 9255 | } |
| 9256 | } else if (Type->isRealFloatingType()) { |
| 9257 | llvm::APFloat InitValue = llvm::APFloat::getLargest( |
| 9258 | Context.getFloatTypeSemantics(Type), BOK != BO_LT); |
| 9259 | Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, |
| 9260 | Type, ELoc); |
| 9261 | } |
| 9262 | break; |
| 9263 | } |
| 9264 | case BO_PtrMemD: |
| 9265 | case BO_PtrMemI: |
| 9266 | case BO_MulAssign: |
| 9267 | case BO_Div: |
| 9268 | case BO_Rem: |
| 9269 | case BO_Sub: |
| 9270 | case BO_Shl: |
| 9271 | case BO_Shr: |
| 9272 | case BO_LE: |
| 9273 | case BO_GE: |
| 9274 | case BO_EQ: |
| 9275 | case BO_NE: |
| 9276 | case BO_AndAssign: |
| 9277 | case BO_XorAssign: |
| 9278 | case BO_OrAssign: |
| 9279 | case BO_Assign: |
| 9280 | case BO_AddAssign: |
| 9281 | case BO_SubAssign: |
| 9282 | case BO_DivAssign: |
| 9283 | case BO_RemAssign: |
| 9284 | case BO_ShlAssign: |
| 9285 | case BO_ShrAssign: |
| 9286 | case BO_Comma: |
| 9287 | llvm_unreachable("Unexpected reduction operation"); |
| 9288 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 9289 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9290 | if (Init && DeclareReductionRef.isUnset()) { |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 9291 | AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9292 | } else if (!Init) |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 9293 | ActOnUninitializedDecl(RHSVD); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9294 | if (RHSVD->isInvalidDecl()) |
| 9295 | continue; |
| 9296 | if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 9297 | Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type |
| 9298 | << ReductionIdRange; |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9299 | bool IsDecl = |
| 9300 | !VD || |
| 9301 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 9302 | Diag(D->getLocation(), |
| 9303 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 9304 | << D; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 9305 | continue; |
| 9306 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9307 | // Store initializer for single element in private copy. Will be used during |
| 9308 | // codegen. |
| 9309 | PrivateVD->setInit(RHSVD->getInit()); |
| 9310 | PrivateVD->setInitStyle(RHSVD->getInitStyle()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9311 | auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9312 | ExprResult ReductionOp; |
| 9313 | if (DeclareReductionRef.isUsable()) { |
| 9314 | QualType RedTy = DeclareReductionRef.get()->getType(); |
| 9315 | QualType PtrRedTy = Context.getPointerType(RedTy); |
| 9316 | ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); |
| 9317 | ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); |
| 9318 | if (!BasePath.empty()) { |
| 9319 | LHS = DefaultLvalueConversion(LHS.get()); |
| 9320 | RHS = DefaultLvalueConversion(RHS.get()); |
| 9321 | LHS = ImplicitCastExpr::Create(Context, PtrRedTy, |
| 9322 | CK_UncheckedDerivedToBase, LHS.get(), |
| 9323 | &BasePath, LHS.get()->getValueKind()); |
| 9324 | RHS = ImplicitCastExpr::Create(Context, PtrRedTy, |
| 9325 | CK_UncheckedDerivedToBase, RHS.get(), |
| 9326 | &BasePath, RHS.get()->getValueKind()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 9327 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 9328 | FunctionProtoType::ExtProtoInfo EPI; |
| 9329 | QualType Params[] = {PtrRedTy, PtrRedTy}; |
| 9330 | QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); |
| 9331 | auto *OVE = new (Context) OpaqueValueExpr( |
| 9332 | ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, |
| 9333 | DefaultLvalueConversion(DeclareReductionRef.get()).get()); |
| 9334 | Expr *Args[] = {LHS.get(), RHS.get()}; |
| 9335 | ReductionOp = new (Context) |
| 9336 | CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); |
| 9337 | } else { |
| 9338 | ReductionOp = BuildBinOp(DSAStack->getCurScope(), |
| 9339 | ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE); |
| 9340 | if (ReductionOp.isUsable()) { |
| 9341 | if (BOK != BO_LT && BOK != BO_GT) { |
| 9342 | ReductionOp = |
| 9343 | BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), |
| 9344 | BO_Assign, LHSDRE, ReductionOp.get()); |
| 9345 | } else { |
| 9346 | auto *ConditionalOp = new (Context) ConditionalOperator( |
| 9347 | ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(), |
| 9348 | RHSDRE, Type, VK_LValue, OK_Ordinary); |
| 9349 | ReductionOp = |
| 9350 | BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), |
| 9351 | BO_Assign, LHSDRE, ConditionalOp); |
| 9352 | } |
| 9353 | ReductionOp = ActOnFinishFullExpr(ReductionOp.get()); |
| 9354 | } |
| 9355 | if (ReductionOp.isInvalid()) |
| 9356 | continue; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9357 | } |
| 9358 | |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9359 | DeclRefExpr *Ref = nullptr; |
| 9360 | Expr *VarsExpr = RefExpr->IgnoreParens(); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 9361 | if (!VD && !CurContext->isDependentContext()) { |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9362 | if (ASE || OASE) { |
| 9363 | TransformExprToCaptures RebuildToCapture(*this, D); |
| 9364 | VarsExpr = |
| 9365 | RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); |
| 9366 | Ref = RebuildToCapture.getCapturedExpr(); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 9367 | } else { |
| 9368 | VarsExpr = Ref = |
| 9369 | buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 9370 | } |
| 9371 | if (!IsOpenMPCapturedDecl(D)) { |
| 9372 | ExprCaptures.push_back(Ref->getDecl()); |
| 9373 | if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { |
| 9374 | ExprResult RefRes = DefaultLvalueConversion(Ref); |
| 9375 | if (!RefRes.isUsable()) |
| 9376 | continue; |
| 9377 | ExprResult PostUpdateRes = |
| 9378 | BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, |
| 9379 | SimpleRefExpr, RefRes.get()); |
| 9380 | if (!PostUpdateRes.isUsable()) |
| 9381 | continue; |
| 9382 | ExprPostUpdates.push_back( |
| 9383 | IgnoredValueConversions(PostUpdateRes.get()).get()); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 9384 | } |
| 9385 | } |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 9386 | } |
| 9387 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); |
| 9388 | Vars.push_back(VarsExpr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9389 | Privates.push_back(PrivateDRE); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 9390 | LHSs.push_back(LHSDRE); |
| 9391 | RHSs.push_back(RHSDRE); |
| 9392 | ReductionOps.push_back(ReductionOp.get()); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9393 | } |
| 9394 | |
| 9395 | if (Vars.empty()) |
| 9396 | return nullptr; |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 9397 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9398 | return OMPReductionClause::Create( |
| 9399 | Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 9400 | ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 9401 | LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures), |
| 9402 | buildPostUpdate(*this, ExprPostUpdates)); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 9403 | } |
| 9404 | |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 9405 | bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, |
| 9406 | SourceLocation LinLoc) { |
| 9407 | if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || |
| 9408 | LinKind == OMPC_LINEAR_unknown) { |
| 9409 | Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; |
| 9410 | return true; |
| 9411 | } |
| 9412 | return false; |
| 9413 | } |
| 9414 | |
| 9415 | bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc, |
| 9416 | OpenMPLinearClauseKind LinKind, |
| 9417 | QualType Type) { |
| 9418 | auto *VD = dyn_cast_or_null<VarDecl>(D); |
| 9419 | // A variable must not have an incomplete type or a reference type. |
| 9420 | if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) |
| 9421 | return true; |
| 9422 | if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && |
| 9423 | !Type->isReferenceType()) { |
| 9424 | Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) |
| 9425 | << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); |
| 9426 | return true; |
| 9427 | } |
| 9428 | Type = Type.getNonReferenceType(); |
| 9429 | |
| 9430 | // A list item must not be const-qualified. |
| 9431 | if (Type.isConstant(Context)) { |
| 9432 | Diag(ELoc, diag::err_omp_const_variable) |
| 9433 | << getOpenMPClauseName(OMPC_linear); |
| 9434 | if (D) { |
| 9435 | bool IsDecl = |
| 9436 | !VD || |
| 9437 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 9438 | Diag(D->getLocation(), |
| 9439 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 9440 | << D; |
| 9441 | } |
| 9442 | return true; |
| 9443 | } |
| 9444 | |
| 9445 | // A list item must be of integral or pointer type. |
| 9446 | Type = Type.getUnqualifiedType().getCanonicalType(); |
| 9447 | const auto *Ty = Type.getTypePtrOrNull(); |
| 9448 | if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && |
| 9449 | !Ty->isPointerType())) { |
| 9450 | Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; |
| 9451 | if (D) { |
| 9452 | bool IsDecl = |
| 9453 | !VD || |
| 9454 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 9455 | Diag(D->getLocation(), |
| 9456 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 9457 | << D; |
| 9458 | } |
| 9459 | return true; |
| 9460 | } |
| 9461 | return false; |
| 9462 | } |
| 9463 | |
Alexey Bataev | 182227b | 2015-08-20 10:54:39 +0000 | [diff] [blame] | 9464 | OMPClause *Sema::ActOnOpenMPLinearClause( |
| 9465 | ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, |
| 9466 | SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, |
| 9467 | SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9468 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 9469 | SmallVector<Expr *, 8> Privates; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9470 | SmallVector<Expr *, 8> Inits; |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 9471 | SmallVector<Decl *, 4> ExprCaptures; |
| 9472 | SmallVector<Expr *, 4> ExprPostUpdates; |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 9473 | if (CheckOpenMPLinearModifier(LinKind, LinLoc)) |
Alexey Bataev | 182227b | 2015-08-20 10:54:39 +0000 | [diff] [blame] | 9474 | LinKind = OMPC_LINEAR_val; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9475 | for (auto &RefExpr : VarList) { |
| 9476 | assert(RefExpr && "NULL expr in OpenMP linear clause."); |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9477 | SourceLocation ELoc; |
| 9478 | SourceRange ERange; |
| 9479 | Expr *SimpleRefExpr = RefExpr; |
| 9480 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, |
| 9481 | /*AllowArraySection=*/false); |
| 9482 | if (Res.second) { |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9483 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9484 | Vars.push_back(RefExpr); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 9485 | Privates.push_back(nullptr); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9486 | Inits.push_back(nullptr); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9487 | } |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9488 | ValueDecl *D = Res.first; |
| 9489 | if (!D) |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9490 | continue; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9491 | |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9492 | QualType Type = D->getType(); |
| 9493 | auto *VD = dyn_cast<VarDecl>(D); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9494 | |
| 9495 | // OpenMP [2.14.3.7, linear clause] |
| 9496 | // A list-item cannot appear in more than one linear clause. |
| 9497 | // A list-item that appears in a linear clause cannot appear in any |
| 9498 | // other data-sharing attribute clause. |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9499 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9500 | if (DVar.RefExpr) { |
| 9501 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 9502 | << getOpenMPClauseName(OMPC_linear); |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9503 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9504 | continue; |
| 9505 | } |
| 9506 | |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 9507 | if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9508 | continue; |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 9509 | Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9510 | |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 9511 | // Build private copy of original var. |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9512 | auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 9513 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 9514 | auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9515 | // Build var to save initial value. |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9516 | VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 9517 | Expr *InitExpr; |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9518 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 9519 | if (!VD && !CurContext->isDependentContext()) { |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 9520 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); |
| 9521 | if (!IsOpenMPCapturedDecl(D)) { |
| 9522 | ExprCaptures.push_back(Ref->getDecl()); |
| 9523 | if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { |
| 9524 | ExprResult RefRes = DefaultLvalueConversion(Ref); |
| 9525 | if (!RefRes.isUsable()) |
| 9526 | continue; |
| 9527 | ExprResult PostUpdateRes = |
| 9528 | BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, |
| 9529 | SimpleRefExpr, RefRes.get()); |
| 9530 | if (!PostUpdateRes.isUsable()) |
| 9531 | continue; |
| 9532 | ExprPostUpdates.push_back( |
| 9533 | IgnoredValueConversions(PostUpdateRes.get()).get()); |
| 9534 | } |
| 9535 | } |
| 9536 | } |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 9537 | if (LinKind == OMPC_LINEAR_uval) |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9538 | InitExpr = VD ? VD->getInit() : SimpleRefExpr; |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 9539 | else |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9540 | InitExpr = VD ? SimpleRefExpr : Ref; |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 9541 | AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 9542 | /*DirectInit=*/false); |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 9543 | auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); |
| 9544 | |
| 9545 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 9546 | Vars.push_back((VD || CurContext->isDependentContext()) |
| 9547 | ? RefExpr->IgnoreParens() |
| 9548 | : Ref); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 9549 | Privates.push_back(PrivateRef); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9550 | Inits.push_back(InitRef); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9551 | } |
| 9552 | |
| 9553 | if (Vars.empty()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 9554 | return nullptr; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9555 | |
| 9556 | Expr *StepExpr = Step; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9557 | Expr *CalcStepExpr = nullptr; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9558 | if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && |
| 9559 | !Step->isInstantiationDependent() && |
| 9560 | !Step->containsUnexpandedParameterPack()) { |
| 9561 | SourceLocation StepLoc = Step->getLocStart(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 9562 | ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9563 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 9564 | return nullptr; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 9565 | StepExpr = Val.get(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9566 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9567 | // Build var to save the step value. |
| 9568 | VarDecl *SaveVar = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 9569 | buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9570 | ExprResult SaveRef = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 9571 | buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9572 | ExprResult CalcStep = |
| 9573 | BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 9574 | CalcStep = ActOnFinishFullExpr(CalcStep.get()); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9575 | |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9576 | // Warn about zero linear step (it would be probably better specified as |
| 9577 | // making corresponding variables 'const'). |
| 9578 | llvm::APSInt Result; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9579 | bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); |
| 9580 | if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9581 | Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] |
| 9582 | << (Vars.size() > 1); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9583 | if (!IsConstant && CalcStep.isUsable()) { |
| 9584 | // Calculate the step beforehand instead of doing this on each iteration. |
| 9585 | // (This is not used if the number of iterations may be kfold-ed). |
| 9586 | CalcStepExpr = CalcStep.get(); |
| 9587 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9588 | } |
| 9589 | |
Alexey Bataev | 182227b | 2015-08-20 10:54:39 +0000 | [diff] [blame] | 9590 | return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, |
| 9591 | ColonLoc, EndLoc, Vars, Privates, Inits, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 9592 | StepExpr, CalcStepExpr, |
| 9593 | buildPreInits(Context, ExprCaptures), |
| 9594 | buildPostUpdate(*this, ExprPostUpdates)); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9595 | } |
| 9596 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 9597 | static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, |
| 9598 | Expr *NumIterations, Sema &SemaRef, |
| 9599 | Scope *S, DSAStackTy *Stack) { |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9600 | // Walk the vars and build update/final expressions for the CodeGen. |
| 9601 | SmallVector<Expr *, 8> Updates; |
| 9602 | SmallVector<Expr *, 8> Finals; |
| 9603 | Expr *Step = Clause.getStep(); |
| 9604 | Expr *CalcStep = Clause.getCalcStep(); |
| 9605 | // OpenMP [2.14.3.7, linear clause] |
| 9606 | // If linear-step is not specified it is assumed to be 1. |
| 9607 | if (Step == nullptr) |
| 9608 | Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 9609 | else if (CalcStep) { |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9610 | Step = cast<BinaryOperator>(CalcStep)->getLHS(); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 9611 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9612 | bool HasErrors = false; |
| 9613 | auto CurInit = Clause.inits().begin(); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 9614 | auto CurPrivate = Clause.privates().begin(); |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 9615 | auto LinKind = Clause.getModifier(); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9616 | for (auto &RefExpr : Clause.varlists()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 9617 | SourceLocation ELoc; |
| 9618 | SourceRange ERange; |
| 9619 | Expr *SimpleRefExpr = RefExpr; |
| 9620 | auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange, |
| 9621 | /*AllowArraySection=*/false); |
| 9622 | ValueDecl *D = Res.first; |
| 9623 | if (Res.second || !D) { |
| 9624 | Updates.push_back(nullptr); |
| 9625 | Finals.push_back(nullptr); |
| 9626 | HasErrors = true; |
| 9627 | continue; |
| 9628 | } |
| 9629 | if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) { |
| 9630 | D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts()) |
| 9631 | ->getMemberDecl(); |
| 9632 | } |
| 9633 | auto &&Info = Stack->isLoopControlVariable(D); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9634 | Expr *InitExpr = *CurInit; |
| 9635 | |
| 9636 | // Build privatized reference to the current linear var. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 9637 | auto *DE = cast<DeclRefExpr>(SimpleRefExpr); |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 9638 | Expr *CapturedRef; |
| 9639 | if (LinKind == OMPC_LINEAR_uval) |
| 9640 | CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); |
| 9641 | else |
| 9642 | CapturedRef = |
| 9643 | buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), |
| 9644 | DE->getType().getUnqualifiedType(), DE->getExprLoc(), |
| 9645 | /*RefersToCapture=*/true); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9646 | |
| 9647 | // Build update: Var = InitExpr + IV * Step |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 9648 | ExprResult Update; |
| 9649 | if (!Info.first) { |
| 9650 | Update = |
| 9651 | BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, |
| 9652 | InitExpr, IV, Step, /* Subtract */ false); |
| 9653 | } else |
| 9654 | Update = *CurPrivate; |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 9655 | Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(), |
| 9656 | /*DiscardedValue=*/true); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9657 | |
| 9658 | // Build final: Var = InitExpr + NumIterations * Step |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 9659 | ExprResult Final; |
| 9660 | if (!Info.first) { |
| 9661 | Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, |
| 9662 | InitExpr, NumIterations, Step, |
| 9663 | /* Subtract */ false); |
| 9664 | } else |
| 9665 | Final = *CurPrivate; |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 9666 | Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(), |
| 9667 | /*DiscardedValue=*/true); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 9668 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9669 | if (!Update.isUsable() || !Final.isUsable()) { |
| 9670 | Updates.push_back(nullptr); |
| 9671 | Finals.push_back(nullptr); |
| 9672 | HasErrors = true; |
| 9673 | } else { |
| 9674 | Updates.push_back(Update.get()); |
| 9675 | Finals.push_back(Final.get()); |
| 9676 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 9677 | ++CurInit; |
| 9678 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9679 | } |
| 9680 | Clause.setUpdates(Updates); |
| 9681 | Clause.setFinals(Finals); |
| 9682 | return HasErrors; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9683 | } |
| 9684 | |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9685 | OMPClause *Sema::ActOnOpenMPAlignedClause( |
| 9686 | ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, |
| 9687 | SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { |
| 9688 | |
| 9689 | SmallVector<Expr *, 8> Vars; |
| 9690 | for (auto &RefExpr : VarList) { |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9691 | assert(RefExpr && "NULL expr in OpenMP linear clause."); |
| 9692 | SourceLocation ELoc; |
| 9693 | SourceRange ERange; |
| 9694 | Expr *SimpleRefExpr = RefExpr; |
| 9695 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, |
| 9696 | /*AllowArraySection=*/false); |
| 9697 | if (Res.second) { |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9698 | // It will be analyzed later. |
| 9699 | Vars.push_back(RefExpr); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9700 | } |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9701 | ValueDecl *D = Res.first; |
| 9702 | if (!D) |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9703 | continue; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9704 | |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9705 | QualType QType = D->getType(); |
| 9706 | auto *VD = dyn_cast<VarDecl>(D); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9707 | |
| 9708 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 9709 | // The type of list items appearing in the aligned clause must be |
| 9710 | // array, pointer, reference to array, or reference to pointer. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 9711 | QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9712 | const Type *Ty = QType.getTypePtrOrNull(); |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9713 | if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9714 | Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9715 | << QType << getLangOpts().CPlusPlus << ERange; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9716 | bool IsDecl = |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9717 | !VD || |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9718 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9719 | Diag(D->getLocation(), |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9720 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9721 | << D; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9722 | continue; |
| 9723 | } |
| 9724 | |
| 9725 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 9726 | // A list-item cannot appear in more than one aligned clause. |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9727 | if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 9728 | Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9729 | Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) |
| 9730 | << getOpenMPClauseName(OMPC_aligned); |
| 9731 | continue; |
| 9732 | } |
| 9733 | |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9734 | DeclRefExpr *Ref = nullptr; |
| 9735 | if (!VD && IsOpenMPCapturedDecl(D)) |
| 9736 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); |
| 9737 | Vars.push_back(DefaultFunctionArrayConversion( |
| 9738 | (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) |
| 9739 | .get()); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9740 | } |
| 9741 | |
| 9742 | // OpenMP [2.8.1, simd construct, Description] |
| 9743 | // The parameter of the aligned clause, alignment, must be a constant |
| 9744 | // positive integer expression. |
| 9745 | // If no optional parameter is specified, implementation-defined default |
| 9746 | // alignments for SIMD instructions on the target platforms are assumed. |
| 9747 | if (Alignment != nullptr) { |
| 9748 | ExprResult AlignResult = |
| 9749 | VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); |
| 9750 | if (AlignResult.isInvalid()) |
| 9751 | return nullptr; |
| 9752 | Alignment = AlignResult.get(); |
| 9753 | } |
| 9754 | if (Vars.empty()) |
| 9755 | return nullptr; |
| 9756 | |
| 9757 | return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, |
| 9758 | EndLoc, Vars, Alignment); |
| 9759 | } |
| 9760 | |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9761 | OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, |
| 9762 | SourceLocation StartLoc, |
| 9763 | SourceLocation LParenLoc, |
| 9764 | SourceLocation EndLoc) { |
| 9765 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9766 | SmallVector<Expr *, 8> SrcExprs; |
| 9767 | SmallVector<Expr *, 8> DstExprs; |
| 9768 | SmallVector<Expr *, 8> AssignmentOps; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9769 | for (auto &RefExpr : VarList) { |
| 9770 | assert(RefExpr && "NULL expr in OpenMP copyin clause."); |
| 9771 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9772 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9773 | Vars.push_back(RefExpr); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9774 | SrcExprs.push_back(nullptr); |
| 9775 | DstExprs.push_back(nullptr); |
| 9776 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9777 | continue; |
| 9778 | } |
| 9779 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9780 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9781 | // OpenMP [2.1, C/C++] |
| 9782 | // A list item is a variable name. |
| 9783 | // OpenMP [2.14.4.1, Restrictions, p.1] |
| 9784 | // A list item that appears in a copyin clause must be threadprivate. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9785 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9786 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 9787 | Diag(ELoc, diag::err_omp_expected_var_name_member_expr) |
| 9788 | << 0 << RefExpr->getSourceRange(); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9789 | continue; |
| 9790 | } |
| 9791 | |
| 9792 | Decl *D = DE->getDecl(); |
| 9793 | VarDecl *VD = cast<VarDecl>(D); |
| 9794 | |
| 9795 | QualType Type = VD->getType(); |
| 9796 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 9797 | // It will be analyzed later. |
| 9798 | Vars.push_back(DE); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9799 | SrcExprs.push_back(nullptr); |
| 9800 | DstExprs.push_back(nullptr); |
| 9801 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9802 | continue; |
| 9803 | } |
| 9804 | |
| 9805 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] |
| 9806 | // A list item that appears in a copyin clause must be threadprivate. |
| 9807 | if (!DSAStack->isThreadPrivate(VD)) { |
| 9808 | Diag(ELoc, diag::err_omp_required_access) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9809 | << getOpenMPClauseName(OMPC_copyin) |
| 9810 | << getOpenMPDirectiveName(OMPD_threadprivate); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9811 | continue; |
| 9812 | } |
| 9813 | |
| 9814 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] |
| 9815 | // A variable of class type (or array thereof) that appears in a |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 9816 | // copyin clause requires an accessible, unambiguous copy assignment |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9817 | // operator for the class type. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 9818 | auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 9819 | auto *SrcVD = |
| 9820 | buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(), |
| 9821 | ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 9822 | auto *PseudoSrcExpr = buildDeclRefExpr( |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 9823 | *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); |
| 9824 | auto *DstVD = |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 9825 | buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst", |
| 9826 | VD->hasAttrs() ? &VD->getAttrs() : nullptr); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9827 | auto *PseudoDstExpr = |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 9828 | buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9829 | // For arrays generate assignment operation for single element and replace |
| 9830 | // it by the original array element in CodeGen. |
| 9831 | auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, |
| 9832 | PseudoDstExpr, PseudoSrcExpr); |
| 9833 | if (AssignmentOp.isInvalid()) |
| 9834 | continue; |
| 9835 | AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), |
| 9836 | /*DiscardedValue=*/true); |
| 9837 | if (AssignmentOp.isInvalid()) |
| 9838 | continue; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9839 | |
| 9840 | DSAStack->addDSA(VD, DE, OMPC_copyin); |
| 9841 | Vars.push_back(DE); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9842 | SrcExprs.push_back(PseudoSrcExpr); |
| 9843 | DstExprs.push_back(PseudoDstExpr); |
| 9844 | AssignmentOps.push_back(AssignmentOp.get()); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9845 | } |
| 9846 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9847 | if (Vars.empty()) |
| 9848 | return nullptr; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9849 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9850 | return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, |
| 9851 | SrcExprs, DstExprs, AssignmentOps); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9852 | } |
| 9853 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9854 | OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, |
| 9855 | SourceLocation StartLoc, |
| 9856 | SourceLocation LParenLoc, |
| 9857 | SourceLocation EndLoc) { |
| 9858 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9859 | SmallVector<Expr *, 8> SrcExprs; |
| 9860 | SmallVector<Expr *, 8> DstExprs; |
| 9861 | SmallVector<Expr *, 8> AssignmentOps; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9862 | for (auto &RefExpr : VarList) { |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9863 | assert(RefExpr && "NULL expr in OpenMP linear clause."); |
| 9864 | SourceLocation ELoc; |
| 9865 | SourceRange ERange; |
| 9866 | Expr *SimpleRefExpr = RefExpr; |
| 9867 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, |
| 9868 | /*AllowArraySection=*/false); |
| 9869 | if (Res.second) { |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9870 | // It will be analyzed later. |
| 9871 | Vars.push_back(RefExpr); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9872 | SrcExprs.push_back(nullptr); |
| 9873 | DstExprs.push_back(nullptr); |
| 9874 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9875 | } |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9876 | ValueDecl *D = Res.first; |
| 9877 | if (!D) |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9878 | continue; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9879 | |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9880 | QualType Type = D->getType(); |
| 9881 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9882 | |
| 9883 | // OpenMP [2.14.4.2, Restrictions, p.2] |
| 9884 | // A list item that appears in a copyprivate clause may not appear in a |
| 9885 | // private or firstprivate clause on the single construct. |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9886 | if (!VD || !DSAStack->isThreadPrivate(VD)) { |
| 9887 | auto DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9888 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && |
| 9889 | DVar.RefExpr) { |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9890 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 9891 | << getOpenMPClauseName(DVar.CKind) |
| 9892 | << getOpenMPClauseName(OMPC_copyprivate); |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9893 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9894 | continue; |
| 9895 | } |
| 9896 | |
| 9897 | // OpenMP [2.11.4.2, Restrictions, p.1] |
| 9898 | // All list items that appear in a copyprivate clause must be either |
| 9899 | // threadprivate or private in the enclosing context. |
| 9900 | if (DVar.CKind == OMPC_unknown) { |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9901 | DVar = DSAStack->getImplicitDSA(D, false); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9902 | if (DVar.CKind == OMPC_shared) { |
| 9903 | Diag(ELoc, diag::err_omp_required_access) |
| 9904 | << getOpenMPClauseName(OMPC_copyprivate) |
| 9905 | << "threadprivate or private in the enclosing context"; |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9906 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9907 | continue; |
| 9908 | } |
| 9909 | } |
| 9910 | } |
| 9911 | |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9912 | // Variably modified types are not supported. |
Alexey Bataev | 5129d3a | 2015-05-21 09:47:46 +0000 | [diff] [blame] | 9913 | if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9914 | Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 9915 | << getOpenMPClauseName(OMPC_copyprivate) << Type |
| 9916 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9917 | bool IsDecl = |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9918 | !VD || |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9919 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9920 | Diag(D->getLocation(), |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9921 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9922 | << D; |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9923 | continue; |
| 9924 | } |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 9925 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9926 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] |
| 9927 | // A variable of class type (or array thereof) that appears in a |
| 9928 | // copyin clause requires an accessible, unambiguous copy assignment |
| 9929 | // operator for the class type. |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 9930 | Type = Context.getBaseElementType(Type.getNonReferenceType()) |
| 9931 | .getUnqualifiedType(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 9932 | auto *SrcVD = |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9933 | buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src", |
| 9934 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 9935 | auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 9936 | auto *DstVD = |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9937 | buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst", |
| 9938 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 9939 | auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9940 | auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9941 | PseudoDstExpr, PseudoSrcExpr); |
| 9942 | if (AssignmentOp.isInvalid()) |
| 9943 | continue; |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9944 | AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9945 | /*DiscardedValue=*/true); |
| 9946 | if (AssignmentOp.isInvalid()) |
| 9947 | continue; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9948 | |
| 9949 | // No need to mark vars as copyprivate, they are already threadprivate or |
| 9950 | // implicitly private. |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9951 | assert(VD || IsOpenMPCapturedDecl(D)); |
| 9952 | Vars.push_back( |
| 9953 | VD ? RefExpr->IgnoreParens() |
| 9954 | : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9955 | SrcExprs.push_back(PseudoSrcExpr); |
| 9956 | DstExprs.push_back(PseudoDstExpr); |
| 9957 | AssignmentOps.push_back(AssignmentOp.get()); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9958 | } |
| 9959 | |
| 9960 | if (Vars.empty()) |
| 9961 | return nullptr; |
| 9962 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9963 | return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 9964 | Vars, SrcExprs, DstExprs, AssignmentOps); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9965 | } |
| 9966 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 9967 | OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, |
| 9968 | SourceLocation StartLoc, |
| 9969 | SourceLocation LParenLoc, |
| 9970 | SourceLocation EndLoc) { |
| 9971 | if (VarList.empty()) |
| 9972 | return nullptr; |
| 9973 | |
| 9974 | return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); |
| 9975 | } |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 9976 | |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9977 | OMPClause * |
| 9978 | Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, |
| 9979 | SourceLocation DepLoc, SourceLocation ColonLoc, |
| 9980 | ArrayRef<Expr *> VarList, SourceLocation StartLoc, |
| 9981 | SourceLocation LParenLoc, SourceLocation EndLoc) { |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 9982 | if (DSAStack->getCurrentDirective() == OMPD_ordered && |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9983 | DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 9984 | Diag(DepLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 9985 | << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 9986 | return nullptr; |
| 9987 | } |
| 9988 | if (DSAStack->getCurrentDirective() != OMPD_ordered && |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9989 | (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || |
| 9990 | DepKind == OMPC_DEPEND_sink)) { |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 9991 | unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9992 | Diag(DepLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 9993 | << getListOfPossibleValues(OMPC_depend, /*First=*/0, |
| 9994 | /*Last=*/OMPC_DEPEND_unknown, Except) |
| 9995 | << getOpenMPClauseName(OMPC_depend); |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9996 | return nullptr; |
| 9997 | } |
| 9998 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9999 | DSAStackTy::OperatorOffsetTy OpsOffs; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 10000 | llvm::APSInt DepCounter(/*BitWidth=*/32); |
| 10001 | llvm::APSInt TotalDepCount(/*BitWidth=*/32); |
| 10002 | if (DepKind == OMPC_DEPEND_sink) { |
| 10003 | if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) { |
| 10004 | TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); |
| 10005 | TotalDepCount.setIsUnsigned(/*Val=*/true); |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 10006 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 10007 | } |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 10008 | if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) || |
| 10009 | DSAStack->getParentOrderedRegionParam()) { |
| 10010 | for (auto &RefExpr : VarList) { |
| 10011 | assert(RefExpr && "NULL expr in OpenMP shared clause."); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10012 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 10013 | // It will be analyzed later. |
| 10014 | Vars.push_back(RefExpr); |
| 10015 | continue; |
| 10016 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 10017 | |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 10018 | SourceLocation ELoc = RefExpr->getExprLoc(); |
| 10019 | auto *SimpleExpr = RefExpr->IgnoreParenCasts(); |
| 10020 | if (DepKind == OMPC_DEPEND_sink) { |
| 10021 | if (DepCounter >= TotalDepCount) { |
| 10022 | Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); |
| 10023 | continue; |
| 10024 | } |
| 10025 | ++DepCounter; |
| 10026 | // OpenMP [2.13.9, Summary] |
| 10027 | // depend(dependence-type : vec), where dependence-type is: |
| 10028 | // 'sink' and where vec is the iteration vector, which has the form: |
| 10029 | // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] |
| 10030 | // where n is the value specified by the ordered clause in the loop |
| 10031 | // directive, xi denotes the loop iteration variable of the i-th nested |
| 10032 | // loop associated with the loop directive, and di is a constant |
| 10033 | // non-negative integer. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10034 | if (CurContext->isDependentContext()) { |
| 10035 | // It will be analyzed later. |
| 10036 | Vars.push_back(RefExpr); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 10037 | continue; |
| 10038 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10039 | SimpleExpr = SimpleExpr->IgnoreImplicit(); |
| 10040 | OverloadedOperatorKind OOK = OO_None; |
| 10041 | SourceLocation OOLoc; |
| 10042 | Expr *LHS = SimpleExpr; |
| 10043 | Expr *RHS = nullptr; |
| 10044 | if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { |
| 10045 | OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); |
| 10046 | OOLoc = BO->getOperatorLoc(); |
| 10047 | LHS = BO->getLHS()->IgnoreParenImpCasts(); |
| 10048 | RHS = BO->getRHS()->IgnoreParenImpCasts(); |
| 10049 | } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { |
| 10050 | OOK = OCE->getOperator(); |
| 10051 | OOLoc = OCE->getOperatorLoc(); |
| 10052 | LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); |
| 10053 | RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); |
| 10054 | } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { |
| 10055 | OOK = MCE->getMethodDecl() |
| 10056 | ->getNameInfo() |
| 10057 | .getName() |
| 10058 | .getCXXOverloadedOperator(); |
| 10059 | OOLoc = MCE->getCallee()->getExprLoc(); |
| 10060 | LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); |
| 10061 | RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); |
| 10062 | } |
| 10063 | SourceLocation ELoc; |
| 10064 | SourceRange ERange; |
| 10065 | auto Res = getPrivateItem(*this, LHS, ELoc, ERange, |
| 10066 | /*AllowArraySection=*/false); |
| 10067 | if (Res.second) { |
| 10068 | // It will be analyzed later. |
| 10069 | Vars.push_back(RefExpr); |
| 10070 | } |
| 10071 | ValueDecl *D = Res.first; |
| 10072 | if (!D) |
| 10073 | continue; |
| 10074 | |
| 10075 | if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { |
| 10076 | Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); |
| 10077 | continue; |
| 10078 | } |
| 10079 | if (RHS) { |
| 10080 | ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( |
| 10081 | RHS, OMPC_depend, /*StrictlyPositive=*/false); |
| 10082 | if (RHSRes.isInvalid()) |
| 10083 | continue; |
| 10084 | } |
| 10085 | if (!CurContext->isDependentContext() && |
| 10086 | DSAStack->getParentOrderedRegionParam() && |
| 10087 | DepCounter != DSAStack->isParentLoopControlVariable(D).first) { |
| 10088 | Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) |
| 10089 | << DSAStack->getParentLoopControlVariable( |
| 10090 | DepCounter.getZExtValue()); |
| 10091 | continue; |
| 10092 | } |
| 10093 | OpsOffs.push_back({RHS, OOK}); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 10094 | } else { |
| 10095 | // OpenMP [2.11.1.1, Restrictions, p.3] |
| 10096 | // A variable that is part of another variable (such as a field of a |
| 10097 | // structure) but is not an array element or an array section cannot |
| 10098 | // appear in a depend clause. |
| 10099 | auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); |
| 10100 | auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); |
| 10101 | auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); |
| 10102 | if (!RefExpr->IgnoreParenImpCasts()->isLValue() || |
| 10103 | (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) || |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 10104 | (ASE && |
| 10105 | !ASE->getBase() |
| 10106 | ->getType() |
| 10107 | .getNonReferenceType() |
| 10108 | ->isPointerType() && |
| 10109 | !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 10110 | Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item) |
| 10111 | << 0 << RefExpr->getSourceRange(); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 10112 | continue; |
| 10113 | } |
| 10114 | } |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 10115 | Vars.push_back(RefExpr->IgnoreParenImpCasts()); |
| 10116 | } |
| 10117 | |
| 10118 | if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && |
| 10119 | TotalDepCount > VarList.size() && |
| 10120 | DSAStack->getParentOrderedRegionParam()) { |
| 10121 | Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) |
| 10122 | << DSAStack->getParentLoopControlVariable(VarList.size() + 1); |
| 10123 | } |
| 10124 | if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && |
| 10125 | Vars.empty()) |
| 10126 | return nullptr; |
| 10127 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10128 | auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 10129 | DepKind, DepLoc, ColonLoc, Vars); |
| 10130 | if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) |
| 10131 | DSAStack->addDoacrossDependClause(C, OpsOffs); |
| 10132 | return C; |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 10133 | } |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 10134 | |
| 10135 | OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, |
| 10136 | SourceLocation LParenLoc, |
| 10137 | SourceLocation EndLoc) { |
| 10138 | Expr *ValExpr = Device; |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 10139 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 10140 | // OpenMP [2.9.1, Restrictions] |
| 10141 | // The device expression must evaluate to a non-negative integer value. |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 10142 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device, |
| 10143 | /*StrictlyPositive=*/false)) |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 10144 | return nullptr; |
| 10145 | |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 10146 | return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 10147 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10148 | |
| 10149 | static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc, |
| 10150 | DSAStackTy *Stack, CXXRecordDecl *RD) { |
| 10151 | if (!RD || RD->isInvalidDecl()) |
| 10152 | return true; |
| 10153 | |
| 10154 | auto QTy = SemaRef.Context.getRecordType(RD); |
| 10155 | if (RD->isDynamicClass()) { |
| 10156 | SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; |
| 10157 | SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target); |
| 10158 | return false; |
| 10159 | } |
| 10160 | auto *DC = RD; |
| 10161 | bool IsCorrect = true; |
| 10162 | for (auto *I : DC->decls()) { |
| 10163 | if (I) { |
| 10164 | if (auto *MD = dyn_cast<CXXMethodDecl>(I)) { |
| 10165 | if (MD->isStatic()) { |
| 10166 | SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; |
| 10167 | SemaRef.Diag(MD->getLocation(), |
| 10168 | diag::note_omp_static_member_in_target); |
| 10169 | IsCorrect = false; |
| 10170 | } |
| 10171 | } else if (auto *VD = dyn_cast<VarDecl>(I)) { |
| 10172 | if (VD->isStaticDataMember()) { |
| 10173 | SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; |
| 10174 | SemaRef.Diag(VD->getLocation(), |
| 10175 | diag::note_omp_static_member_in_target); |
| 10176 | IsCorrect = false; |
| 10177 | } |
| 10178 | } |
| 10179 | } |
| 10180 | } |
| 10181 | |
| 10182 | for (auto &I : RD->bases()) { |
| 10183 | if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack, |
| 10184 | I.getType()->getAsCXXRecordDecl())) |
| 10185 | IsCorrect = false; |
| 10186 | } |
| 10187 | return IsCorrect; |
| 10188 | } |
| 10189 | |
| 10190 | static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, |
| 10191 | DSAStackTy *Stack, QualType QTy) { |
| 10192 | NamedDecl *ND; |
| 10193 | if (QTy->isIncompleteType(&ND)) { |
| 10194 | SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; |
| 10195 | return false; |
| 10196 | } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10197 | if (!RD->isInvalidDecl() && !IsCXXRecordForMappable(SemaRef, SL, Stack, RD)) |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10198 | return false; |
| 10199 | } |
| 10200 | return true; |
| 10201 | } |
| 10202 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10203 | /// \brief Return true if it can be proven that the provided array expression |
| 10204 | /// (array section or array subscript) does NOT specify the whole size of the |
| 10205 | /// array whose base type is \a BaseQTy. |
| 10206 | static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, |
| 10207 | const Expr *E, |
| 10208 | QualType BaseQTy) { |
| 10209 | auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
| 10210 | |
| 10211 | // If this is an array subscript, it refers to the whole size if the size of |
| 10212 | // the dimension is constant and equals 1. Also, an array section assumes the |
| 10213 | // format of an array subscript if no colon is used. |
| 10214 | if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { |
| 10215 | if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
| 10216 | return ATy->getSize().getSExtValue() != 1; |
| 10217 | // Size can't be evaluated statically. |
| 10218 | return false; |
| 10219 | } |
| 10220 | |
| 10221 | assert(OASE && "Expecting array section if not an array subscript."); |
| 10222 | auto *LowerBound = OASE->getLowerBound(); |
| 10223 | auto *Length = OASE->getLength(); |
| 10224 | |
| 10225 | // If there is a lower bound that does not evaluates to zero, we are not |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10226 | // covering the whole dimension. |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10227 | if (LowerBound) { |
| 10228 | llvm::APSInt ConstLowerBound; |
| 10229 | if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext())) |
| 10230 | return false; // Can't get the integer value as a constant. |
| 10231 | if (ConstLowerBound.getSExtValue()) |
| 10232 | return true; |
| 10233 | } |
| 10234 | |
| 10235 | // If we don't have a length we covering the whole dimension. |
| 10236 | if (!Length) |
| 10237 | return false; |
| 10238 | |
| 10239 | // If the base is a pointer, we don't have a way to get the size of the |
| 10240 | // pointee. |
| 10241 | if (BaseQTy->isPointerType()) |
| 10242 | return false; |
| 10243 | |
| 10244 | // We can only check if the length is the same as the size of the dimension |
| 10245 | // if we have a constant array. |
| 10246 | auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); |
| 10247 | if (!CATy) |
| 10248 | return false; |
| 10249 | |
| 10250 | llvm::APSInt ConstLength; |
| 10251 | if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) |
| 10252 | return false; // Can't get the integer value as a constant. |
| 10253 | |
| 10254 | return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); |
| 10255 | } |
| 10256 | |
| 10257 | // Return true if it can be proven that the provided array expression (array |
| 10258 | // section or array subscript) does NOT specify a single element of the array |
| 10259 | // whose base type is \a BaseQTy. |
| 10260 | static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10261 | const Expr *E, |
| 10262 | QualType BaseQTy) { |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10263 | auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
| 10264 | |
| 10265 | // An array subscript always refer to a single element. Also, an array section |
| 10266 | // assumes the format of an array subscript if no colon is used. |
| 10267 | if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) |
| 10268 | return false; |
| 10269 | |
| 10270 | assert(OASE && "Expecting array section if not an array subscript."); |
| 10271 | auto *Length = OASE->getLength(); |
| 10272 | |
| 10273 | // If we don't have a length we have to check if the array has unitary size |
| 10274 | // for this dimension. Also, we should always expect a length if the base type |
| 10275 | // is pointer. |
| 10276 | if (!Length) { |
| 10277 | if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
| 10278 | return ATy->getSize().getSExtValue() != 1; |
| 10279 | // We cannot assume anything. |
| 10280 | return false; |
| 10281 | } |
| 10282 | |
| 10283 | // Check if the length evaluates to 1. |
| 10284 | llvm::APSInt ConstLength; |
| 10285 | if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) |
| 10286 | return false; // Can't get the integer value as a constant. |
| 10287 | |
| 10288 | return ConstLength.getSExtValue() != 1; |
| 10289 | } |
| 10290 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10291 | // Return the expression of the base of the mappable expression or null if it |
| 10292 | // cannot be determined and do all the necessary checks to see if the expression |
| 10293 | // is valid as a standalone mappable expression. In the process, record all the |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10294 | // components of the expression. |
| 10295 | static Expr *CheckMapClauseExpressionBase( |
| 10296 | Sema &SemaRef, Expr *E, |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10297 | OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, |
| 10298 | OpenMPClauseKind CKind) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10299 | SourceLocation ELoc = E->getExprLoc(); |
| 10300 | SourceRange ERange = E->getSourceRange(); |
| 10301 | |
| 10302 | // The base of elements of list in a map clause have to be either: |
| 10303 | // - a reference to variable or field. |
| 10304 | // - a member expression. |
| 10305 | // - an array expression. |
| 10306 | // |
| 10307 | // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the |
| 10308 | // reference to 'r'. |
| 10309 | // |
| 10310 | // If we have: |
| 10311 | // |
| 10312 | // struct SS { |
| 10313 | // Bla S; |
| 10314 | // foo() { |
| 10315 | // #pragma omp target map (S.Arr[:12]); |
| 10316 | // } |
| 10317 | // } |
| 10318 | // |
| 10319 | // We want to retrieve the member expression 'this->S'; |
| 10320 | |
| 10321 | Expr *RelevantExpr = nullptr; |
| 10322 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10323 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] |
| 10324 | // If a list item is an array section, it must specify contiguous storage. |
| 10325 | // |
| 10326 | // For this restriction it is sufficient that we make sure only references |
| 10327 | // to variables or fields and array expressions, and that no array sections |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10328 | // exist except in the rightmost expression (unless they cover the whole |
| 10329 | // dimension of the array). E.g. these would be invalid: |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10330 | // |
| 10331 | // r.ArrS[3:5].Arr[6:7] |
| 10332 | // |
| 10333 | // r.ArrS[3:5].x |
| 10334 | // |
| 10335 | // but these would be valid: |
| 10336 | // r.ArrS[3].Arr[6:7] |
| 10337 | // |
| 10338 | // r.ArrS[3].x |
| 10339 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10340 | bool AllowUnitySizeArraySection = true; |
| 10341 | bool AllowWholeSizeArraySection = true; |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10342 | |
Dmitry Polukhin | 644a925 | 2016-03-11 07:58:34 +0000 | [diff] [blame] | 10343 | while (!RelevantExpr) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10344 | E = E->IgnoreParenImpCasts(); |
| 10345 | |
| 10346 | if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { |
| 10347 | if (!isa<VarDecl>(CurE->getDecl())) |
| 10348 | break; |
| 10349 | |
| 10350 | RelevantExpr = CurE; |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10351 | |
| 10352 | // If we got a reference to a declaration, we should not expect any array |
| 10353 | // section before that. |
| 10354 | AllowUnitySizeArraySection = false; |
| 10355 | AllowWholeSizeArraySection = false; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10356 | |
| 10357 | // Record the component. |
| 10358 | CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent( |
| 10359 | CurE, CurE->getDecl())); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10360 | continue; |
| 10361 | } |
| 10362 | |
| 10363 | if (auto *CurE = dyn_cast<MemberExpr>(E)) { |
| 10364 | auto *BaseE = CurE->getBase()->IgnoreParenImpCasts(); |
| 10365 | |
| 10366 | if (isa<CXXThisExpr>(BaseE)) |
| 10367 | // We found a base expression: this->Val. |
| 10368 | RelevantExpr = CurE; |
| 10369 | else |
| 10370 | E = BaseE; |
| 10371 | |
| 10372 | if (!isa<FieldDecl>(CurE->getMemberDecl())) { |
| 10373 | SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) |
| 10374 | << CurE->getSourceRange(); |
| 10375 | break; |
| 10376 | } |
| 10377 | |
| 10378 | auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); |
| 10379 | |
| 10380 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] |
| 10381 | // A bit-field cannot appear in a map clause. |
| 10382 | // |
| 10383 | if (FD->isBitField()) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10384 | SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) |
| 10385 | << CurE->getSourceRange() << getOpenMPClauseName(CKind); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10386 | break; |
| 10387 | } |
| 10388 | |
| 10389 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] |
| 10390 | // If the type of a list item is a reference to a type T then the type |
| 10391 | // will be considered to be T for all purposes of this clause. |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10392 | QualType CurType = BaseE->getType().getNonReferenceType(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10393 | |
| 10394 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] |
| 10395 | // A list item cannot be a variable that is a member of a structure with |
| 10396 | // a union type. |
| 10397 | // |
| 10398 | if (auto *RT = CurType->getAs<RecordType>()) |
| 10399 | if (RT->isUnionType()) { |
| 10400 | SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) |
| 10401 | << CurE->getSourceRange(); |
| 10402 | break; |
| 10403 | } |
| 10404 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10405 | // If we got a member expression, we should not expect any array section |
| 10406 | // before that: |
| 10407 | // |
| 10408 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] |
| 10409 | // If a list item is an element of a structure, only the rightmost symbol |
| 10410 | // of the variable reference can be an array section. |
| 10411 | // |
| 10412 | AllowUnitySizeArraySection = false; |
| 10413 | AllowWholeSizeArraySection = false; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10414 | |
| 10415 | // Record the component. |
| 10416 | CurComponents.push_back( |
| 10417 | OMPClauseMappableExprCommon::MappableComponent(CurE, FD)); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10418 | continue; |
| 10419 | } |
| 10420 | |
| 10421 | if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 10422 | E = CurE->getBase()->IgnoreParenImpCasts(); |
| 10423 | |
| 10424 | if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { |
| 10425 | SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) |
| 10426 | << 0 << CurE->getSourceRange(); |
| 10427 | break; |
| 10428 | } |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10429 | |
| 10430 | // If we got an array subscript that express the whole dimension we |
| 10431 | // can have any array expressions before. If it only expressing part of |
| 10432 | // the dimension, we can only have unitary-size array expressions. |
| 10433 | if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, |
| 10434 | E->getType())) |
| 10435 | AllowWholeSizeArraySection = false; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10436 | |
| 10437 | // Record the component - we don't have any declaration associated. |
| 10438 | CurComponents.push_back( |
| 10439 | OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10440 | continue; |
| 10441 | } |
| 10442 | |
| 10443 | if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10444 | E = CurE->getBase()->IgnoreParenImpCasts(); |
| 10445 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10446 | auto CurType = |
| 10447 | OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); |
| 10448 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10449 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] |
| 10450 | // If the type of a list item is a reference to a type T then the type |
| 10451 | // will be considered to be T for all purposes of this clause. |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10452 | if (CurType->isReferenceType()) |
| 10453 | CurType = CurType->getPointeeType(); |
| 10454 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10455 | bool IsPointer = CurType->isAnyPointerType(); |
| 10456 | |
| 10457 | if (!IsPointer && !CurType->isArrayType()) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10458 | SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) |
| 10459 | << 0 << CurE->getSourceRange(); |
| 10460 | break; |
| 10461 | } |
| 10462 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10463 | bool NotWhole = |
| 10464 | CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); |
| 10465 | bool NotUnity = |
| 10466 | CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); |
| 10467 | |
Samuel Antao | dab51bb | 2016-07-18 23:22:11 +0000 | [diff] [blame] | 10468 | if (AllowWholeSizeArraySection) { |
| 10469 | // Any array section is currently allowed. Allowing a whole size array |
| 10470 | // section implies allowing a unity array section as well. |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10471 | // |
| 10472 | // If this array section refers to the whole dimension we can still |
| 10473 | // accept other array sections before this one, except if the base is a |
| 10474 | // pointer. Otherwise, only unitary sections are accepted. |
| 10475 | if (NotWhole || IsPointer) |
| 10476 | AllowWholeSizeArraySection = false; |
Samuel Antao | dab51bb | 2016-07-18 23:22:11 +0000 | [diff] [blame] | 10477 | } else if (AllowUnitySizeArraySection && NotUnity) { |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 10478 | // A unity or whole array section is not allowed and that is not |
| 10479 | // compatible with the properties of the current array section. |
| 10480 | SemaRef.Diag( |
| 10481 | ELoc, diag::err_array_section_does_not_specify_contiguous_storage) |
| 10482 | << CurE->getSourceRange(); |
| 10483 | break; |
| 10484 | } |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10485 | |
| 10486 | // Record the component - we don't have any declaration associated. |
| 10487 | CurComponents.push_back( |
| 10488 | OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10489 | continue; |
| 10490 | } |
| 10491 | |
| 10492 | // If nothing else worked, this is not a valid map clause expression. |
| 10493 | SemaRef.Diag(ELoc, |
| 10494 | diag::err_omp_expected_named_var_member_or_array_expression) |
| 10495 | << ERange; |
| 10496 | break; |
| 10497 | } |
| 10498 | |
| 10499 | return RelevantExpr; |
| 10500 | } |
| 10501 | |
| 10502 | // Return true if expression E associated with value VD has conflicts with other |
| 10503 | // map information. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10504 | static bool CheckMapConflicts( |
| 10505 | Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E, |
| 10506 | bool CurrentRegionOnly, |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10507 | OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, |
| 10508 | OpenMPClauseKind CKind) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10509 | assert(VD && E); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10510 | SourceLocation ELoc = E->getExprLoc(); |
| 10511 | SourceRange ERange = E->getSourceRange(); |
| 10512 | |
| 10513 | // In order to easily check the conflicts we need to match each component of |
| 10514 | // the expression under test with the components of the expressions that are |
| 10515 | // already in the stack. |
| 10516 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10517 | assert(!CurComponents.empty() && "Map clause expression with no components!"); |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10518 | assert(CurComponents.back().getAssociatedDeclaration() == VD && |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10519 | "Map clause expression with unexpected base!"); |
| 10520 | |
| 10521 | // Variables to help detecting enclosing problems in data environment nests. |
| 10522 | bool IsEnclosedByDataEnvironmentExpr = false; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10523 | const Expr *EnclosingExpr = nullptr; |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10524 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10525 | bool FoundError = DSAS->checkMappableExprComponentListsForDecl( |
| 10526 | VD, CurrentRegionOnly, |
| 10527 | [&](OMPClauseMappableExprCommon::MappableExprComponentListRef |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 10528 | StackComponents, |
| 10529 | OpenMPClauseKind) -> bool { |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10530 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10531 | assert(!StackComponents.empty() && |
| 10532 | "Map clause expression with no components!"); |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10533 | assert(StackComponents.back().getAssociatedDeclaration() == VD && |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10534 | "Map clause expression with unexpected base!"); |
| 10535 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10536 | // The whole expression in the stack. |
| 10537 | auto *RE = StackComponents.front().getAssociatedExpression(); |
| 10538 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10539 | // Expressions must start from the same base. Here we detect at which |
| 10540 | // point both expressions diverge from each other and see if we can |
| 10541 | // detect if the memory referred to both expressions is contiguous and |
| 10542 | // do not overlap. |
| 10543 | auto CI = CurComponents.rbegin(); |
| 10544 | auto CE = CurComponents.rend(); |
| 10545 | auto SI = StackComponents.rbegin(); |
| 10546 | auto SE = StackComponents.rend(); |
| 10547 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 10548 | |
| 10549 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] |
| 10550 | // At most one list item can be an array item derived from a given |
| 10551 | // variable in map clauses of the same construct. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10552 | if (CurrentRegionOnly && |
| 10553 | (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || |
| 10554 | isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && |
| 10555 | (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || |
| 10556 | isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { |
| 10557 | SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10558 | diag::err_omp_multiple_array_items_in_map_clause) |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10559 | << CI->getAssociatedExpression()->getSourceRange(); |
| 10560 | SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), |
| 10561 | diag::note_used_here) |
| 10562 | << SI->getAssociatedExpression()->getSourceRange(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10563 | return true; |
| 10564 | } |
| 10565 | |
| 10566 | // Do both expressions have the same kind? |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10567 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 10568 | SI->getAssociatedExpression()->getStmtClass()) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10569 | break; |
| 10570 | |
| 10571 | // Are we dealing with different variables/fields? |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10572 | if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10573 | break; |
| 10574 | } |
Kelvin Li | 9f645ae | 2016-07-18 22:49:16 +0000 | [diff] [blame] | 10575 | // Check if the extra components of the expressions in the enclosing |
| 10576 | // data environment are redundant for the current base declaration. |
| 10577 | // If they are, the maps completely overlap, which is legal. |
| 10578 | for (; SI != SE; ++SI) { |
| 10579 | QualType Type; |
| 10580 | if (auto *ASE = |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10581 | dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { |
Kelvin Li | 9f645ae | 2016-07-18 22:49:16 +0000 | [diff] [blame] | 10582 | Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10583 | } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>( |
| 10584 | SI->getAssociatedExpression())) { |
Kelvin Li | 9f645ae | 2016-07-18 22:49:16 +0000 | [diff] [blame] | 10585 | auto *E = OASE->getBase()->IgnoreParenImpCasts(); |
| 10586 | Type = |
| 10587 | OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); |
| 10588 | } |
| 10589 | if (Type.isNull() || Type->isAnyPointerType() || |
| 10590 | CheckArrayExpressionDoesNotReferToWholeSize( |
| 10591 | SemaRef, SI->getAssociatedExpression(), Type)) |
| 10592 | break; |
| 10593 | } |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10594 | |
| 10595 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] |
| 10596 | // List items of map clauses in the same construct must not share |
| 10597 | // original storage. |
| 10598 | // |
| 10599 | // If the expressions are exactly the same or one is a subset of the |
| 10600 | // other, it means they are sharing storage. |
| 10601 | if (CI == CE && SI == SE) { |
| 10602 | if (CurrentRegionOnly) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10603 | if (CKind == OMPC_map) |
| 10604 | SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; |
| 10605 | else { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 10606 | assert(CKind == OMPC_to || CKind == OMPC_from); |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10607 | SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) |
| 10608 | << ERange; |
| 10609 | } |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10610 | SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) |
| 10611 | << RE->getSourceRange(); |
| 10612 | return true; |
| 10613 | } else { |
| 10614 | // If we find the same expression in the enclosing data environment, |
| 10615 | // that is legal. |
| 10616 | IsEnclosedByDataEnvironmentExpr = true; |
| 10617 | return false; |
| 10618 | } |
| 10619 | } |
| 10620 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10621 | QualType DerivedType = |
| 10622 | std::prev(CI)->getAssociatedDeclaration()->getType(); |
| 10623 | SourceLocation DerivedLoc = |
| 10624 | std::prev(CI)->getAssociatedExpression()->getExprLoc(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10625 | |
| 10626 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] |
| 10627 | // If the type of a list item is a reference to a type T then the type |
| 10628 | // will be considered to be T for all purposes of this clause. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10629 | DerivedType = DerivedType.getNonReferenceType(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10630 | |
| 10631 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] |
| 10632 | // A variable for which the type is pointer and an array section |
| 10633 | // derived from that variable must not appear as list items of map |
| 10634 | // clauses of the same construct. |
| 10635 | // |
| 10636 | // Also, cover one of the cases in: |
| 10637 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] |
| 10638 | // If any part of the original storage of a list item has corresponding |
| 10639 | // storage in the device data environment, all of the original storage |
| 10640 | // must have corresponding storage in the device data environment. |
| 10641 | // |
| 10642 | if (DerivedType->isAnyPointerType()) { |
| 10643 | if (CI == CE || SI == SE) { |
| 10644 | SemaRef.Diag( |
| 10645 | DerivedLoc, |
| 10646 | diag::err_omp_pointer_mapped_along_with_derived_section) |
| 10647 | << DerivedLoc; |
| 10648 | } else { |
| 10649 | assert(CI != CE && SI != SE); |
| 10650 | SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced) |
| 10651 | << DerivedLoc; |
| 10652 | } |
| 10653 | SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) |
| 10654 | << RE->getSourceRange(); |
| 10655 | return true; |
| 10656 | } |
| 10657 | |
| 10658 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] |
| 10659 | // List items of map clauses in the same construct must not share |
| 10660 | // original storage. |
| 10661 | // |
| 10662 | // An expression is a subset of the other. |
| 10663 | if (CurrentRegionOnly && (CI == CE || SI == SE)) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10664 | if (CKind == OMPC_map) |
| 10665 | SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; |
| 10666 | else { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 10667 | assert(CKind == OMPC_to || CKind == OMPC_from); |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10668 | SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) |
| 10669 | << ERange; |
| 10670 | } |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10671 | SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) |
| 10672 | << RE->getSourceRange(); |
| 10673 | return true; |
| 10674 | } |
| 10675 | |
| 10676 | // The current expression uses the same base as other expression in the |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10677 | // data environment but does not contain it completely. |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10678 | if (!CurrentRegionOnly && SI != SE) |
| 10679 | EnclosingExpr = RE; |
| 10680 | |
| 10681 | // The current expression is a subset of the expression in the data |
| 10682 | // environment. |
| 10683 | IsEnclosedByDataEnvironmentExpr |= |
| 10684 | (!CurrentRegionOnly && CI != CE && SI == SE); |
| 10685 | |
| 10686 | return false; |
| 10687 | }); |
| 10688 | |
| 10689 | if (CurrentRegionOnly) |
| 10690 | return FoundError; |
| 10691 | |
| 10692 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] |
| 10693 | // If any part of the original storage of a list item has corresponding |
| 10694 | // storage in the device data environment, all of the original storage must |
| 10695 | // have corresponding storage in the device data environment. |
| 10696 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] |
| 10697 | // If a list item is an element of a structure, and a different element of |
| 10698 | // the structure has a corresponding list item in the device data environment |
| 10699 | // prior to a task encountering the construct associated with the map clause, |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10700 | // then the list item must also have a corresponding list item in the device |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10701 | // data environment prior to the task encountering the construct. |
| 10702 | // |
| 10703 | if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { |
| 10704 | SemaRef.Diag(ELoc, |
| 10705 | diag::err_omp_original_storage_is_shared_and_does_not_contain) |
| 10706 | << ERange; |
| 10707 | SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) |
| 10708 | << EnclosingExpr->getSourceRange(); |
| 10709 | return true; |
| 10710 | } |
| 10711 | |
| 10712 | return FoundError; |
| 10713 | } |
| 10714 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10715 | namespace { |
| 10716 | // Utility struct that gathers all the related lists associated with a mappable |
| 10717 | // expression. |
| 10718 | struct MappableVarListInfo final { |
| 10719 | // The list of expressions. |
| 10720 | ArrayRef<Expr *> VarList; |
| 10721 | // The list of processed expressions. |
| 10722 | SmallVector<Expr *, 16> ProcessedVarList; |
| 10723 | // The mappble components for each expression. |
| 10724 | OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; |
| 10725 | // The base declaration of the variable. |
| 10726 | SmallVector<ValueDecl *, 16> VarBaseDeclarations; |
| 10727 | |
| 10728 | MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { |
| 10729 | // We have a list of components and base declarations for each entry in the |
| 10730 | // variable list. |
| 10731 | VarComponents.reserve(VarList.size()); |
| 10732 | VarBaseDeclarations.reserve(VarList.size()); |
| 10733 | } |
| 10734 | }; |
| 10735 | } |
| 10736 | |
| 10737 | // Check the validity of the provided variable list for the provided clause kind |
| 10738 | // \a CKind. In the check process the valid expressions, and mappable expression |
| 10739 | // components and variables are extracted and used to fill \a Vars, |
| 10740 | // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and |
| 10741 | // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'. |
| 10742 | static void |
| 10743 | checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS, |
| 10744 | OpenMPClauseKind CKind, MappableVarListInfo &MVLI, |
| 10745 | SourceLocation StartLoc, |
| 10746 | OpenMPMapClauseKind MapType = OMPC_MAP_unknown, |
| 10747 | bool IsMapTypeImplicit = false) { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 10748 | // We only expect mappable expressions in 'to', 'from', and 'map' clauses. |
| 10749 | assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10750 | "Unexpected clause kind with mappable expressions!"); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10751 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10752 | // Keep track of the mappable components and base declarations in this clause. |
| 10753 | // Each entry in the list is going to have a list of components associated. We |
| 10754 | // record each set of the components so that we can build the clause later on. |
| 10755 | // In the end we should have the same amount of declarations and component |
| 10756 | // lists. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10757 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10758 | for (auto &RE : MVLI.VarList) { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 10759 | assert(RE && "Null expr in omp to/from/map clause"); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10760 | SourceLocation ELoc = RE->getExprLoc(); |
| 10761 | |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10762 | auto *VE = RE->IgnoreParenLValueCasts(); |
| 10763 | |
| 10764 | if (VE->isValueDependent() || VE->isTypeDependent() || |
| 10765 | VE->isInstantiationDependent() || |
| 10766 | VE->containsUnexpandedParameterPack()) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10767 | // We can only analyze this information once the missing information is |
| 10768 | // resolved. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10769 | MVLI.ProcessedVarList.push_back(RE); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10770 | continue; |
| 10771 | } |
| 10772 | |
| 10773 | auto *SimpleExpr = RE->IgnoreParenCasts(); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10774 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10775 | if (!RE->IgnoreParenImpCasts()->isLValue()) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10776 | SemaRef.Diag(ELoc, |
| 10777 | diag::err_omp_expected_named_var_member_or_array_expression) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10778 | << RE->getSourceRange(); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10779 | continue; |
| 10780 | } |
| 10781 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10782 | OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; |
| 10783 | ValueDecl *CurDeclaration = nullptr; |
| 10784 | |
| 10785 | // Obtain the array or member expression bases if required. Also, fill the |
| 10786 | // components array with all the components identified in the process. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10787 | auto *BE = |
| 10788 | CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10789 | if (!BE) |
| 10790 | continue; |
| 10791 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10792 | assert(!CurComponents.empty() && |
| 10793 | "Invalid mappable expression information."); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10794 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10795 | // For the following checks, we rely on the base declaration which is |
| 10796 | // expected to be associated with the last component. The declaration is |
| 10797 | // expected to be a variable or a field (if 'this' is being mapped). |
| 10798 | CurDeclaration = CurComponents.back().getAssociatedDeclaration(); |
| 10799 | assert(CurDeclaration && "Null decl on map clause."); |
| 10800 | assert( |
| 10801 | CurDeclaration->isCanonicalDecl() && |
| 10802 | "Expecting components to have associated only canonical declarations."); |
| 10803 | |
| 10804 | auto *VD = dyn_cast<VarDecl>(CurDeclaration); |
| 10805 | auto *FD = dyn_cast<FieldDecl>(CurDeclaration); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10806 | |
| 10807 | assert((VD || FD) && "Only variables or fields are expected here!"); |
NAKAMURA Takumi | 6dcb814 | 2016-01-23 01:38:20 +0000 | [diff] [blame] | 10808 | (void)FD; |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10809 | |
| 10810 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10811 | // threadprivate variables cannot appear in a map clause. |
| 10812 | // OpenMP 4.5 [2.10.5, target update Construct] |
| 10813 | // threadprivate variables cannot appear in a from clause. |
| 10814 | if (VD && DSAS->isThreadPrivate(VD)) { |
| 10815 | auto DVar = DSAS->getTopDSA(VD, false); |
| 10816 | SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) |
| 10817 | << getOpenMPClauseName(CKind); |
| 10818 | ReportOriginalDSA(SemaRef, DSAS, VD, DVar); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10819 | continue; |
| 10820 | } |
| 10821 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10822 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] |
| 10823 | // A list item cannot appear in both a map clause and a data-sharing |
| 10824 | // attribute clause on the same construct. |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10825 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10826 | // Check conflicts with other map clause expressions. We check the conflicts |
| 10827 | // with the current construct separately from the enclosing data |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10828 | // environment, because the restrictions are different. We only have to |
| 10829 | // check conflicts across regions for the map clauses. |
| 10830 | if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, |
| 10831 | /*CurrentRegionOnly=*/true, CurComponents, CKind)) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10832 | break; |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10833 | if (CKind == OMPC_map && |
| 10834 | CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, |
| 10835 | /*CurrentRegionOnly=*/false, CurComponents, CKind)) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10836 | break; |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10837 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10838 | // OpenMP 4.5 [2.10.5, target update Construct] |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10839 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] |
| 10840 | // If the type of a list item is a reference to a type T then the type will |
| 10841 | // be considered to be T for all purposes of this clause. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10842 | QualType Type = CurDeclaration->getType().getNonReferenceType(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10843 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10844 | // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] |
| 10845 | // A list item in a to or from clause must have a mappable type. |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10846 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10847 | // A list item must have a mappable type. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10848 | if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, |
| 10849 | DSAS, Type)) |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10850 | continue; |
| 10851 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10852 | if (CKind == OMPC_map) { |
| 10853 | // target enter data |
| 10854 | // OpenMP [2.10.2, Restrictions, p. 99] |
| 10855 | // A map-type must be specified in all map clauses and must be either |
| 10856 | // to or alloc. |
| 10857 | OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); |
| 10858 | if (DKind == OMPD_target_enter_data && |
| 10859 | !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { |
| 10860 | SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) |
| 10861 | << (IsMapTypeImplicit ? 1 : 0) |
| 10862 | << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) |
| 10863 | << getOpenMPDirectiveName(DKind); |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 10864 | continue; |
| 10865 | } |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10866 | |
| 10867 | // target exit_data |
| 10868 | // OpenMP [2.10.3, Restrictions, p. 102] |
| 10869 | // A map-type must be specified in all map clauses and must be either |
| 10870 | // from, release, or delete. |
| 10871 | if (DKind == OMPD_target_exit_data && |
| 10872 | !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || |
| 10873 | MapType == OMPC_MAP_delete)) { |
| 10874 | SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) |
| 10875 | << (IsMapTypeImplicit ? 1 : 0) |
| 10876 | << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) |
| 10877 | << getOpenMPDirectiveName(DKind); |
| 10878 | continue; |
| 10879 | } |
| 10880 | |
| 10881 | // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] |
| 10882 | // A list item cannot appear in both a map clause and a data-sharing |
| 10883 | // attribute clause on the same construct |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 10884 | if ((DKind == OMPD_target || DKind == OMPD_target_teams || |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 10885 | DKind == OMPD_target_teams_distribute || |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 10886 | DKind == OMPD_target_teams_distribute_parallel_for || |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 10887 | DKind == OMPD_target_teams_distribute_parallel_for_simd || |
| 10888 | DKind == OMPD_target_teams_distribute_simd) && VD) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10889 | auto DVar = DSAS->getTopDSA(VD, false); |
| 10890 | if (isOpenMPPrivate(DVar.CKind)) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 10891 | SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10892 | << getOpenMPClauseName(DVar.CKind) |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 10893 | << getOpenMPClauseName(OMPC_map) |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10894 | << getOpenMPDirectiveName(DSAS->getCurrentDirective()); |
| 10895 | ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar); |
| 10896 | continue; |
| 10897 | } |
| 10898 | } |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 10899 | } |
| 10900 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10901 | // Save the current expression. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10902 | MVLI.ProcessedVarList.push_back(RE); |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10903 | |
| 10904 | // Store the components in the stack so that they can be used to check |
| 10905 | // against other clauses later on. |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 10906 | DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, |
| 10907 | /*WhereFoundClauseKind=*/OMPC_map); |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10908 | |
| 10909 | // Save the components and declaration to create the clause. For purposes of |
| 10910 | // the clause creation, any component list that has has base 'this' uses |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 10911 | // null as base declaration. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10912 | MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); |
| 10913 | MVLI.VarComponents.back().append(CurComponents.begin(), |
| 10914 | CurComponents.end()); |
| 10915 | MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr |
| 10916 | : CurDeclaration); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10917 | } |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10918 | } |
| 10919 | |
| 10920 | OMPClause * |
| 10921 | Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier, |
| 10922 | OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, |
| 10923 | SourceLocation MapLoc, SourceLocation ColonLoc, |
| 10924 | ArrayRef<Expr *> VarList, SourceLocation StartLoc, |
| 10925 | SourceLocation LParenLoc, SourceLocation EndLoc) { |
| 10926 | MappableVarListInfo MVLI(VarList); |
| 10927 | checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc, |
| 10928 | MapType, IsMapTypeImplicit); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10929 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10930 | // We need to produce a map clause even if we don't have variables so that |
| 10931 | // other diagnostics related with non-existing map clauses are accurate. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10932 | return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 10933 | MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, |
| 10934 | MVLI.VarComponents, MapTypeModifier, MapType, |
| 10935 | IsMapTypeImplicit, MapLoc); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10936 | } |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 10937 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 10938 | QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, |
| 10939 | TypeResult ParsedType) { |
| 10940 | assert(ParsedType.isUsable()); |
| 10941 | |
| 10942 | QualType ReductionType = GetTypeFromParser(ParsedType.get()); |
| 10943 | if (ReductionType.isNull()) |
| 10944 | return QualType(); |
| 10945 | |
| 10946 | // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ |
| 10947 | // A type name in a declare reduction directive cannot be a function type, an |
| 10948 | // array type, a reference type, or a type qualified with const, volatile or |
| 10949 | // restrict. |
| 10950 | if (ReductionType.hasQualifiers()) { |
| 10951 | Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; |
| 10952 | return QualType(); |
| 10953 | } |
| 10954 | |
| 10955 | if (ReductionType->isFunctionType()) { |
| 10956 | Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; |
| 10957 | return QualType(); |
| 10958 | } |
| 10959 | if (ReductionType->isReferenceType()) { |
| 10960 | Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; |
| 10961 | return QualType(); |
| 10962 | } |
| 10963 | if (ReductionType->isArrayType()) { |
| 10964 | Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; |
| 10965 | return QualType(); |
| 10966 | } |
| 10967 | return ReductionType; |
| 10968 | } |
| 10969 | |
| 10970 | Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( |
| 10971 | Scope *S, DeclContext *DC, DeclarationName Name, |
| 10972 | ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, |
| 10973 | AccessSpecifier AS, Decl *PrevDeclInScope) { |
| 10974 | SmallVector<Decl *, 8> Decls; |
| 10975 | Decls.reserve(ReductionTypes.size()); |
| 10976 | |
| 10977 | LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, |
| 10978 | ForRedeclaration); |
| 10979 | // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions |
| 10980 | // A reduction-identifier may not be re-declared in the current scope for the |
| 10981 | // same type or for a type that is compatible according to the base language |
| 10982 | // rules. |
| 10983 | llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; |
| 10984 | OMPDeclareReductionDecl *PrevDRD = nullptr; |
| 10985 | bool InCompoundScope = true; |
| 10986 | if (S != nullptr) { |
| 10987 | // Find previous declaration with the same name not referenced in other |
| 10988 | // declarations. |
| 10989 | FunctionScopeInfo *ParentFn = getEnclosingFunction(); |
| 10990 | InCompoundScope = |
| 10991 | (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); |
| 10992 | LookupName(Lookup, S); |
| 10993 | FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, |
| 10994 | /*AllowInlineNamespace=*/false); |
| 10995 | llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; |
| 10996 | auto Filter = Lookup.makeFilter(); |
| 10997 | while (Filter.hasNext()) { |
| 10998 | auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); |
| 10999 | if (InCompoundScope) { |
| 11000 | auto I = UsedAsPrevious.find(PrevDecl); |
| 11001 | if (I == UsedAsPrevious.end()) |
| 11002 | UsedAsPrevious[PrevDecl] = false; |
| 11003 | if (auto *D = PrevDecl->getPrevDeclInScope()) |
| 11004 | UsedAsPrevious[D] = true; |
| 11005 | } |
| 11006 | PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = |
| 11007 | PrevDecl->getLocation(); |
| 11008 | } |
| 11009 | Filter.done(); |
| 11010 | if (InCompoundScope) { |
| 11011 | for (auto &PrevData : UsedAsPrevious) { |
| 11012 | if (!PrevData.second) { |
| 11013 | PrevDRD = PrevData.first; |
| 11014 | break; |
| 11015 | } |
| 11016 | } |
| 11017 | } |
| 11018 | } else if (PrevDeclInScope != nullptr) { |
| 11019 | auto *PrevDRDInScope = PrevDRD = |
| 11020 | cast<OMPDeclareReductionDecl>(PrevDeclInScope); |
| 11021 | do { |
| 11022 | PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = |
| 11023 | PrevDRDInScope->getLocation(); |
| 11024 | PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); |
| 11025 | } while (PrevDRDInScope != nullptr); |
| 11026 | } |
| 11027 | for (auto &TyData : ReductionTypes) { |
| 11028 | auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); |
| 11029 | bool Invalid = false; |
| 11030 | if (I != PreviousRedeclTypes.end()) { |
| 11031 | Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) |
| 11032 | << TyData.first; |
| 11033 | Diag(I->second, diag::note_previous_definition); |
| 11034 | Invalid = true; |
| 11035 | } |
| 11036 | PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; |
| 11037 | auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, |
| 11038 | Name, TyData.first, PrevDRD); |
| 11039 | DC->addDecl(DRD); |
| 11040 | DRD->setAccess(AS); |
| 11041 | Decls.push_back(DRD); |
| 11042 | if (Invalid) |
| 11043 | DRD->setInvalidDecl(); |
| 11044 | else |
| 11045 | PrevDRD = DRD; |
| 11046 | } |
| 11047 | |
| 11048 | return DeclGroupPtrTy::make( |
| 11049 | DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); |
| 11050 | } |
| 11051 | |
| 11052 | void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { |
| 11053 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 11054 | |
| 11055 | // Enter new function scope. |
| 11056 | PushFunctionScope(); |
| 11057 | getCurFunction()->setHasBranchProtectedScope(); |
| 11058 | getCurFunction()->setHasOMPDeclareReductionCombiner(); |
| 11059 | |
| 11060 | if (S != nullptr) |
| 11061 | PushDeclContext(S, DRD); |
| 11062 | else |
| 11063 | CurContext = DRD; |
| 11064 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 11065 | PushExpressionEvaluationContext( |
| 11066 | ExpressionEvaluationContext::PotentiallyEvaluated); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 11067 | |
| 11068 | QualType ReductionType = DRD->getType(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 11069 | // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will |
| 11070 | // be replaced by '*omp_parm' during codegen. This required because 'omp_in' |
| 11071 | // uses semantics of argument handles by value, but it should be passed by |
| 11072 | // reference. C lang does not support references, so pass all parameters as |
| 11073 | // pointers. |
| 11074 | // Create 'T omp_in;' variable. |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 11075 | auto *OmpInParm = |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 11076 | buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 11077 | // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will |
| 11078 | // be replaced by '*omp_parm' during codegen. This required because 'omp_out' |
| 11079 | // uses semantics of argument handles by value, but it should be passed by |
| 11080 | // reference. C lang does not support references, so pass all parameters as |
| 11081 | // pointers. |
| 11082 | // Create 'T omp_out;' variable. |
| 11083 | auto *OmpOutParm = |
| 11084 | buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); |
| 11085 | if (S != nullptr) { |
| 11086 | PushOnScopeChains(OmpInParm, S); |
| 11087 | PushOnScopeChains(OmpOutParm, S); |
| 11088 | } else { |
| 11089 | DRD->addDecl(OmpInParm); |
| 11090 | DRD->addDecl(OmpOutParm); |
| 11091 | } |
| 11092 | } |
| 11093 | |
| 11094 | void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { |
| 11095 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 11096 | DiscardCleanupsInEvaluationContext(); |
| 11097 | PopExpressionEvaluationContext(); |
| 11098 | |
| 11099 | PopDeclContext(); |
| 11100 | PopFunctionScopeInfo(); |
| 11101 | |
| 11102 | if (Combiner != nullptr) |
| 11103 | DRD->setCombiner(Combiner); |
| 11104 | else |
| 11105 | DRD->setInvalidDecl(); |
| 11106 | } |
| 11107 | |
| 11108 | void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { |
| 11109 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 11110 | |
| 11111 | // Enter new function scope. |
| 11112 | PushFunctionScope(); |
| 11113 | getCurFunction()->setHasBranchProtectedScope(); |
| 11114 | |
| 11115 | if (S != nullptr) |
| 11116 | PushDeclContext(S, DRD); |
| 11117 | else |
| 11118 | CurContext = DRD; |
| 11119 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 11120 | PushExpressionEvaluationContext( |
| 11121 | ExpressionEvaluationContext::PotentiallyEvaluated); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 11122 | |
| 11123 | QualType ReductionType = DRD->getType(); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 11124 | // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will |
| 11125 | // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' |
| 11126 | // uses semantics of argument handles by value, but it should be passed by |
| 11127 | // reference. C lang does not support references, so pass all parameters as |
| 11128 | // pointers. |
| 11129 | // Create 'T omp_priv;' variable. |
| 11130 | auto *OmpPrivParm = |
| 11131 | buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 11132 | // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will |
| 11133 | // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' |
| 11134 | // uses semantics of argument handles by value, but it should be passed by |
| 11135 | // reference. C lang does not support references, so pass all parameters as |
| 11136 | // pointers. |
| 11137 | // Create 'T omp_orig;' variable. |
| 11138 | auto *OmpOrigParm = |
| 11139 | buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 11140 | if (S != nullptr) { |
| 11141 | PushOnScopeChains(OmpPrivParm, S); |
| 11142 | PushOnScopeChains(OmpOrigParm, S); |
| 11143 | } else { |
| 11144 | DRD->addDecl(OmpPrivParm); |
| 11145 | DRD->addDecl(OmpOrigParm); |
| 11146 | } |
| 11147 | } |
| 11148 | |
| 11149 | void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, |
| 11150 | Expr *Initializer) { |
| 11151 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 11152 | DiscardCleanupsInEvaluationContext(); |
| 11153 | PopExpressionEvaluationContext(); |
| 11154 | |
| 11155 | PopDeclContext(); |
| 11156 | PopFunctionScopeInfo(); |
| 11157 | |
| 11158 | if (Initializer != nullptr) |
| 11159 | DRD->setInitializer(Initializer); |
| 11160 | else |
| 11161 | DRD->setInvalidDecl(); |
| 11162 | } |
| 11163 | |
| 11164 | Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( |
| 11165 | Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { |
| 11166 | for (auto *D : DeclReductions.get()) { |
| 11167 | if (IsValid) { |
| 11168 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 11169 | if (S != nullptr) |
| 11170 | PushOnScopeChains(DRD, S, /*AddToContext=*/false); |
| 11171 | } else |
| 11172 | D->setInvalidDecl(); |
| 11173 | } |
| 11174 | return DeclReductions; |
| 11175 | } |
| 11176 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 11177 | OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 11178 | SourceLocation StartLoc, |
| 11179 | SourceLocation LParenLoc, |
| 11180 | SourceLocation EndLoc) { |
| 11181 | Expr *ValExpr = NumTeams; |
Arpith Chacko Jacob | bc12634 | 2017-01-25 11:28:18 +0000 | [diff] [blame] | 11182 | Stmt *HelperValStmt = nullptr; |
| 11183 | OpenMPDirectiveKind CaptureRegion = OMPD_unknown; |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 11184 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 11185 | // OpenMP [teams Constrcut, Restrictions] |
| 11186 | // The num_teams expression must evaluate to a positive integer value. |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 11187 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, |
| 11188 | /*StrictlyPositive=*/true)) |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 11189 | return nullptr; |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 11190 | |
Arpith Chacko Jacob | bc12634 | 2017-01-25 11:28:18 +0000 | [diff] [blame] | 11191 | OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); |
| 11192 | CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); |
| 11193 | if (CaptureRegion != OMPD_unknown) { |
| 11194 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
| 11195 | ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); |
| 11196 | HelperValStmt = buildPreInits(Context, Captures); |
| 11197 | } |
| 11198 | |
| 11199 | return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, |
| 11200 | StartLoc, LParenLoc, EndLoc); |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 11201 | } |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 11202 | |
| 11203 | OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, |
| 11204 | SourceLocation StartLoc, |
| 11205 | SourceLocation LParenLoc, |
| 11206 | SourceLocation EndLoc) { |
| 11207 | Expr *ValExpr = ThreadLimit; |
Arpith Chacko Jacob | 7ecc0b7 | 2017-01-25 11:44:35 +0000 | [diff] [blame] | 11208 | Stmt *HelperValStmt = nullptr; |
| 11209 | OpenMPDirectiveKind CaptureRegion = OMPD_unknown; |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 11210 | |
| 11211 | // OpenMP [teams Constrcut, Restrictions] |
| 11212 | // The thread_limit expression must evaluate to a positive integer value. |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 11213 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, |
| 11214 | /*StrictlyPositive=*/true)) |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 11215 | return nullptr; |
| 11216 | |
Arpith Chacko Jacob | 7ecc0b7 | 2017-01-25 11:44:35 +0000 | [diff] [blame] | 11217 | OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); |
| 11218 | CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); |
| 11219 | if (CaptureRegion != OMPD_unknown) { |
| 11220 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
| 11221 | ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); |
| 11222 | HelperValStmt = buildPreInits(Context, Captures); |
| 11223 | } |
| 11224 | |
| 11225 | return new (Context) OMPThreadLimitClause( |
| 11226 | ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 11227 | } |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 11228 | |
| 11229 | OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, |
| 11230 | SourceLocation StartLoc, |
| 11231 | SourceLocation LParenLoc, |
| 11232 | SourceLocation EndLoc) { |
| 11233 | Expr *ValExpr = Priority; |
| 11234 | |
| 11235 | // OpenMP [2.9.1, task Constrcut] |
| 11236 | // The priority-value is a non-negative numerical scalar expression. |
| 11237 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, |
| 11238 | /*StrictlyPositive=*/false)) |
| 11239 | return nullptr; |
| 11240 | |
| 11241 | return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 11242 | } |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 11243 | |
| 11244 | OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, |
| 11245 | SourceLocation StartLoc, |
| 11246 | SourceLocation LParenLoc, |
| 11247 | SourceLocation EndLoc) { |
| 11248 | Expr *ValExpr = Grainsize; |
| 11249 | |
| 11250 | // OpenMP [2.9.2, taskloop Constrcut] |
| 11251 | // The parameter of the grainsize clause must be a positive integer |
| 11252 | // expression. |
| 11253 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, |
| 11254 | /*StrictlyPositive=*/true)) |
| 11255 | return nullptr; |
| 11256 | |
| 11257 | return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 11258 | } |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 11259 | |
| 11260 | OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, |
| 11261 | SourceLocation StartLoc, |
| 11262 | SourceLocation LParenLoc, |
| 11263 | SourceLocation EndLoc) { |
| 11264 | Expr *ValExpr = NumTasks; |
| 11265 | |
| 11266 | // OpenMP [2.9.2, taskloop Constrcut] |
| 11267 | // The parameter of the num_tasks clause must be a positive integer |
| 11268 | // expression. |
| 11269 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, |
| 11270 | /*StrictlyPositive=*/true)) |
| 11271 | return nullptr; |
| 11272 | |
| 11273 | return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 11274 | } |
| 11275 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 11276 | OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, |
| 11277 | SourceLocation LParenLoc, |
| 11278 | SourceLocation EndLoc) { |
| 11279 | // OpenMP [2.13.2, critical construct, Description] |
| 11280 | // ... where hint-expression is an integer constant expression that evaluates |
| 11281 | // to a valid lock hint. |
| 11282 | ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); |
| 11283 | if (HintExpr.isInvalid()) |
| 11284 | return nullptr; |
| 11285 | return new (Context) |
| 11286 | OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); |
| 11287 | } |
| 11288 | |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 11289 | OMPClause *Sema::ActOnOpenMPDistScheduleClause( |
| 11290 | OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, |
| 11291 | SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, |
| 11292 | SourceLocation EndLoc) { |
| 11293 | if (Kind == OMPC_DIST_SCHEDULE_unknown) { |
| 11294 | std::string Values; |
| 11295 | Values += "'"; |
| 11296 | Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); |
| 11297 | Values += "'"; |
| 11298 | Diag(KindLoc, diag::err_omp_unexpected_clause_value) |
| 11299 | << Values << getOpenMPClauseName(OMPC_dist_schedule); |
| 11300 | return nullptr; |
| 11301 | } |
| 11302 | Expr *ValExpr = ChunkSize; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 11303 | Stmt *HelperValStmt = nullptr; |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 11304 | if (ChunkSize) { |
| 11305 | if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && |
| 11306 | !ChunkSize->isInstantiationDependent() && |
| 11307 | !ChunkSize->containsUnexpandedParameterPack()) { |
| 11308 | SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); |
| 11309 | ExprResult Val = |
| 11310 | PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); |
| 11311 | if (Val.isInvalid()) |
| 11312 | return nullptr; |
| 11313 | |
| 11314 | ValExpr = Val.get(); |
| 11315 | |
| 11316 | // OpenMP [2.7.1, Restrictions] |
| 11317 | // chunk_size must be a loop invariant integer expression with a positive |
| 11318 | // value. |
| 11319 | llvm::APSInt Result; |
| 11320 | if (ValExpr->isIntegerConstantExpr(Result, Context)) { |
| 11321 | if (Result.isSigned() && !Result.isStrictlyPositive()) { |
| 11322 | Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) |
| 11323 | << "dist_schedule" << ChunkSize->getSourceRange(); |
| 11324 | return nullptr; |
| 11325 | } |
Alexey Bataev | b46cdea | 2016-06-15 11:20:48 +0000 | [diff] [blame] | 11326 | } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && |
| 11327 | !CurContext->isDependentContext()) { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 11328 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
| 11329 | ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); |
| 11330 | HelperValStmt = buildPreInits(Context, Captures); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 11331 | } |
| 11332 | } |
| 11333 | } |
| 11334 | |
| 11335 | return new (Context) |
| 11336 | OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 11337 | Kind, ValExpr, HelperValStmt); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 11338 | } |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 11339 | |
| 11340 | OMPClause *Sema::ActOnOpenMPDefaultmapClause( |
| 11341 | OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, |
| 11342 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, |
| 11343 | SourceLocation KindLoc, SourceLocation EndLoc) { |
| 11344 | // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 11345 | if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 11346 | std::string Value; |
| 11347 | SourceLocation Loc; |
| 11348 | Value += "'"; |
| 11349 | if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { |
| 11350 | Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 11351 | OMPC_DEFAULTMAP_MODIFIER_tofrom); |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 11352 | Loc = MLoc; |
| 11353 | } else { |
| 11354 | Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 11355 | OMPC_DEFAULTMAP_scalar); |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 11356 | Loc = KindLoc; |
| 11357 | } |
| 11358 | Value += "'"; |
| 11359 | Diag(Loc, diag::err_omp_unexpected_clause_value) |
| 11360 | << Value << getOpenMPClauseName(OMPC_defaultmap); |
| 11361 | return nullptr; |
| 11362 | } |
| 11363 | |
| 11364 | return new (Context) |
| 11365 | OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); |
| 11366 | } |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11367 | |
| 11368 | bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { |
| 11369 | DeclContext *CurLexicalContext = getCurLexicalContext(); |
| 11370 | if (!CurLexicalContext->isFileContext() && |
| 11371 | !CurLexicalContext->isExternCContext() && |
| 11372 | !CurLexicalContext->isExternCXXContext()) { |
| 11373 | Diag(Loc, diag::err_omp_region_not_file_context); |
| 11374 | return false; |
| 11375 | } |
| 11376 | if (IsInOpenMPDeclareTargetContext) { |
| 11377 | Diag(Loc, diag::err_omp_enclosed_declare_target); |
| 11378 | return false; |
| 11379 | } |
| 11380 | |
| 11381 | IsInOpenMPDeclareTargetContext = true; |
| 11382 | return true; |
| 11383 | } |
| 11384 | |
| 11385 | void Sema::ActOnFinishOpenMPDeclareTargetDirective() { |
| 11386 | assert(IsInOpenMPDeclareTargetContext && |
| 11387 | "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); |
| 11388 | |
| 11389 | IsInOpenMPDeclareTargetContext = false; |
| 11390 | } |
| 11391 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 11392 | void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, |
| 11393 | CXXScopeSpec &ScopeSpec, |
| 11394 | const DeclarationNameInfo &Id, |
| 11395 | OMPDeclareTargetDeclAttr::MapTypeTy MT, |
| 11396 | NamedDeclSetType &SameDirectiveDecls) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11397 | LookupResult Lookup(*this, Id, LookupOrdinaryName); |
| 11398 | LookupParsedName(Lookup, CurScope, &ScopeSpec, true); |
| 11399 | |
| 11400 | if (Lookup.isAmbiguous()) |
| 11401 | return; |
| 11402 | Lookup.suppressDiagnostics(); |
| 11403 | |
| 11404 | if (!Lookup.isSingleResult()) { |
| 11405 | if (TypoCorrection Corrected = |
| 11406 | CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, |
| 11407 | llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), |
| 11408 | CTK_ErrorRecovery)) { |
| 11409 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) |
| 11410 | << Id.getName()); |
| 11411 | checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); |
| 11412 | return; |
| 11413 | } |
| 11414 | |
| 11415 | Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); |
| 11416 | return; |
| 11417 | } |
| 11418 | |
| 11419 | NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); |
| 11420 | if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { |
| 11421 | if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) |
| 11422 | Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); |
| 11423 | |
| 11424 | if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) { |
| 11425 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); |
| 11426 | ND->addAttr(A); |
| 11427 | if (ASTMutationListener *ML = Context.getASTMutationListener()) |
| 11428 | ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); |
| 11429 | checkDeclIsAllowedInOpenMPTarget(nullptr, ND); |
| 11430 | } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) { |
| 11431 | Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) |
| 11432 | << Id.getName(); |
| 11433 | } |
| 11434 | } else |
| 11435 | Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); |
| 11436 | } |
| 11437 | |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11438 | static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, |
| 11439 | Sema &SemaRef, Decl *D) { |
| 11440 | if (!D) |
| 11441 | return; |
| 11442 | Decl *LD = nullptr; |
| 11443 | if (isa<TagDecl>(D)) { |
| 11444 | LD = cast<TagDecl>(D)->getDefinition(); |
| 11445 | } else if (isa<VarDecl>(D)) { |
| 11446 | LD = cast<VarDecl>(D)->getDefinition(); |
| 11447 | |
| 11448 | // If this is an implicit variable that is legal and we do not need to do |
| 11449 | // anything. |
| 11450 | if (cast<VarDecl>(D)->isImplicit()) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11451 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 11452 | SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); |
| 11453 | D->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11454 | if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11455 | ML->DeclarationMarkedOpenMPDeclareTarget(D, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11456 | return; |
| 11457 | } |
| 11458 | |
| 11459 | } else if (isa<FunctionDecl>(D)) { |
| 11460 | const FunctionDecl *FD = nullptr; |
| 11461 | if (cast<FunctionDecl>(D)->hasBody(FD)) |
| 11462 | LD = const_cast<FunctionDecl *>(FD); |
| 11463 | |
| 11464 | // If the definition is associated with the current declaration in the |
| 11465 | // target region (it can be e.g. a lambda) that is legal and we do not need |
| 11466 | // to do anything else. |
| 11467 | if (LD == D) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11468 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 11469 | SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); |
| 11470 | D->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11471 | if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11472 | ML->DeclarationMarkedOpenMPDeclareTarget(D, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11473 | return; |
| 11474 | } |
| 11475 | } |
| 11476 | if (!LD) |
| 11477 | LD = D; |
| 11478 | if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() && |
| 11479 | (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) { |
| 11480 | // Outlined declaration is not declared target. |
| 11481 | if (LD->isOutOfLine()) { |
| 11482 | SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); |
| 11483 | SemaRef.Diag(SL, diag::note_used_here) << SR; |
| 11484 | } else { |
| 11485 | DeclContext *DC = LD->getDeclContext(); |
| 11486 | while (DC) { |
| 11487 | if (isa<FunctionDecl>(DC) && |
| 11488 | cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>()) |
| 11489 | break; |
| 11490 | DC = DC->getParent(); |
| 11491 | } |
| 11492 | if (DC) |
| 11493 | return; |
| 11494 | |
| 11495 | // Is not declared in target context. |
| 11496 | SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); |
| 11497 | SemaRef.Diag(SL, diag::note_used_here) << SR; |
| 11498 | } |
| 11499 | // Mark decl as declared target to prevent further diagnostic. |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11500 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 11501 | SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); |
| 11502 | D->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11503 | if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11504 | ML->DeclarationMarkedOpenMPDeclareTarget(D, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11505 | } |
| 11506 | } |
| 11507 | |
| 11508 | static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, |
| 11509 | Sema &SemaRef, DSAStackTy *Stack, |
| 11510 | ValueDecl *VD) { |
| 11511 | if (VD->hasAttr<OMPDeclareTargetDeclAttr>()) |
| 11512 | return true; |
| 11513 | if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType())) |
| 11514 | return false; |
| 11515 | return true; |
| 11516 | } |
| 11517 | |
| 11518 | void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) { |
| 11519 | if (!D || D->isInvalidDecl()) |
| 11520 | return; |
| 11521 | SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); |
| 11522 | SourceLocation SL = E ? E->getLocStart() : D->getLocation(); |
| 11523 | // 2.10.6: threadprivate variable cannot appear in a declare target directive. |
| 11524 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 11525 | if (DSAStack->isThreadPrivate(VD)) { |
| 11526 | Diag(SL, diag::err_omp_threadprivate_in_target); |
| 11527 | ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); |
| 11528 | return; |
| 11529 | } |
| 11530 | } |
| 11531 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 11532 | // Problem if any with var declared with incomplete type will be reported |
| 11533 | // as normal, so no need to check it here. |
| 11534 | if ((E || !VD->getType()->isIncompleteType()) && |
| 11535 | !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) { |
| 11536 | // Mark decl as declared target to prevent further diagnostic. |
| 11537 | if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11538 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 11539 | Context, OMPDeclareTargetDeclAttr::MT_To); |
| 11540 | VD->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11541 | if (ASTMutationListener *ML = Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11542 | ML->DeclarationMarkedOpenMPDeclareTarget(VD, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11543 | } |
| 11544 | return; |
| 11545 | } |
| 11546 | } |
| 11547 | if (!E) { |
| 11548 | // Checking declaration inside declare target region. |
| 11549 | if (!D->hasAttr<OMPDeclareTargetDeclAttr>() && |
| 11550 | (isa<VarDecl>(D) || isa<FunctionDecl>(D))) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11551 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 11552 | Context, OMPDeclareTargetDeclAttr::MT_To); |
| 11553 | D->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11554 | if (ASTMutationListener *ML = Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 11555 | ML->DeclarationMarkedOpenMPDeclareTarget(D, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 11556 | } |
| 11557 | return; |
| 11558 | } |
| 11559 | checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); |
| 11560 | } |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 11561 | |
| 11562 | OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, |
| 11563 | SourceLocation StartLoc, |
| 11564 | SourceLocation LParenLoc, |
| 11565 | SourceLocation EndLoc) { |
| 11566 | MappableVarListInfo MVLI(VarList); |
| 11567 | checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc); |
| 11568 | if (MVLI.ProcessedVarList.empty()) |
| 11569 | return nullptr; |
| 11570 | |
| 11571 | return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 11572 | MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, |
| 11573 | MVLI.VarComponents); |
| 11574 | } |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 11575 | |
| 11576 | OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, |
| 11577 | SourceLocation StartLoc, |
| 11578 | SourceLocation LParenLoc, |
| 11579 | SourceLocation EndLoc) { |
| 11580 | MappableVarListInfo MVLI(VarList); |
| 11581 | checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc); |
| 11582 | if (MVLI.ProcessedVarList.empty()) |
| 11583 | return nullptr; |
| 11584 | |
| 11585 | return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 11586 | MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, |
| 11587 | MVLI.VarComponents); |
| 11588 | } |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 11589 | |
| 11590 | OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, |
| 11591 | SourceLocation StartLoc, |
| 11592 | SourceLocation LParenLoc, |
| 11593 | SourceLocation EndLoc) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 11594 | MappableVarListInfo MVLI(VarList); |
| 11595 | SmallVector<Expr *, 8> PrivateCopies; |
| 11596 | SmallVector<Expr *, 8> Inits; |
| 11597 | |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 11598 | for (auto &RefExpr : VarList) { |
| 11599 | assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); |
| 11600 | SourceLocation ELoc; |
| 11601 | SourceRange ERange; |
| 11602 | Expr *SimpleRefExpr = RefExpr; |
| 11603 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
| 11604 | if (Res.second) { |
| 11605 | // It will be analyzed later. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 11606 | MVLI.ProcessedVarList.push_back(RefExpr); |
| 11607 | PrivateCopies.push_back(nullptr); |
| 11608 | Inits.push_back(nullptr); |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 11609 | } |
| 11610 | ValueDecl *D = Res.first; |
| 11611 | if (!D) |
| 11612 | continue; |
| 11613 | |
| 11614 | QualType Type = D->getType(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 11615 | Type = Type.getNonReferenceType().getUnqualifiedType(); |
| 11616 | |
| 11617 | auto *VD = dyn_cast<VarDecl>(D); |
| 11618 | |
| 11619 | // Item should be a pointer or reference to pointer. |
| 11620 | if (!Type->isPointerType()) { |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 11621 | Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) |
| 11622 | << 0 << RefExpr->getSourceRange(); |
| 11623 | continue; |
| 11624 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 11625 | |
| 11626 | // Build the private variable and the expression that refers to it. |
| 11627 | auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 11628 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 11629 | if (VDPrivate->isInvalidDecl()) |
| 11630 | continue; |
| 11631 | |
| 11632 | CurContext->addDecl(VDPrivate); |
| 11633 | auto VDPrivateRefExpr = buildDeclRefExpr( |
| 11634 | *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); |
| 11635 | |
| 11636 | // Add temporary variable to initialize the private copy of the pointer. |
| 11637 | auto *VDInit = |
| 11638 | buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); |
| 11639 | auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), |
| 11640 | RefExpr->getExprLoc()); |
| 11641 | AddInitializerToDecl(VDPrivate, |
| 11642 | DefaultLvalueConversion(VDInitRefExpr).get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 11643 | /*DirectInit=*/false); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 11644 | |
| 11645 | // If required, build a capture to implement the privatization initialized |
| 11646 | // with the current list item value. |
| 11647 | DeclRefExpr *Ref = nullptr; |
| 11648 | if (!VD) |
| 11649 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); |
| 11650 | MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); |
| 11651 | PrivateCopies.push_back(VDPrivateRefExpr); |
| 11652 | Inits.push_back(VDInitRefExpr); |
| 11653 | |
| 11654 | // We need to add a data sharing attribute for this variable to make sure it |
| 11655 | // is correctly captured. A variable that shows up in a use_device_ptr has |
| 11656 | // similar properties of a first private variable. |
| 11657 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); |
| 11658 | |
| 11659 | // Create a mappable component for the list item. List items in this clause |
| 11660 | // only need a component. |
| 11661 | MVLI.VarBaseDeclarations.push_back(D); |
| 11662 | MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); |
| 11663 | MVLI.VarComponents.back().push_back( |
| 11664 | OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 11665 | } |
| 11666 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 11667 | if (MVLI.ProcessedVarList.empty()) |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 11668 | return nullptr; |
| 11669 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 11670 | return OMPUseDevicePtrClause::Create( |
| 11671 | Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, |
| 11672 | PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents); |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 11673 | } |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11674 | |
| 11675 | OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, |
| 11676 | SourceLocation StartLoc, |
| 11677 | SourceLocation LParenLoc, |
| 11678 | SourceLocation EndLoc) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11679 | MappableVarListInfo MVLI(VarList); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11680 | for (auto &RefExpr : VarList) { |
Kelvin Li | 8437625 | 2016-12-14 15:39:58 +0000 | [diff] [blame] | 11681 | assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11682 | SourceLocation ELoc; |
| 11683 | SourceRange ERange; |
| 11684 | Expr *SimpleRefExpr = RefExpr; |
| 11685 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
| 11686 | if (Res.second) { |
| 11687 | // It will be analyzed later. |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11688 | MVLI.ProcessedVarList.push_back(RefExpr); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11689 | } |
| 11690 | ValueDecl *D = Res.first; |
| 11691 | if (!D) |
| 11692 | continue; |
| 11693 | |
| 11694 | QualType Type = D->getType(); |
| 11695 | // item should be a pointer or array or reference to pointer or array |
| 11696 | if (!Type.getNonReferenceType()->isPointerType() && |
| 11697 | !Type.getNonReferenceType()->isArrayType()) { |
| 11698 | Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) |
| 11699 | << 0 << RefExpr->getSourceRange(); |
| 11700 | continue; |
| 11701 | } |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11702 | |
| 11703 | // Check if the declaration in the clause does not show up in any data |
| 11704 | // sharing attribute. |
| 11705 | auto DVar = DSAStack->getTopDSA(D, false); |
| 11706 | if (isOpenMPPrivate(DVar.CKind)) { |
| 11707 | Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) |
| 11708 | << getOpenMPClauseName(DVar.CKind) |
| 11709 | << getOpenMPClauseName(OMPC_is_device_ptr) |
| 11710 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 11711 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
| 11712 | continue; |
| 11713 | } |
| 11714 | |
| 11715 | Expr *ConflictExpr; |
| 11716 | if (DSAStack->checkMappableExprComponentListsForDecl( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 11717 | D, /*CurrentRegionOnly=*/true, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11718 | [&ConflictExpr]( |
| 11719 | OMPClauseMappableExprCommon::MappableExprComponentListRef R, |
| 11720 | OpenMPClauseKind) -> bool { |
| 11721 | ConflictExpr = R.front().getAssociatedExpression(); |
| 11722 | return true; |
| 11723 | })) { |
| 11724 | Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); |
| 11725 | Diag(ConflictExpr->getExprLoc(), diag::note_used_here) |
| 11726 | << ConflictExpr->getSourceRange(); |
| 11727 | continue; |
| 11728 | } |
| 11729 | |
| 11730 | // Store the components in the stack so that they can be used to check |
| 11731 | // against other clauses later on. |
| 11732 | OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); |
| 11733 | DSAStack->addMappableExpressionComponents( |
| 11734 | D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); |
| 11735 | |
| 11736 | // Record the expression we've just processed. |
| 11737 | MVLI.ProcessedVarList.push_back(SimpleRefExpr); |
| 11738 | |
| 11739 | // Create a mappable component for the list item. List items in this clause |
| 11740 | // only need a component. We use a null declaration to signal fields in |
| 11741 | // 'this'. |
| 11742 | assert((isa<DeclRefExpr>(SimpleRefExpr) || |
| 11743 | isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && |
| 11744 | "Unexpected device pointer expression!"); |
| 11745 | MVLI.VarBaseDeclarations.push_back( |
| 11746 | isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); |
| 11747 | MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); |
| 11748 | MVLI.VarComponents.back().push_back(MC); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11749 | } |
| 11750 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11751 | if (MVLI.ProcessedVarList.empty()) |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11752 | return nullptr; |
| 11753 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11754 | return OMPIsDevicePtrClause::Create( |
| 11755 | Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, |
| 11756 | MVLI.VarBaseDeclarations, MVLI.VarComponents); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11757 | } |