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. |
| 121 | StackTy Stack; |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 122 | /// \brief true, if check for DSA must be from parent directive, false, if |
| 123 | /// from current directive. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 124 | OpenMPClauseKind ClauseKindMode = OMPC_unknown; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 125 | Sema &SemaRef; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 126 | bool ForceCapturing = false; |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 127 | CriticalsWithHintsTy Criticals; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 128 | |
| 129 | typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; |
| 130 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 131 | DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 132 | |
| 133 | /// \brief Checks if the variable is a local for OpenMP region. |
| 134 | bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 135 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 136 | public: |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 137 | explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {} |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 138 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 139 | bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } |
| 140 | void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 141 | |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 142 | bool isForceVarCapturing() const { return ForceCapturing; } |
| 143 | void setForceVarCapturing(bool V) { ForceCapturing = V; } |
| 144 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 145 | void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 146 | Scope *CurScope, SourceLocation Loc) { |
| 147 | Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc)); |
| 148 | Stack.back().DefaultAttrLoc = Loc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void pop() { |
| 152 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!"); |
| 153 | Stack.pop_back(); |
| 154 | } |
| 155 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 156 | void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) { |
| 157 | Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint); |
| 158 | } |
| 159 | const std::pair<OMPCriticalDirective *, llvm::APSInt> |
| 160 | getCriticalWithHint(const DeclarationNameInfo &Name) const { |
| 161 | auto I = Criticals.find(Name.getAsString()); |
| 162 | if (I != Criticals.end()) |
| 163 | return I->second; |
| 164 | return std::make_pair(nullptr, llvm::APSInt()); |
| 165 | } |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 166 | /// \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] | 167 | /// add it and return NULL; otherwise return previous occurrence's expression |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 168 | /// for diagnostics. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 169 | Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 170 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 171 | /// \brief Register specified variable as loop control variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 172 | void addLoopControlVariable(ValueDecl *D, VarDecl *Capture); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 173 | /// \brief Check if the specified variable is a loop control variable for |
| 174 | /// current region. |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 175 | /// \return The index of the loop control variable in the list of associated |
| 176 | /// for-loops (from outer to inner). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 177 | LCDeclInfo isLoopControlVariable(ValueDecl *D); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 178 | /// \brief Check if the specified variable is a loop control variable for |
| 179 | /// parent region. |
| 180 | /// \return The index of the loop control variable in the list of associated |
| 181 | /// for-loops (from outer to inner). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 182 | LCDeclInfo isParentLoopControlVariable(ValueDecl *D); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 183 | /// \brief Get the loop control variable for the I-th loop (or nullptr) in |
| 184 | /// parent directive. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 185 | ValueDecl *getParentLoopControlVariable(unsigned I); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 186 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 187 | /// \brief Adds explicit data sharing attribute to the specified declaration. |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 188 | void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, |
| 189 | DeclRefExpr *PrivateCopy = nullptr); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 190 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 191 | /// \brief Returns data sharing attributes from top of the stack for the |
| 192 | /// specified declaration. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 193 | DSAVarData getTopDSA(ValueDecl *D, bool FromParent); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 194 | /// \brief Returns data-sharing attributes for the specified declaration. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 195 | DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 196 | /// \brief Checks if the specified variables has data-sharing attributes which |
| 197 | /// match specified \a CPred predicate in any directive which matches \a DPred |
| 198 | /// predicate. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 199 | DSAVarData hasDSA(ValueDecl *D, |
| 200 | const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 201 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 202 | bool FromParent); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 203 | /// \brief Checks if the specified variables has data-sharing attributes which |
| 204 | /// match specified \a CPred predicate in any innermost directive which |
| 205 | /// matches \a DPred predicate. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 206 | DSAVarData |
| 207 | hasInnermostDSA(ValueDecl *D, |
| 208 | const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 209 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 210 | bool FromParent); |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 211 | /// \brief Checks if the specified variables has explicit data-sharing |
| 212 | /// attributes which match specified \a CPred predicate at the specified |
| 213 | /// OpenMP region. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 214 | bool hasExplicitDSA(ValueDecl *D, |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 215 | const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 216 | unsigned Level, bool NotLastprivate = false); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 217 | |
| 218 | /// \brief Returns true if the directive at level \Level matches in the |
| 219 | /// specified \a DPred predicate. |
| 220 | bool hasExplicitDirective( |
| 221 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 222 | unsigned Level); |
| 223 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 224 | /// \brief Finds a directive which matches specified \a DPred predicate. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 225 | bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind, |
| 226 | const DeclarationNameInfo &, |
| 227 | SourceLocation)> &DPred, |
| 228 | bool FromParent); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 229 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 230 | /// \brief Returns currently analyzed directive. |
| 231 | OpenMPDirectiveKind getCurrentDirective() const { |
| 232 | return Stack.back().Directive; |
| 233 | } |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 234 | /// \brief Returns parent directive. |
| 235 | OpenMPDirectiveKind getParentDirective() const { |
| 236 | if (Stack.size() > 2) |
| 237 | return Stack[Stack.size() - 2].Directive; |
| 238 | return OMPD_unknown; |
| 239 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 240 | |
| 241 | /// \brief Set default data sharing attribute to none. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 242 | void setDefaultDSANone(SourceLocation Loc) { |
| 243 | Stack.back().DefaultAttr = DSA_none; |
| 244 | Stack.back().DefaultAttrLoc = Loc; |
| 245 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 246 | /// \brief Set default data sharing attribute to shared. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 247 | void setDefaultDSAShared(SourceLocation Loc) { |
| 248 | Stack.back().DefaultAttr = DSA_shared; |
| 249 | Stack.back().DefaultAttrLoc = Loc; |
| 250 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 251 | |
| 252 | DefaultDataSharingAttributes getDefaultDSA() const { |
| 253 | return Stack.back().DefaultAttr; |
| 254 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 255 | SourceLocation getDefaultDSALocation() const { |
| 256 | return Stack.back().DefaultAttrLoc; |
| 257 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 258 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 259 | /// \brief Checks if the specified variable is a threadprivate. |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 260 | bool isThreadPrivate(VarDecl *D) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 261 | DSAVarData DVar = getTopDSA(D, false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 262 | return isOpenMPThreadPrivate(DVar.CKind); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 265 | /// \brief Marks current region as ordered (it has an 'ordered' clause). |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 266 | void setOrderedRegion(bool IsOrdered, Expr *Param) { |
| 267 | Stack.back().OrderedRegion.setInt(IsOrdered); |
| 268 | Stack.back().OrderedRegion.setPointer(Param); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 269 | } |
| 270 | /// \brief Returns true, if parent region is ordered (has associated |
| 271 | /// 'ordered' clause), false - otherwise. |
| 272 | bool isParentOrderedRegion() const { |
| 273 | if (Stack.size() > 2) |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 274 | return Stack[Stack.size() - 2].OrderedRegion.getInt(); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 275 | return false; |
| 276 | } |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 277 | /// \brief Returns optional parameter for the ordered region. |
| 278 | Expr *getParentOrderedRegionParam() const { |
| 279 | if (Stack.size() > 2) |
| 280 | return Stack[Stack.size() - 2].OrderedRegion.getPointer(); |
| 281 | return nullptr; |
| 282 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 283 | /// \brief Marks current region as nowait (it has a 'nowait' clause). |
| 284 | void setNowaitRegion(bool IsNowait = true) { |
| 285 | Stack.back().NowaitRegion = IsNowait; |
| 286 | } |
| 287 | /// \brief Returns true, if parent region is nowait (has associated |
| 288 | /// 'nowait' clause), false - otherwise. |
| 289 | bool isParentNowaitRegion() const { |
| 290 | if (Stack.size() > 2) |
| 291 | return Stack[Stack.size() - 2].NowaitRegion; |
| 292 | return false; |
| 293 | } |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 294 | /// \brief Marks parent region as cancel region. |
| 295 | void setParentCancelRegion(bool Cancel = true) { |
| 296 | if (Stack.size() > 2) |
| 297 | Stack[Stack.size() - 2].CancelRegion = |
| 298 | Stack[Stack.size() - 2].CancelRegion || Cancel; |
| 299 | } |
| 300 | /// \brief Return true if current region has inner cancel construct. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 301 | bool isCancelRegion() const { return Stack.back().CancelRegion; } |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 302 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 303 | /// \brief Set collapse value for the region. |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 304 | void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; } |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 305 | /// \brief Return collapse value for region. |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 306 | unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; } |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 307 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 308 | /// \brief Marks current target region as one with closely nested teams |
| 309 | /// region. |
| 310 | void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { |
| 311 | if (Stack.size() > 2) |
| 312 | Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc; |
| 313 | } |
| 314 | /// \brief Returns true, if current region has closely nested teams region. |
| 315 | bool hasInnerTeamsRegion() const { |
| 316 | return getInnerTeamsRegionLoc().isValid(); |
| 317 | } |
| 318 | /// \brief Returns location of the nested teams region (if any). |
| 319 | SourceLocation getInnerTeamsRegionLoc() const { |
| 320 | if (Stack.size() > 1) |
| 321 | return Stack.back().InnerTeamsRegionLoc; |
| 322 | return SourceLocation(); |
| 323 | } |
| 324 | |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 325 | Scope *getCurScope() const { return Stack.back().CurScope; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 326 | Scope *getCurScope() { return Stack.back().CurScope; } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 327 | SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 328 | |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 329 | /// Do the check specified in \a Check to all component lists and return true |
| 330 | /// if any issue is found. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 331 | bool checkMappableExprComponentListsForDecl( |
| 332 | ValueDecl *VD, bool CurrentRegionOnly, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 333 | const llvm::function_ref< |
| 334 | bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| 335 | OpenMPClauseKind)> &Check) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 336 | auto SI = Stack.rbegin(); |
| 337 | auto SE = Stack.rend(); |
| 338 | |
| 339 | if (SI == SE) |
| 340 | return false; |
| 341 | |
| 342 | if (CurrentRegionOnly) { |
| 343 | SE = std::next(SI); |
| 344 | } else { |
| 345 | ++SI; |
| 346 | } |
| 347 | |
| 348 | for (; SI != SE; ++SI) { |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 349 | auto MI = SI->MappedExprComponents.find(VD); |
| 350 | if (MI != SI->MappedExprComponents.end()) |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 351 | for (auto &L : MI->second.Components) |
| 352 | if (Check(L, MI->second.Kind)) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 353 | return true; |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 354 | } |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 355 | return false; |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 358 | /// Create a new mappable expression component list associated with a given |
| 359 | /// declaration and initialize it with the provided list of components. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 360 | void addMappableExpressionComponents( |
| 361 | ValueDecl *VD, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 362 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
| 363 | OpenMPClauseKind WhereFoundClauseKind) { |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 364 | assert(Stack.size() > 1 && |
| 365 | "Not expecting to retrieve components from a empty stack!"); |
| 366 | auto &MEC = Stack.back().MappedExprComponents[VD]; |
| 367 | // Create new entry and append the new components there. |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 368 | MEC.Components.resize(MEC.Components.size() + 1); |
| 369 | MEC.Components.back().append(Components.begin(), Components.end()); |
| 370 | MEC.Kind = WhereFoundClauseKind; |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 371 | } |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 372 | |
| 373 | unsigned getNestingLevel() const { |
| 374 | assert(Stack.size() > 1); |
| 375 | return Stack.size() - 2; |
| 376 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 377 | void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) { |
| 378 | assert(Stack.size() > 2); |
| 379 | assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive)); |
| 380 | Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs}); |
| 381 | } |
| 382 | llvm::iterator_range<DoacrossDependMapTy::const_iterator> |
| 383 | getDoacrossDependClauses() const { |
| 384 | assert(Stack.size() > 1); |
| 385 | if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) { |
| 386 | auto &Ref = Stack[Stack.size() - 1].DoacrossDepends; |
| 387 | return llvm::make_range(Ref.begin(), Ref.end()); |
| 388 | } |
| 389 | return llvm::make_range(Stack[0].DoacrossDepends.end(), |
| 390 | Stack[0].DoacrossDepends.end()); |
| 391 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 392 | }; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 393 | bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 394 | return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) || |
| 395 | isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 396 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 397 | } // namespace |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 398 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 399 | static ValueDecl *getCanonicalDecl(ValueDecl *D) { |
| 400 | auto *VD = dyn_cast<VarDecl>(D); |
| 401 | auto *FD = dyn_cast<FieldDecl>(D); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 402 | if (VD != nullptr) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 403 | VD = VD->getCanonicalDecl(); |
| 404 | D = VD; |
| 405 | } else { |
| 406 | assert(FD); |
| 407 | FD = FD->getCanonicalDecl(); |
| 408 | D = FD; |
| 409 | } |
| 410 | return D; |
| 411 | } |
| 412 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 413 | DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 414 | ValueDecl *D) { |
| 415 | D = getCanonicalDecl(D); |
| 416 | auto *VD = dyn_cast<VarDecl>(D); |
| 417 | auto *FD = dyn_cast<FieldDecl>(D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 418 | DSAVarData DVar; |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 419 | if (Iter == std::prev(Stack.rend())) { |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 420 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 421 | // in a region but not in construct] |
| 422 | // File-scope or namespace-scope variables referenced in called routines |
| 423 | // in the region are shared unless they appear in a threadprivate |
| 424 | // directive. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 425 | if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D)) |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 426 | DVar.CKind = OMPC_shared; |
| 427 | |
| 428 | // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced |
| 429 | // in a region but not in construct] |
| 430 | // Variables with static storage duration that are declared in called |
| 431 | // routines in the region are shared. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 432 | if (VD && VD->hasGlobalStorage()) |
| 433 | DVar.CKind = OMPC_shared; |
| 434 | |
| 435 | // Non-static data members are shared by default. |
| 436 | if (FD) |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 437 | DVar.CKind = OMPC_shared; |
| 438 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 439 | return DVar; |
| 440 | } |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 441 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 442 | DVar.DKind = Iter->Directive; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 443 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 444 | // in a Construct, C/C++, predetermined, p.1] |
| 445 | // Variables with automatic storage duration that are declared in a scope |
| 446 | // inside the construct are private. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 447 | if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && |
| 448 | (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 449 | DVar.CKind = OMPC_private; |
| 450 | return DVar; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 453 | // Explicitly specified attributes and local variables with predetermined |
| 454 | // attributes. |
| 455 | if (Iter->SharingMap.count(D)) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 456 | DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer(); |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 457 | DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 458 | DVar.CKind = Iter->SharingMap[D].Attributes; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 459 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 460 | return DVar; |
| 461 | } |
| 462 | |
| 463 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 464 | // in a Construct, C/C++, implicitly determined, p.1] |
| 465 | // In a parallel or task construct, the data-sharing attributes of these |
| 466 | // variables are determined by the default clause, if present. |
| 467 | switch (Iter->DefaultAttr) { |
| 468 | case DSA_shared: |
| 469 | DVar.CKind = OMPC_shared; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 470 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 471 | return DVar; |
| 472 | case DSA_none: |
| 473 | return DVar; |
| 474 | case DSA_unspecified: |
| 475 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 476 | // in a Construct, implicitly determined, p.2] |
| 477 | // In a parallel construct, if no default clause is present, these |
| 478 | // variables are shared. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 479 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 480 | if (isOpenMPParallelDirective(DVar.DKind) || |
| 481 | isOpenMPTeamsDirective(DVar.DKind)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 482 | DVar.CKind = OMPC_shared; |
| 483 | return DVar; |
| 484 | } |
| 485 | |
| 486 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 487 | // in a Construct, implicitly determined, p.4] |
| 488 | // In a task construct, if no default clause is present, a variable that in |
| 489 | // the enclosing context is determined to be shared by all implicit tasks |
| 490 | // bound to the current team is shared. |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 491 | if (isOpenMPTaskingDirective(DVar.DKind)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 492 | DSAVarData DVarTemp; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 493 | for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 494 | I != EE; ++I) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 495 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 496 | // Referenced in a Construct, implicitly determined, p.6] |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 497 | // In a task construct, if no default clause is present, a variable |
| 498 | // whose data-sharing attribute is not determined by the rules above is |
| 499 | // firstprivate. |
| 500 | DVarTemp = getDSA(I, D); |
| 501 | if (DVarTemp.CKind != OMPC_shared) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 502 | DVar.RefExpr = nullptr; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 503 | DVar.CKind = OMPC_firstprivate; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 504 | return DVar; |
| 505 | } |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 506 | if (isParallelOrTaskRegion(I->Directive)) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 507 | break; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 508 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 509 | DVar.CKind = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 510 | (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 511 | return DVar; |
| 512 | } |
| 513 | } |
| 514 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 515 | // in a Construct, implicitly determined, p.3] |
| 516 | // For constructs other than task, if no default clause is present, these |
| 517 | // variables inherit their data-sharing attributes from the enclosing |
| 518 | // context. |
Dmitry Polukhin | dc78bc82 | 2016-04-01 09:52:30 +0000 | [diff] [blame] | 519 | return getDSA(++Iter, D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 522 | Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) { |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 523 | assert(Stack.size() > 1 && "Data sharing attributes stack is empty"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 524 | D = getCanonicalDecl(D); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 525 | auto It = Stack.back().AlignedMap.find(D); |
| 526 | if (It == Stack.back().AlignedMap.end()) { |
| 527 | assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); |
| 528 | Stack.back().AlignedMap[D] = NewDE; |
| 529 | return nullptr; |
| 530 | } else { |
| 531 | assert(It->second && "Unexpected nullptr expr in the aligned map"); |
| 532 | return It->second; |
| 533 | } |
| 534 | return nullptr; |
| 535 | } |
| 536 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 537 | void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) { |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 538 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 539 | D = getCanonicalDecl(D); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 540 | Stack.back().LCVMap.insert( |
| 541 | std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture))); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 544 | DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) { |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 545 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 546 | D = getCanonicalDecl(D); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 547 | return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] |
| 548 | : LCDeclInfo(0, nullptr); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 551 | DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) { |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 552 | assert(Stack.size() > 2 && "Data-sharing attributes stack is empty"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 553 | D = getCanonicalDecl(D); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 554 | return Stack[Stack.size() - 2].LCVMap.count(D) > 0 |
| 555 | ? Stack[Stack.size() - 2].LCVMap[D] |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 556 | : LCDeclInfo(0, nullptr); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 559 | ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) { |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 560 | assert(Stack.size() > 2 && "Data-sharing attributes stack is empty"); |
| 561 | if (Stack[Stack.size() - 2].LCVMap.size() < I) |
| 562 | return nullptr; |
| 563 | for (auto &Pair : Stack[Stack.size() - 2].LCVMap) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 564 | if (Pair.second.first == I) |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 565 | return Pair.first; |
| 566 | } |
| 567 | return nullptr; |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 570 | void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, |
| 571 | DeclRefExpr *PrivateCopy) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 572 | D = getCanonicalDecl(D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 573 | if (A == OMPC_threadprivate) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 574 | auto &Data = Stack[0].SharingMap[D]; |
| 575 | Data.Attributes = A; |
| 576 | Data.RefExpr.setPointer(E); |
| 577 | Data.PrivateCopy = nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 578 | } else { |
| 579 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 580 | auto &Data = Stack.back().SharingMap[D]; |
| 581 | assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || |
| 582 | (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || |
| 583 | (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || |
| 584 | (isLoopControlVariable(D).first && A == OMPC_private)); |
| 585 | if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { |
| 586 | Data.RefExpr.setInt(/*IntVal=*/true); |
| 587 | return; |
| 588 | } |
| 589 | const bool IsLastprivate = |
| 590 | A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; |
| 591 | Data.Attributes = A; |
| 592 | Data.RefExpr.setPointerAndInt(E, IsLastprivate); |
| 593 | Data.PrivateCopy = PrivateCopy; |
| 594 | if (PrivateCopy) { |
| 595 | auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()]; |
| 596 | Data.Attributes = A; |
| 597 | Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); |
| 598 | Data.PrivateCopy = nullptr; |
| 599 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 603 | bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 604 | D = D->getCanonicalDecl(); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 605 | if (Stack.size() > 2) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 606 | reverse_iterator I = Iter, E = std::prev(Stack.rend()); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 607 | Scope *TopScope = nullptr; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 608 | while (I != E && !isParallelOrTaskRegion(I->Directive)) { |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 609 | ++I; |
| 610 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 611 | if (I == E) |
| 612 | return false; |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 613 | TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 614 | Scope *CurScope = getCurScope(); |
| 615 | while (CurScope != TopScope && !CurScope->isDeclScope(D)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 616 | CurScope = CurScope->getParent(); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 617 | } |
| 618 | return CurScope != TopScope; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 619 | } |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 620 | return false; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 623 | /// \brief Build a variable declaration for OpenMP loop iteration variable. |
| 624 | static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 625 | StringRef Name, const AttrVec *Attrs = nullptr) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 626 | DeclContext *DC = SemaRef.CurContext; |
| 627 | IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); |
| 628 | TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); |
| 629 | VarDecl *Decl = |
| 630 | VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 631 | if (Attrs) { |
| 632 | for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); |
| 633 | I != E; ++I) |
| 634 | Decl->addAttr(*I); |
| 635 | } |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 636 | Decl->setImplicit(); |
| 637 | return Decl; |
| 638 | } |
| 639 | |
| 640 | static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, |
| 641 | SourceLocation Loc, |
| 642 | bool RefersToCapture = false) { |
| 643 | D->setReferenced(); |
| 644 | D->markUsed(S.Context); |
| 645 | return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), |
| 646 | SourceLocation(), D, RefersToCapture, Loc, Ty, |
| 647 | VK_LValue); |
| 648 | } |
| 649 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 650 | DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) { |
| 651 | D = getCanonicalDecl(D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 652 | DSAVarData DVar; |
| 653 | |
| 654 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 655 | // in a Construct, C/C++, predetermined, p.1] |
| 656 | // Variables appearing in threadprivate directives are threadprivate. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 657 | auto *VD = dyn_cast<VarDecl>(D); |
| 658 | if ((VD && VD->getTLSKind() != VarDecl::TLS_None && |
| 659 | !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 660 | SemaRef.getLangOpts().OpenMPUseTLS && |
| 661 | SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 662 | (VD && VD->getStorageClass() == SC_Register && |
| 663 | VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { |
| 664 | addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 665 | D->getLocation()), |
Alexey Bataev | f2453a0 | 2015-05-06 07:25:08 +0000 | [diff] [blame] | 666 | OMPC_threadprivate); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 667 | } |
| 668 | if (Stack[0].SharingMap.count(D)) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 669 | DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 670 | DVar.CKind = OMPC_threadprivate; |
| 671 | return DVar; |
| 672 | } |
| 673 | |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 674 | if (Stack.size() == 1) { |
| 675 | // Not in OpenMP execution region and top scope was already checked. |
| 676 | return DVar; |
| 677 | } |
| 678 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 679 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
Alexey Bataev | dffa93a | 2015-12-10 08:20:58 +0000 | [diff] [blame] | 680 | // in a Construct, C/C++, predetermined, p.4] |
| 681 | // Static data members are shared. |
| 682 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 683 | // in a Construct, C/C++, predetermined, p.7] |
| 684 | // Variables with static storage duration that are declared in a scope |
| 685 | // inside the construct are shared. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 686 | auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; }; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 687 | if (VD && VD->isStaticDataMember()) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 688 | DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); |
Alexey Bataev | dffa93a | 2015-12-10 08:20:58 +0000 | [diff] [blame] | 689 | if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 690 | return DVar; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 691 | |
Alexey Bataev | dffa93a | 2015-12-10 08:20:58 +0000 | [diff] [blame] | 692 | DVar.CKind = OMPC_shared; |
| 693 | return DVar; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | QualType Type = D->getType().getNonReferenceType().getCanonicalType(); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 697 | bool IsConstant = Type.isConstant(SemaRef.getASTContext()); |
| 698 | Type = SemaRef.getASTContext().getBaseElementType(Type); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 699 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 700 | // in a Construct, C/C++, predetermined, p.6] |
| 701 | // Variables with const qualified type having no mutable member are |
| 702 | // shared. |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 703 | CXXRecordDecl *RD = |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 704 | SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; |
Alexey Bataev | c9bd03d | 2015-12-17 06:55:08 +0000 | [diff] [blame] | 705 | if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) |
| 706 | if (auto *CTD = CTSD->getSpecializedTemplate()) |
| 707 | RD = CTD->getTemplatedDecl(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 708 | if (IsConstant && |
Alexey Bataev | 4bcad7f | 2016-02-10 10:50:12 +0000 | [diff] [blame] | 709 | !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() && |
| 710 | RD->hasMutableFields())) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 711 | // Variables with const-qualified type having no mutable member may be |
| 712 | // listed in a firstprivate clause, even if they are static data members. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 713 | DSAVarData DVarTemp = hasDSA( |
| 714 | D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; }, |
| 715 | MatchesAlways, FromParent); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 716 | if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) |
| 717 | return DVar; |
| 718 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 719 | DVar.CKind = OMPC_shared; |
| 720 | return DVar; |
| 721 | } |
| 722 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 723 | // Explicitly specified attributes and local variables with predetermined |
| 724 | // attributes. |
Alexey Bataev | dffa93a | 2015-12-10 08:20:58 +0000 | [diff] [blame] | 725 | auto StartI = std::next(Stack.rbegin()); |
| 726 | auto EndI = std::prev(Stack.rend()); |
| 727 | if (FromParent && StartI != EndI) { |
| 728 | StartI = std::next(StartI); |
| 729 | } |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 730 | auto I = std::prev(StartI); |
| 731 | if (I->SharingMap.count(D)) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 732 | DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer(); |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 733 | DVar.PrivateCopy = I->SharingMap[D].PrivateCopy; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 734 | DVar.CKind = I->SharingMap[D].Attributes; |
| 735 | DVar.ImplicitDSALoc = I->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | return DVar; |
| 739 | } |
| 740 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 741 | DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, |
| 742 | bool FromParent) { |
| 743 | D = getCanonicalDecl(D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 744 | auto StartI = Stack.rbegin(); |
| 745 | auto EndI = std::prev(Stack.rend()); |
| 746 | if (FromParent && StartI != EndI) { |
| 747 | StartI = std::next(StartI); |
| 748 | } |
| 749 | return getDSA(StartI, D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 752 | DSAStackTy::DSAVarData |
| 753 | DSAStackTy::hasDSA(ValueDecl *D, |
| 754 | const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 755 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 756 | bool FromParent) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 757 | D = getCanonicalDecl(D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 758 | auto StartI = std::next(Stack.rbegin()); |
Dmitry Polukhin | dc78bc82 | 2016-04-01 09:52:30 +0000 | [diff] [blame] | 759 | auto EndI = Stack.rend(); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 760 | if (FromParent && StartI != EndI) { |
| 761 | StartI = std::next(StartI); |
| 762 | } |
| 763 | for (auto I = StartI, EE = EndI; I != EE; ++I) { |
| 764 | if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 765 | continue; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 766 | DSAVarData DVar = getDSA(I, D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 767 | if (CPred(DVar.CKind)) |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 768 | return DVar; |
| 769 | } |
| 770 | return DSAVarData(); |
| 771 | } |
| 772 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 773 | DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( |
| 774 | ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
| 775 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 776 | bool FromParent) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 777 | D = getCanonicalDecl(D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 778 | auto StartI = std::next(Stack.rbegin()); |
Dmitry Polukhin | dc78bc82 | 2016-04-01 09:52:30 +0000 | [diff] [blame] | 779 | auto EndI = Stack.rend(); |
Alexey Bataev | e397812 | 2016-07-19 05:06:39 +0000 | [diff] [blame] | 780 | if (FromParent && StartI != EndI) |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 781 | StartI = std::next(StartI); |
Alexey Bataev | e397812 | 2016-07-19 05:06:39 +0000 | [diff] [blame] | 782 | if (StartI == EndI || !DPred(StartI->Directive)) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 783 | return DSAVarData(); |
Alexey Bataev | e397812 | 2016-07-19 05:06:39 +0000 | [diff] [blame] | 784 | DSAVarData DVar = getDSA(StartI, D); |
| 785 | return CPred(DVar.CKind) ? DVar : DSAVarData(); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 788 | bool DSAStackTy::hasExplicitDSA( |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 789 | ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 790 | unsigned Level, bool NotLastprivate) { |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 791 | if (CPred(ClauseKindMode)) |
| 792 | return true; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 793 | D = getCanonicalDecl(D); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 794 | auto StartI = std::next(Stack.begin()); |
| 795 | auto EndI = Stack.end(); |
NAKAMURA Takumi | 0332eda | 2015-06-23 10:01:20 +0000 | [diff] [blame] | 796 | if (std::distance(StartI, EndI) <= (int)Level) |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 797 | return false; |
| 798 | std::advance(StartI, Level); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 799 | return (StartI->SharingMap.count(D) > 0) && |
| 800 | StartI->SharingMap[D].RefExpr.getPointer() && |
| 801 | CPred(StartI->SharingMap[D].Attributes) && |
| 802 | (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt()); |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 805 | bool DSAStackTy::hasExplicitDirective( |
| 806 | const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, |
| 807 | unsigned Level) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 808 | auto StartI = std::next(Stack.begin()); |
| 809 | auto EndI = Stack.end(); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 810 | if (std::distance(StartI, EndI) <= (int)Level) |
| 811 | return false; |
| 812 | std::advance(StartI, Level); |
| 813 | return DPred(StartI->Directive); |
| 814 | } |
| 815 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 816 | bool DSAStackTy::hasDirective( |
| 817 | const llvm::function_ref<bool(OpenMPDirectiveKind, |
| 818 | const DeclarationNameInfo &, SourceLocation)> |
| 819 | &DPred, |
| 820 | bool FromParent) { |
Samuel Antao | f0d7975 | 2016-05-27 15:21:27 +0000 | [diff] [blame] | 821 | // We look only in the enclosing region. |
| 822 | if (Stack.size() < 2) |
| 823 | return false; |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 824 | auto StartI = std::next(Stack.rbegin()); |
| 825 | auto EndI = std::prev(Stack.rend()); |
| 826 | if (FromParent && StartI != EndI) { |
| 827 | StartI = std::next(StartI); |
| 828 | } |
| 829 | for (auto I = StartI, EE = EndI; I != EE; ++I) { |
| 830 | if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) |
| 831 | return true; |
| 832 | } |
| 833 | return false; |
| 834 | } |
| 835 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 836 | void Sema::InitDataSharingAttributesStack() { |
| 837 | VarDataSharingAttributesStack = new DSAStackTy(*this); |
| 838 | } |
| 839 | |
| 840 | #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) |
| 841 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 842 | bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 843 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
| 844 | |
| 845 | auto &Ctx = getASTContext(); |
| 846 | bool IsByRef = true; |
| 847 | |
| 848 | // Find the directive that is associated with the provided scope. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 849 | auto Ty = D->getType(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 850 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 851 | if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 852 | // This table summarizes how a given variable should be passed to the device |
| 853 | // given its type and the clauses where it appears. This table is based on |
| 854 | // the description in OpenMP 4.5 [2.10.4, target Construct] and |
| 855 | // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. |
| 856 | // |
| 857 | // ========================================================================= |
| 858 | // | type | defaultmap | pvt | first | is_device_ptr | map | res. | |
| 859 | // | |(tofrom:scalar)| | pvt | | | | |
| 860 | // ========================================================================= |
| 861 | // | scl | | | | - | | bycopy| |
| 862 | // | scl | | - | x | - | - | bycopy| |
| 863 | // | scl | | x | - | - | - | null | |
| 864 | // | scl | x | | | - | | byref | |
| 865 | // | scl | x | - | x | - | - | bycopy| |
| 866 | // | scl | x | x | - | - | - | null | |
| 867 | // | scl | | - | - | - | x | byref | |
| 868 | // | scl | x | - | - | - | x | byref | |
| 869 | // |
| 870 | // | agg | n.a. | | | - | | byref | |
| 871 | // | agg | n.a. | - | x | - | - | byref | |
| 872 | // | agg | n.a. | x | - | - | - | null | |
| 873 | // | agg | n.a. | - | - | - | x | byref | |
| 874 | // | agg | n.a. | - | - | - | x[] | byref | |
| 875 | // |
| 876 | // | ptr | n.a. | | | - | | bycopy| |
| 877 | // | ptr | n.a. | - | x | - | - | bycopy| |
| 878 | // | ptr | n.a. | x | - | - | - | null | |
| 879 | // | ptr | n.a. | - | - | - | x | byref | |
| 880 | // | ptr | n.a. | - | - | - | x[] | bycopy| |
| 881 | // | ptr | n.a. | - | - | x | | bycopy| |
| 882 | // | ptr | n.a. | - | - | x | x | bycopy| |
| 883 | // | ptr | n.a. | - | - | x | x[] | bycopy| |
| 884 | // ========================================================================= |
| 885 | // Legend: |
| 886 | // scl - scalar |
| 887 | // ptr - pointer |
| 888 | // agg - aggregate |
| 889 | // x - applies |
| 890 | // - - invalid in this combination |
| 891 | // [] - mapped with an array section |
| 892 | // byref - should be mapped by reference |
| 893 | // byval - should be mapped by value |
| 894 | // null - initialize a local variable to null on the device |
| 895 | // |
| 896 | // Observations: |
| 897 | // - All scalar declarations that show up in a map clause have to be passed |
| 898 | // by reference, because they may have been mapped in the enclosing data |
| 899 | // environment. |
| 900 | // - If the scalar value does not fit the size of uintptr, it has to be |
| 901 | // passed by reference, regardless the result in the table above. |
| 902 | // - For pointers mapped by value that have either an implicit map or an |
| 903 | // array section, the runtime library may pass the NULL value to the |
| 904 | // device instead of the value passed to it by the compiler. |
| 905 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 906 | if (Ty->isReferenceType()) |
| 907 | Ty = Ty->castAs<ReferenceType>()->getPointeeType(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 908 | |
| 909 | // Locate map clauses and see if the variable being captured is referred to |
| 910 | // in any of those clauses. Here we only care about variables, not fields, |
| 911 | // because fields are part of aggregates. |
| 912 | bool IsVariableUsedInMapClause = false; |
| 913 | bool IsVariableAssociatedWithSection = false; |
| 914 | |
| 915 | DSAStack->checkMappableExprComponentListsForDecl( |
| 916 | D, /*CurrentRegionOnly=*/true, |
| 917 | [&](OMPClauseMappableExprCommon::MappableExprComponentListRef |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 918 | MapExprComponents, |
| 919 | OpenMPClauseKind WhereFoundClauseKind) { |
| 920 | // Only the map clause information influences how a variable is |
| 921 | // captured. E.g. is_device_ptr does not require changing the default |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 922 | // behavior. |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 923 | if (WhereFoundClauseKind != OMPC_map) |
| 924 | return false; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 925 | |
| 926 | auto EI = MapExprComponents.rbegin(); |
| 927 | auto EE = MapExprComponents.rend(); |
| 928 | |
| 929 | assert(EI != EE && "Invalid map expression!"); |
| 930 | |
| 931 | if (isa<DeclRefExpr>(EI->getAssociatedExpression())) |
| 932 | IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; |
| 933 | |
| 934 | ++EI; |
| 935 | if (EI == EE) |
| 936 | return false; |
| 937 | |
| 938 | if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || |
| 939 | isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || |
| 940 | isa<MemberExpr>(EI->getAssociatedExpression())) { |
| 941 | IsVariableAssociatedWithSection = true; |
| 942 | // There is nothing more we need to know about this variable. |
| 943 | return true; |
| 944 | } |
| 945 | |
| 946 | // Keep looking for more map info. |
| 947 | return false; |
| 948 | }); |
| 949 | |
| 950 | if (IsVariableUsedInMapClause) { |
| 951 | // If variable is identified in a map clause it is always captured by |
| 952 | // reference except if it is a pointer that is dereferenced somehow. |
| 953 | IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); |
| 954 | } else { |
| 955 | // By default, all the data that has a scalar type is mapped by copy. |
| 956 | IsByRef = !Ty->isScalarType(); |
| 957 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 960 | if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { |
| 961 | IsByRef = !DSAStack->hasExplicitDSA( |
| 962 | D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, |
| 963 | Level, /*NotLastprivate=*/true); |
| 964 | } |
| 965 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 966 | // 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] | 967 | // and alignment, because the runtime library only deals with uintptr types. |
| 968 | // If it does not fit the uintptr size, we need to pass the data by reference |
| 969 | // instead. |
| 970 | if (!IsByRef && |
| 971 | (Ctx.getTypeSizeInChars(Ty) > |
| 972 | Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 973 | Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 974 | IsByRef = true; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 975 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 976 | |
| 977 | return IsByRef; |
| 978 | } |
| 979 | |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 980 | unsigned Sema::getOpenMPNestingLevel() const { |
| 981 | assert(getLangOpts().OpenMP); |
| 982 | return DSAStack->getNestingLevel(); |
| 983 | } |
| 984 | |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 985 | VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) { |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 986 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 987 | D = getCanonicalDecl(D); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 988 | |
| 989 | // If we are attempting to capture a global variable in a directive with |
| 990 | // 'target' we return true so that this global is also mapped to the device. |
| 991 | // |
| 992 | // FIXME: If the declaration is enclosed in a 'declare target' directive, |
| 993 | // then it should not be captured. Therefore, an extra check has to be |
| 994 | // inserted here once support for 'declare target' is added. |
| 995 | // |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 996 | auto *VD = dyn_cast<VarDecl>(D); |
| 997 | if (VD && !VD->hasLocalStorage()) { |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 998 | if (DSAStack->getCurrentDirective() == OMPD_target && |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 999 | !DSAStack->isClauseParsingMode()) |
| 1000 | return VD; |
Samuel Antao | f0d7975 | 2016-05-27 15:21:27 +0000 | [diff] [blame] | 1001 | if (DSAStack->hasDirective( |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1002 | [](OpenMPDirectiveKind K, const DeclarationNameInfo &, |
| 1003 | SourceLocation) -> bool { |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 1004 | return isOpenMPTargetExecutionDirective(K); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1005 | }, |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1006 | false)) |
| 1007 | return VD; |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Alexey Bataev | 48977c3 | 2015-08-04 08:10:48 +0000 | [diff] [blame] | 1010 | if (DSAStack->getCurrentDirective() != OMPD_unknown && |
| 1011 | (!DSAStack->isClauseParsingMode() || |
| 1012 | DSAStack->getParentDirective() != OMPD_unknown)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 1013 | auto &&Info = DSAStack->isLoopControlVariable(D); |
| 1014 | if (Info.first || |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1015 | (VD && VD->hasLocalStorage() && |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 1016 | isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1017 | (VD && DSAStack->isForceVarCapturing())) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 1018 | return VD ? VD : Info.second; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1019 | auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 1020 | if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1021 | return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1022 | DVarPrivate = DSAStack->hasDSA( |
| 1023 | D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, |
| 1024 | DSAStack->isClauseParsingMode()); |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1025 | if (DVarPrivate.CKind != OMPC_unknown) |
| 1026 | return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 1027 | } |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1028 | return nullptr; |
Alexey Bataev | f841bd9 | 2014-12-16 07:00:22 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1031 | bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) { |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 1032 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
| 1033 | return DSAStack->hasExplicitDSA( |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1034 | D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level); |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1037 | bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) { |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1038 | assert(LangOpts.OpenMP && "OpenMP is not allowed"); |
| 1039 | // Return true if the current level is no longer enclosed in a target region. |
| 1040 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1041 | auto *VD = dyn_cast<VarDecl>(D); |
| 1042 | return VD && !VD->hasLocalStorage() && |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 1043 | DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, |
| 1044 | Level); |
Samuel Antao | 4be30e9 | 2015-10-02 17:14:03 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1047 | void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1048 | |
| 1049 | void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, |
| 1050 | const DeclarationNameInfo &DirName, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1051 | Scope *CurScope, SourceLocation Loc) { |
| 1052 | DSAStack->push(DKind, DirName, CurScope, Loc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1053 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
| 1054 | } |
| 1055 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 1056 | void Sema::StartOpenMPClause(OpenMPClauseKind K) { |
| 1057 | DSAStack->setClauseParsingMode(K); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Alexey Bataev | aac108a | 2015-06-23 04:51:00 +0000 | [diff] [blame] | 1060 | void Sema::EndOpenMPClause() { |
| 1061 | DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1064 | void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1065 | // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] |
| 1066 | // A variable of class type (or array thereof) that appears in a lastprivate |
| 1067 | // clause requires an accessible, unambiguous default constructor for the |
| 1068 | // class type, unless the list item is also specified in a firstprivate |
| 1069 | // clause. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1070 | if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1071 | for (auto *C : D->clauses()) { |
| 1072 | if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { |
| 1073 | SmallVector<Expr *, 8> PrivateCopies; |
| 1074 | for (auto *DE : Clause->varlists()) { |
| 1075 | if (DE->isValueDependent() || DE->isTypeDependent()) { |
| 1076 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1077 | continue; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1078 | } |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 1079 | auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1080 | VarDecl *VD = cast<VarDecl>(DRE->getDecl()); |
| 1081 | QualType Type = VD->getType().getNonReferenceType(); |
| 1082 | auto DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1083 | if (DVar.CKind == OMPC_lastprivate) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1084 | // Generate helper private variable and initialize it with the |
| 1085 | // default value. The address of the original variable is replaced |
| 1086 | // by the address of the new private variable in CodeGen. This new |
| 1087 | // variable is not added to IdResolver, so the code in the OpenMP |
| 1088 | // region uses original variable for proper diagnostics. |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 1089 | auto *VDPrivate = buildVarDecl( |
| 1090 | *this, DE->getExprLoc(), Type.getUnqualifiedType(), |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1091 | VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1092 | ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); |
| 1093 | if (VDPrivate->isInvalidDecl()) |
| 1094 | continue; |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1095 | PrivateCopies.push_back(buildDeclRefExpr( |
| 1096 | *this, VDPrivate, DE->getType(), DE->getExprLoc())); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1097 | } else { |
| 1098 | // The variable is also a firstprivate, so initialization sequence |
| 1099 | // for private copy is generated already. |
| 1100 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1101 | } |
| 1102 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1103 | // Set initializers to private copies if no errors were found. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1104 | if (PrivateCopies.size() == Clause->varlist_size()) |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1105 | Clause->setPrivateCopies(PrivateCopies); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1110 | DSAStack->pop(); |
| 1111 | DiscardCleanupsInEvaluationContext(); |
| 1112 | PopExpressionEvaluationContext(); |
| 1113 | } |
| 1114 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1115 | static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, |
| 1116 | Expr *NumIterations, Sema &SemaRef, |
| 1117 | Scope *S, DSAStackTy *Stack); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1118 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1119 | namespace { |
| 1120 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1121 | class VarDeclFilterCCC : public CorrectionCandidateCallback { |
| 1122 | private: |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1123 | Sema &SemaRef; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1124 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1125 | public: |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1126 | explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1127 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1128 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1129 | if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1130 | return VD->hasGlobalStorage() && |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1131 | SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), |
| 1132 | SemaRef.getCurScope()); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1133 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1134 | return false; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1135 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1136 | }; |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 1137 | |
| 1138 | class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback { |
| 1139 | private: |
| 1140 | Sema &SemaRef; |
| 1141 | |
| 1142 | public: |
| 1143 | explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} |
| 1144 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| 1145 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 1146 | if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { |
| 1147 | return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), |
| 1148 | SemaRef.getCurScope()); |
| 1149 | } |
| 1150 | return false; |
| 1151 | } |
| 1152 | }; |
| 1153 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1154 | } // namespace |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1155 | |
| 1156 | ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, |
| 1157 | CXXScopeSpec &ScopeSpec, |
| 1158 | const DeclarationNameInfo &Id) { |
| 1159 | LookupResult Lookup(*this, Id, LookupOrdinaryName); |
| 1160 | LookupParsedName(Lookup, CurScope, &ScopeSpec, true); |
| 1161 | |
| 1162 | if (Lookup.isAmbiguous()) |
| 1163 | return ExprError(); |
| 1164 | |
| 1165 | VarDecl *VD; |
| 1166 | if (!Lookup.isSingleResult()) { |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1167 | if (TypoCorrection Corrected = CorrectTypo( |
| 1168 | Id, LookupOrdinaryName, CurScope, nullptr, |
| 1169 | llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1170 | diagnoseTypo(Corrected, |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 1171 | PDiag(Lookup.empty() |
| 1172 | ? diag::err_undeclared_var_use_suggest |
| 1173 | : diag::err_omp_expected_var_arg_suggest) |
| 1174 | << Id.getName()); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1175 | VD = Corrected.getCorrectionDeclAs<VarDecl>(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1176 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1177 | Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use |
| 1178 | : diag::err_omp_expected_var_arg) |
| 1179 | << Id.getName(); |
| 1180 | return ExprError(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1181 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1182 | } else { |
| 1183 | if (!(VD = Lookup.getAsSingle<VarDecl>())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1184 | Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1185 | Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); |
| 1186 | return ExprError(); |
| 1187 | } |
| 1188 | } |
| 1189 | Lookup.suppressDiagnostics(); |
| 1190 | |
| 1191 | // OpenMP [2.9.2, Syntax, C/C++] |
| 1192 | // Variables must be file-scope, namespace-scope, or static block-scope. |
| 1193 | if (!VD->hasGlobalStorage()) { |
| 1194 | Diag(Id.getLoc(), diag::err_omp_global_var_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1195 | << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); |
| 1196 | bool IsDecl = |
| 1197 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1198 | Diag(VD->getLocation(), |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1199 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1200 | << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1201 | return ExprError(); |
| 1202 | } |
| 1203 | |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1204 | VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 1205 | NamedDecl *ND = cast<NamedDecl>(CanonicalVD); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1206 | // OpenMP [2.9.2, Restrictions, C/C++, p.2] |
| 1207 | // A threadprivate directive for file-scope variables must appear outside |
| 1208 | // any definition or declaration. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1209 | if (CanonicalVD->getDeclContext()->isTranslationUnit() && |
| 1210 | !getCurLexicalContext()->isTranslationUnit()) { |
| 1211 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1212 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 1213 | bool IsDecl = |
| 1214 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1215 | Diag(VD->getLocation(), |
| 1216 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1217 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1218 | return ExprError(); |
| 1219 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1220 | // OpenMP [2.9.2, Restrictions, C/C++, p.3] |
| 1221 | // A threadprivate directive for static class member variables must appear |
| 1222 | // in the class definition, in the same scope in which the member |
| 1223 | // variables are declared. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1224 | if (CanonicalVD->isStaticDataMember() && |
| 1225 | !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { |
| 1226 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1227 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 1228 | bool IsDecl = |
| 1229 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1230 | Diag(VD->getLocation(), |
| 1231 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1232 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1233 | return ExprError(); |
| 1234 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1235 | // OpenMP [2.9.2, Restrictions, C/C++, p.4] |
| 1236 | // A threadprivate directive for namespace-scope variables must appear |
| 1237 | // outside any definition or declaration other than the namespace |
| 1238 | // definition itself. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1239 | if (CanonicalVD->getDeclContext()->isNamespace() && |
| 1240 | (!getCurLexicalContext()->isFileContext() || |
| 1241 | !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { |
| 1242 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1243 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 1244 | bool IsDecl = |
| 1245 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1246 | Diag(VD->getLocation(), |
| 1247 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1248 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1249 | return ExprError(); |
| 1250 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1251 | // OpenMP [2.9.2, Restrictions, C/C++, p.6] |
| 1252 | // A threadprivate directive for static block-scope variables must appear |
| 1253 | // in the scope of the variable and not in a nested scope. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1254 | if (CanonicalVD->isStaticLocal() && CurScope && |
| 1255 | !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1256 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1257 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 1258 | bool IsDecl = |
| 1259 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1260 | Diag(VD->getLocation(), |
| 1261 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1262 | << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1263 | return ExprError(); |
| 1264 | } |
| 1265 | |
| 1266 | // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] |
| 1267 | // A threadprivate directive must lexically precede all references to any |
| 1268 | // of the variables in its list. |
Alexey Bataev | 6ddfe1a | 2015-04-16 13:49:42 +0000 | [diff] [blame] | 1269 | if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1270 | Diag(Id.getLoc(), diag::err_omp_var_used) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1271 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1272 | return ExprError(); |
| 1273 | } |
| 1274 | |
| 1275 | QualType ExprType = VD->getType().getNonReferenceType(); |
Alexey Bataev | 376b4a4 | 2016-02-09 09:41:09 +0000 | [diff] [blame] | 1276 | return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), |
| 1277 | SourceLocation(), VD, |
| 1278 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1279 | Id.getLoc(), ExprType, VK_LValue); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1282 | Sema::DeclGroupPtrTy |
| 1283 | Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, |
| 1284 | ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1285 | if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1286 | CurContext->addDecl(D); |
| 1287 | return DeclGroupPtrTy::make(DeclGroupRef(D)); |
| 1288 | } |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1289 | return nullptr; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1292 | namespace { |
| 1293 | class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { |
| 1294 | Sema &SemaRef; |
| 1295 | |
| 1296 | public: |
| 1297 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1298 | if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1299 | if (VD->hasLocalStorage()) { |
| 1300 | SemaRef.Diag(E->getLocStart(), |
| 1301 | diag::err_omp_local_var_in_threadprivate_init) |
| 1302 | << E->getSourceRange(); |
| 1303 | SemaRef.Diag(VD->getLocation(), diag::note_defined_here) |
| 1304 | << VD << VD->getSourceRange(); |
| 1305 | return true; |
| 1306 | } |
| 1307 | } |
| 1308 | return false; |
| 1309 | } |
| 1310 | bool VisitStmt(const Stmt *S) { |
| 1311 | for (auto Child : S->children()) { |
| 1312 | if (Child && Visit(Child)) |
| 1313 | return true; |
| 1314 | } |
| 1315 | return false; |
| 1316 | } |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1317 | explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1318 | }; |
| 1319 | } // namespace |
| 1320 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1321 | OMPThreadPrivateDecl * |
| 1322 | Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1323 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1324 | for (auto &RefExpr : VarList) { |
| 1325 | DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1326 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 1327 | SourceLocation ILoc = DE->getExprLoc(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1328 | |
Alexey Bataev | 376b4a4 | 2016-02-09 09:41:09 +0000 | [diff] [blame] | 1329 | // Mark variable as used. |
| 1330 | VD->setReferenced(); |
| 1331 | VD->markUsed(Context); |
| 1332 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 1333 | QualType QType = VD->getType(); |
| 1334 | if (QType->isDependentType() || QType->isInstantiationDependentType()) { |
| 1335 | // It will be analyzed later. |
| 1336 | Vars.push_back(DE); |
| 1337 | continue; |
| 1338 | } |
| 1339 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1340 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 1341 | // A threadprivate variable must not have an incomplete type. |
| 1342 | if (RequireCompleteType(ILoc, VD->getType(), |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1343 | diag::err_omp_threadprivate_incomplete_type)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1344 | continue; |
| 1345 | } |
| 1346 | |
| 1347 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 1348 | // A threadprivate variable must not have a reference type. |
| 1349 | if (VD->getType()->isReferenceType()) { |
| 1350 | Diag(ILoc, diag::err_omp_ref_type_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1351 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); |
| 1352 | bool IsDecl = |
| 1353 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1354 | Diag(VD->getLocation(), |
| 1355 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1356 | << VD; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1357 | continue; |
| 1358 | } |
| 1359 | |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1360 | // Check if this is a TLS variable. If TLS is not being supported, produce |
| 1361 | // the corresponding diagnostic. |
| 1362 | if ((VD->getTLSKind() != VarDecl::TLS_None && |
| 1363 | !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && |
| 1364 | getLangOpts().OpenMPUseTLS && |
| 1365 | getASTContext().getTargetInfo().isTLSSupported())) || |
Alexey Bataev | 1a8b3f1 | 2015-05-06 06:34:55 +0000 | [diff] [blame] | 1366 | (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && |
| 1367 | !VD->isLocalVarDecl())) { |
Alexey Bataev | 26a3924 | 2015-01-13 03:35:30 +0000 | [diff] [blame] | 1368 | Diag(ILoc, diag::err_omp_var_thread_local) |
| 1369 | << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1370 | bool IsDecl = |
| 1371 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1372 | Diag(VD->getLocation(), |
| 1373 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1374 | << VD; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1375 | continue; |
| 1376 | } |
| 1377 | |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1378 | // Check if initial value of threadprivate variable reference variable with |
| 1379 | // local storage (it is not supported by runtime). |
| 1380 | if (auto Init = VD->getAnyInitializer()) { |
| 1381 | LocalVarRefChecker Checker(*this); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1382 | if (Checker.Visit(Init)) |
| 1383 | continue; |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1386 | Vars.push_back(RefExpr); |
Alexey Bataev | d178ad4 | 2014-03-07 08:03:37 +0000 | [diff] [blame] | 1387 | DSAStack->addDSA(VD, DE, OMPC_threadprivate); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1388 | VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( |
| 1389 | Context, SourceRange(Loc, Loc))); |
| 1390 | if (auto *ML = Context.getASTMutationListener()) |
| 1391 | ML->DeclarationMarkedOpenMPThreadPrivate(VD); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1392 | } |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 1393 | OMPThreadPrivateDecl *D = nullptr; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 1394 | if (!Vars.empty()) { |
| 1395 | D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, |
| 1396 | Vars); |
| 1397 | D->setAccess(AS_public); |
| 1398 | } |
| 1399 | return D; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1400 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1401 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1402 | static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1403 | const ValueDecl *D, DSAStackTy::DSAVarData DVar, |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1404 | bool IsLoopIterVar = false) { |
| 1405 | if (DVar.RefExpr) { |
| 1406 | SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) |
| 1407 | << getOpenMPClauseName(DVar.CKind); |
| 1408 | return; |
| 1409 | } |
| 1410 | enum { |
| 1411 | PDSA_StaticMemberShared, |
| 1412 | PDSA_StaticLocalVarShared, |
| 1413 | PDSA_LoopIterVarPrivate, |
| 1414 | PDSA_LoopIterVarLinear, |
| 1415 | PDSA_LoopIterVarLastprivate, |
| 1416 | PDSA_ConstVarShared, |
| 1417 | PDSA_GlobalVarShared, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1418 | PDSA_TaskVarFirstprivate, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1419 | PDSA_LocalVarPrivate, |
| 1420 | PDSA_Implicit |
| 1421 | } Reason = PDSA_Implicit; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1422 | bool ReportHint = false; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1423 | auto ReportLoc = D->getLocation(); |
| 1424 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1425 | if (IsLoopIterVar) { |
| 1426 | if (DVar.CKind == OMPC_private) |
| 1427 | Reason = PDSA_LoopIterVarPrivate; |
| 1428 | else if (DVar.CKind == OMPC_lastprivate) |
| 1429 | Reason = PDSA_LoopIterVarLastprivate; |
| 1430 | else |
| 1431 | Reason = PDSA_LoopIterVarLinear; |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1432 | } else if (isOpenMPTaskingDirective(DVar.DKind) && |
| 1433 | DVar.CKind == OMPC_firstprivate) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1434 | Reason = PDSA_TaskVarFirstprivate; |
| 1435 | ReportLoc = DVar.ImplicitDSALoc; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1436 | } else if (VD && VD->isStaticLocal()) |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1437 | Reason = PDSA_StaticLocalVarShared; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1438 | else if (VD && VD->isStaticDataMember()) |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1439 | Reason = PDSA_StaticMemberShared; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1440 | else if (VD && VD->isFileVarDecl()) |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1441 | Reason = PDSA_GlobalVarShared; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1442 | else if (D->getType().isConstant(SemaRef.getASTContext())) |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1443 | Reason = PDSA_ConstVarShared; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1444 | else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1445 | ReportHint = true; |
| 1446 | Reason = PDSA_LocalVarPrivate; |
| 1447 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1448 | if (Reason != PDSA_Implicit) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1449 | SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1450 | << Reason << ReportHint |
| 1451 | << getOpenMPDirectiveName(Stack->getCurrentDirective()); |
| 1452 | } else if (DVar.ImplicitDSALoc.isValid()) { |
| 1453 | SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) |
| 1454 | << getOpenMPClauseName(DVar.CKind); |
| 1455 | } |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1458 | namespace { |
| 1459 | class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { |
| 1460 | DSAStackTy *Stack; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1461 | Sema &SemaRef; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1462 | bool ErrorFound; |
| 1463 | CapturedStmt *CS; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1464 | llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1465 | llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1466 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1467 | public: |
| 1468 | void VisitDeclRefExpr(DeclRefExpr *E) { |
Alexey Bataev | 07b79c2 | 2016-04-29 09:56:11 +0000 | [diff] [blame] | 1469 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1470 | E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) |
| 1471 | return; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1472 | if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1473 | // Skip internally declared variables. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1474 | if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) |
| 1475 | return; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1476 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1477 | auto DVar = Stack->getTopDSA(VD, false); |
| 1478 | // 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] | 1479 | if (DVar.RefExpr) |
| 1480 | return; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1481 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1482 | auto ELoc = E->getExprLoc(); |
| 1483 | auto DKind = Stack->getCurrentDirective(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1484 | // The default(none) clause requires that each variable that is referenced |
| 1485 | // in the construct, and does not have a predetermined data-sharing |
| 1486 | // attribute, must have its data-sharing attribute explicitly determined |
| 1487 | // by being listed in a data-sharing attribute clause. |
| 1488 | if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1489 | isParallelOrTaskRegion(DKind) && |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1490 | VarsWithInheritedDSA.count(VD) == 0) { |
| 1491 | VarsWithInheritedDSA[VD] = E; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1492 | return; |
| 1493 | } |
| 1494 | |
| 1495 | // OpenMP [2.9.3.6, Restrictions, p.2] |
| 1496 | // A list item that appears in a reduction clause of the innermost |
| 1497 | // enclosing worksharing or parallel construct may not be accessed in an |
| 1498 | // explicit task. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1499 | DVar = Stack->hasInnermostDSA( |
| 1500 | VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, |
| 1501 | [](OpenMPDirectiveKind K) -> bool { |
| 1502 | return isOpenMPParallelDirective(K) || |
| 1503 | isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); |
| 1504 | }, |
| 1505 | false); |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1506 | if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 1507 | ErrorFound = true; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1508 | SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); |
| 1509 | ReportOriginalDSA(SemaRef, Stack, VD, DVar); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 1510 | return; |
| 1511 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1512 | |
| 1513 | // Define implicit data-sharing attributes for task. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1514 | DVar = Stack->getImplicitDSA(VD, false); |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1515 | if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && |
| 1516 | !Stack->isLoopControlVariable(VD).first) |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1517 | ImplicitFirstprivate.push_back(E); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1518 | } |
| 1519 | } |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1520 | void VisitMemberExpr(MemberExpr *E) { |
Alexey Bataev | 07b79c2 | 2016-04-29 09:56:11 +0000 | [diff] [blame] | 1521 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1522 | E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) |
| 1523 | return; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1524 | if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { |
| 1525 | if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 1526 | auto DVar = Stack->getTopDSA(FD, false); |
| 1527 | // Check if the variable has explicit DSA set and stop analysis if it |
| 1528 | // so. |
| 1529 | if (DVar.RefExpr) |
| 1530 | return; |
| 1531 | |
| 1532 | auto ELoc = E->getExprLoc(); |
| 1533 | auto DKind = Stack->getCurrentDirective(); |
| 1534 | // OpenMP [2.9.3.6, Restrictions, p.2] |
| 1535 | // A list item that appears in a reduction clause of the innermost |
| 1536 | // enclosing worksharing or parallel construct may not be accessed in |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 1537 | // an explicit task. |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 1538 | DVar = Stack->hasInnermostDSA( |
| 1539 | FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, |
| 1540 | [](OpenMPDirectiveKind K) -> bool { |
| 1541 | return isOpenMPParallelDirective(K) || |
| 1542 | isOpenMPWorksharingDirective(K) || |
| 1543 | isOpenMPTeamsDirective(K); |
| 1544 | }, |
| 1545 | false); |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1546 | if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1547 | ErrorFound = true; |
| 1548 | SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); |
| 1549 | ReportOriginalDSA(SemaRef, Stack, FD, DVar); |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | // Define implicit data-sharing attributes for task. |
| 1554 | DVar = Stack->getImplicitDSA(FD, false); |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1555 | if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && |
| 1556 | !Stack->isLoopControlVariable(FD).first) |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1557 | ImplicitFirstprivate.push_back(E); |
| 1558 | } |
Alexey Bataev | 7fcacd8 | 2016-11-28 15:55:15 +0000 | [diff] [blame] | 1559 | } else |
| 1560 | Visit(E->getBase()); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1561 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1562 | void VisitOMPExecutableDirective(OMPExecutableDirective *S) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1563 | for (auto *C : S->clauses()) { |
| 1564 | // Skip analysis of arguments of implicitly defined firstprivate clause |
| 1565 | // for task directives. |
| 1566 | if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid())) |
| 1567 | for (auto *CC : C->children()) { |
| 1568 | if (CC) |
| 1569 | Visit(CC); |
| 1570 | } |
| 1571 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1572 | } |
| 1573 | void VisitStmt(Stmt *S) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1574 | for (auto *C : S->children()) { |
| 1575 | if (C && !isa<OMPExecutableDirective>(C)) |
| 1576 | Visit(C); |
| 1577 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1578 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1579 | |
| 1580 | bool isErrorFound() { return ErrorFound; } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1581 | ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 1582 | llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() { |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1583 | return VarsWithInheritedDSA; |
| 1584 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1585 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1586 | DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) |
| 1587 | : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1588 | }; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1589 | } // namespace |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1590 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1591 | void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1592 | switch (DKind) { |
Kelvin Li | 70a12c5 | 2016-07-13 21:51:49 +0000 | [diff] [blame] | 1593 | case OMPD_parallel: |
| 1594 | case OMPD_parallel_for: |
| 1595 | case OMPD_parallel_for_simd: |
| 1596 | case OMPD_parallel_sections: |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 1597 | case OMPD_teams: |
| 1598 | case OMPD_target_teams: { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1599 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1600 | QualType KmpInt32PtrTy = |
| 1601 | Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1602 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1603 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1604 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1605 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1606 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1607 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1608 | Params); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1609 | break; |
| 1610 | } |
Kelvin Li | 70a12c5 | 2016-07-13 21:51:49 +0000 | [diff] [blame] | 1611 | case OMPD_simd: |
| 1612 | case OMPD_for: |
| 1613 | case OMPD_for_simd: |
| 1614 | case OMPD_sections: |
| 1615 | case OMPD_section: |
| 1616 | case OMPD_single: |
| 1617 | case OMPD_master: |
| 1618 | case OMPD_critical: |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 1619 | case OMPD_taskgroup: |
| 1620 | case OMPD_distribute: |
Kelvin Li | 70a12c5 | 2016-07-13 21:51:49 +0000 | [diff] [blame] | 1621 | case OMPD_ordered: |
| 1622 | case OMPD_atomic: |
| 1623 | case OMPD_target_data: |
| 1624 | case OMPD_target: |
| 1625 | case OMPD_target_parallel: |
| 1626 | case OMPD_target_parallel_for: |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 1627 | case OMPD_target_parallel_for_simd: |
| 1628 | case OMPD_target_simd: { |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1629 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1630 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1631 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 1632 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1633 | Params); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1634 | break; |
| 1635 | } |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1636 | case OMPD_task: { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1637 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1638 | QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; |
| 1639 | FunctionProtoType::ExtProtoInfo EPI; |
| 1640 | EPI.Variadic = true; |
| 1641 | QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1642 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1643 | std::make_pair(".global_tid.", KmpInt32Ty), |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1644 | std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), |
| 1645 | std::make_pair(".privates.", Context.VoidPtrTy.withConst()), |
| 1646 | std::make_pair(".copy_fn.", |
| 1647 | Context.getPointerType(CopyFnType).withConst()), |
| 1648 | std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1649 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1650 | }; |
| 1651 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1652 | Params); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1653 | // Mark this captured region as inlined, because we don't use outlined |
| 1654 | // function directly. |
| 1655 | getCurCapturedRegion()->TheCapturedDecl->addAttr( |
| 1656 | AlwaysInlineAttr::CreateImplicit( |
| 1657 | Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1658 | break; |
| 1659 | } |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 1660 | case OMPD_taskloop: |
| 1661 | case OMPD_taskloop_simd: { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1662 | QualType KmpInt32Ty = |
| 1663 | Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1664 | QualType KmpUInt64Ty = |
| 1665 | Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 1666 | QualType KmpInt64Ty = |
| 1667 | Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 1668 | QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; |
| 1669 | FunctionProtoType::ExtProtoInfo EPI; |
| 1670 | EPI.Variadic = true; |
| 1671 | QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 1672 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1673 | std::make_pair(".global_tid.", KmpInt32Ty), |
| 1674 | std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), |
| 1675 | std::make_pair(".privates.", |
| 1676 | Context.VoidPtrTy.withConst().withRestrict()), |
| 1677 | std::make_pair( |
| 1678 | ".copy_fn.", |
| 1679 | Context.getPointerType(CopyFnType).withConst().withRestrict()), |
| 1680 | std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), |
| 1681 | std::make_pair(".lb.", KmpUInt64Ty), |
| 1682 | std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty), |
| 1683 | std::make_pair(".liter.", KmpInt32Ty), |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 1684 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1685 | }; |
| 1686 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1687 | Params); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1688 | // Mark this captured region as inlined, because we don't use outlined |
| 1689 | // function directly. |
| 1690 | getCurCapturedRegion()->TheCapturedDecl->addAttr( |
| 1691 | AlwaysInlineAttr::CreateImplicit( |
| 1692 | Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | } |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 1695 | case OMPD_distribute_parallel_for_simd: |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 1696 | case OMPD_distribute_simd: |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 1697 | case OMPD_distribute_parallel_for: |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 1698 | case OMPD_teams_distribute: |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 1699 | case OMPD_teams_distribute_simd: |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 1700 | case OMPD_teams_distribute_parallel_for_simd: |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 1701 | case OMPD_teams_distribute_parallel_for: |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 1702 | case OMPD_target_teams_distribute: |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 1703 | case OMPD_target_teams_distribute_parallel_for: |
| 1704 | case OMPD_target_teams_distribute_parallel_for_simd: { |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 1705 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 1706 | QualType KmpInt32PtrTy = |
| 1707 | Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); |
| 1708 | Sema::CapturedParamNameType Params[] = { |
| 1709 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1710 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1711 | std::make_pair(".previous.lb.", Context.getSizeType()), |
| 1712 | std::make_pair(".previous.ub.", Context.getSizeType()), |
| 1713 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1714 | }; |
| 1715 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1716 | Params); |
| 1717 | break; |
| 1718 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1719 | case OMPD_threadprivate: |
Alexey Bataev | ee9af45 | 2014-11-21 11:33:46 +0000 | [diff] [blame] | 1720 | case OMPD_taskyield: |
| 1721 | case OMPD_barrier: |
| 1722 | case OMPD_taskwait: |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1723 | case OMPD_cancellation_point: |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 1724 | case OMPD_cancel: |
Alexey Bataev | ee9af45 | 2014-11-21 11:33:46 +0000 | [diff] [blame] | 1725 | case OMPD_flush: |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 1726 | case OMPD_target_enter_data: |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 1727 | case OMPD_target_exit_data: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1728 | case OMPD_declare_reduction: |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 1729 | case OMPD_declare_simd: |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 1730 | case OMPD_declare_target: |
| 1731 | case OMPD_end_declare_target: |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 1732 | case OMPD_target_update: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1733 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 1734 | case OMPD_unknown: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1735 | llvm_unreachable("Unknown OpenMP directive"); |
| 1736 | } |
| 1737 | } |
| 1738 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1739 | static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 1740 | Expr *CaptureExpr, bool WithInit, |
| 1741 | bool AsExpression) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 1742 | assert(CaptureExpr); |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1743 | ASTContext &C = S.getASTContext(); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 1744 | Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1745 | QualType Ty = Init->getType(); |
| 1746 | if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { |
| 1747 | if (S.getLangOpts().CPlusPlus) |
| 1748 | Ty = C.getLValueReferenceType(Ty); |
| 1749 | else { |
| 1750 | Ty = C.getPointerType(Ty); |
| 1751 | ExprResult Res = |
| 1752 | S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); |
| 1753 | if (!Res.isUsable()) |
| 1754 | return nullptr; |
| 1755 | Init = Res.get(); |
| 1756 | } |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1757 | WithInit = true; |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1758 | } |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 1759 | auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, |
| 1760 | CaptureExpr->getLocStart()); |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 1761 | if (!WithInit) |
| 1762 | CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange())); |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1763 | S.CurContext->addHiddenDecl(CED); |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 1764 | S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false, |
| 1765 | /*TypeMayContainAuto=*/true); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1766 | return CED; |
| 1767 | } |
| 1768 | |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1769 | static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, |
| 1770 | bool WithInit) { |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 1771 | OMPCapturedExprDecl *CD; |
| 1772 | if (auto *VD = S.IsOpenMPCapturedDecl(D)) |
| 1773 | CD = cast<OMPCapturedExprDecl>(VD); |
| 1774 | else |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 1775 | CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, |
| 1776 | /*AsExpression=*/false); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1777 | return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 1778 | CaptureExpr->getExprLoc()); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 1781 | static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { |
| 1782 | if (!Ref) { |
| 1783 | auto *CD = |
| 1784 | buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."), |
| 1785 | CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true); |
| 1786 | Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), |
| 1787 | CaptureExpr->getExprLoc()); |
| 1788 | } |
| 1789 | ExprResult Res = Ref; |
| 1790 | if (!S.getLangOpts().CPlusPlus && |
| 1791 | CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && |
| 1792 | Ref->getType()->isPointerType()) |
| 1793 | Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); |
| 1794 | if (!Res.isUsable()) |
| 1795 | return ExprError(); |
| 1796 | return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get()); |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1799 | StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, |
| 1800 | ArrayRef<OMPClause *> Clauses) { |
| 1801 | if (!S.isUsable()) { |
| 1802 | ActOnCapturedRegionError(); |
| 1803 | return StmtError(); |
| 1804 | } |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 1805 | |
| 1806 | OMPOrderedClause *OC = nullptr; |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 1807 | OMPScheduleClause *SC = nullptr; |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 1808 | SmallVector<OMPLinearClause *, 4> LCs; |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1809 | // This is required for proper codegen. |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1810 | for (auto *Clause : Clauses) { |
Alexey Bataev | 16dc7b6 | 2015-05-20 03:46:04 +0000 | [diff] [blame] | 1811 | if (isOpenMPPrivate(Clause->getClauseKind()) || |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 1812 | Clause->getClauseKind() == OMPC_copyprivate || |
| 1813 | (getLangOpts().OpenMPUseTLS && |
| 1814 | getASTContext().getTargetInfo().isTLSSupported() && |
| 1815 | Clause->getClauseKind() == OMPC_copyin)) { |
| 1816 | DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1817 | // Mark all variables in private list clauses as used in inner region. |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1818 | for (auto *VarRef : Clause->children()) { |
| 1819 | if (auto *E = cast_or_null<Expr>(VarRef)) { |
Alexey Bataev | 8bf6b3e | 2015-04-02 13:07:08 +0000 | [diff] [blame] | 1820 | MarkDeclarationsReferencedInExpr(E); |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1821 | } |
| 1822 | } |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 1823 | DSAStack->setForceVarCapturing(/*V=*/false); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1824 | } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1825 | // Mark all variables in private list clauses as used in inner region. |
| 1826 | // Required for proper codegen of combined directives. |
| 1827 | // TODO: add processing for other clauses. |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1828 | if (auto *C = OMPClauseWithPreInit::get(Clause)) { |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1829 | if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { |
| 1830 | for (auto *D : DS->decls()) |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 1831 | MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); |
| 1832 | } |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1833 | } |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1834 | if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { |
| 1835 | if (auto *E = C->getPostUpdateExpr()) |
| 1836 | MarkDeclarationsReferencedInExpr(E); |
| 1837 | } |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1838 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 1839 | if (Clause->getClauseKind() == OMPC_schedule) |
| 1840 | SC = cast<OMPScheduleClause>(Clause); |
| 1841 | else if (Clause->getClauseKind() == OMPC_ordered) |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 1842 | OC = cast<OMPOrderedClause>(Clause); |
| 1843 | else if (Clause->getClauseKind() == OMPC_linear) |
| 1844 | LCs.push_back(cast<OMPLinearClause>(Clause)); |
| 1845 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 1846 | bool ErrorFound = false; |
| 1847 | // OpenMP, 2.7.1 Loop Construct, Restrictions |
| 1848 | // The nonmonotonic modifier cannot be specified if an ordered clause is |
| 1849 | // specified. |
| 1850 | if (SC && |
| 1851 | (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || |
| 1852 | SC->getSecondScheduleModifier() == |
| 1853 | OMPC_SCHEDULE_MODIFIER_nonmonotonic) && |
| 1854 | OC) { |
| 1855 | Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic |
| 1856 | ? SC->getFirstScheduleModifierLoc() |
| 1857 | : SC->getSecondScheduleModifierLoc(), |
| 1858 | diag::err_omp_schedule_nonmonotonic_ordered) |
| 1859 | << SourceRange(OC->getLocStart(), OC->getLocEnd()); |
| 1860 | ErrorFound = true; |
| 1861 | } |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 1862 | if (!LCs.empty() && OC && OC->getNumForLoops()) { |
| 1863 | for (auto *C : LCs) { |
| 1864 | Diag(C->getLocStart(), diag::err_omp_linear_ordered) |
| 1865 | << SourceRange(OC->getLocStart(), OC->getLocEnd()); |
| 1866 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 1867 | ErrorFound = true; |
| 1868 | } |
Alexey Bataev | 113438c | 2015-12-30 12:06:23 +0000 | [diff] [blame] | 1869 | if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && |
| 1870 | isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && |
| 1871 | OC->getNumForLoops()) { |
| 1872 | Diag(OC->getLocStart(), diag::err_omp_ordered_simd) |
| 1873 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 1874 | ErrorFound = true; |
| 1875 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 1876 | if (ErrorFound) { |
Alexey Bataev | 993d280 | 2015-12-28 06:23:08 +0000 | [diff] [blame] | 1877 | ActOnCapturedRegionError(); |
| 1878 | return StmtError(); |
Alexey Bataev | a8d4a543 | 2015-04-02 07:48:16 +0000 | [diff] [blame] | 1879 | } |
| 1880 | return ActOnCapturedRegionEnd(S.get()); |
| 1881 | } |
| 1882 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1883 | static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, |
| 1884 | OpenMPDirectiveKind CurrentRegion, |
| 1885 | const DeclarationNameInfo &CurrentName, |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1886 | OpenMPDirectiveKind CancelRegion, |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1887 | SourceLocation StartLoc) { |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1888 | if (Stack->getCurScope()) { |
| 1889 | auto ParentRegion = Stack->getParentDirective(); |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 1890 | auto OffendingRegion = ParentRegion; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1891 | bool NestingProhibited = false; |
| 1892 | bool CloseNesting = true; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1893 | bool OrphanSeen = false; |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1894 | enum { |
| 1895 | NoRecommend, |
| 1896 | ShouldBeInParallelRegion, |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1897 | ShouldBeInOrderedRegion, |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 1898 | ShouldBeInTargetRegion, |
| 1899 | ShouldBeInTeamsRegion |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1900 | } Recommend = NoRecommend; |
Kelvin Li | fd8b574 | 2016-07-01 14:30:25 +0000 | [diff] [blame] | 1901 | if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1902 | // OpenMP [2.16, Nesting of Regions] |
| 1903 | // OpenMP constructs may not be nested inside a simd region. |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 1904 | // OpenMP [2.8.1,simd Construct, Restrictions] |
Kelvin Li | fd8b574 | 2016-07-01 14:30:25 +0000 | [diff] [blame] | 1905 | // An ordered construct with the simd clause is the only OpenMP |
| 1906 | // construct that can appear in the simd region. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1907 | // Allowing a SIMD construct nested in another SIMD construct is an |
Kelvin Li | fd8b574 | 2016-07-01 14:30:25 +0000 | [diff] [blame] | 1908 | // extension. The OpenMP 4.5 spec does not allow it. Issue a warning |
| 1909 | // message. |
| 1910 | SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) |
| 1911 | ? diag::err_omp_prohibited_region_simd |
| 1912 | : diag::warn_omp_nesting_simd); |
| 1913 | return CurrentRegion != OMPD_simd; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1914 | } |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1915 | if (ParentRegion == OMPD_atomic) { |
| 1916 | // OpenMP [2.16, Nesting of Regions] |
| 1917 | // OpenMP constructs may not be nested inside an atomic region. |
| 1918 | SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); |
| 1919 | return true; |
| 1920 | } |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1921 | if (CurrentRegion == OMPD_section) { |
| 1922 | // OpenMP [2.7.2, sections Construct, Restrictions] |
| 1923 | // Orphaned section directives are prohibited. That is, the section |
| 1924 | // directives must appear within the sections construct and must not be |
| 1925 | // encountered elsewhere in the sections region. |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1926 | if (ParentRegion != OMPD_sections && |
| 1927 | ParentRegion != OMPD_parallel_sections) { |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1928 | SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) |
| 1929 | << (ParentRegion != OMPD_unknown) |
| 1930 | << getOpenMPDirectiveName(ParentRegion); |
| 1931 | return true; |
| 1932 | } |
| 1933 | return false; |
| 1934 | } |
Kelvin Li | 2b51f72 | 2016-07-26 04:32:50 +0000 | [diff] [blame] | 1935 | // Allow some constructs (except teams) to be orphaned (they could be |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1936 | // used in functions, called from OpenMP regions with the required |
Kelvin Li | 2b51f72 | 2016-07-26 04:32:50 +0000 | [diff] [blame] | 1937 | // preconditions). |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 1938 | if (ParentRegion == OMPD_unknown && |
| 1939 | !isOpenMPNestingTeamsDirective(CurrentRegion)) |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1940 | return false; |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 1941 | if (CurrentRegion == OMPD_cancellation_point || |
| 1942 | CurrentRegion == OMPD_cancel) { |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1943 | // OpenMP [2.16, Nesting of Regions] |
| 1944 | // A cancellation point construct for which construct-type-clause is |
| 1945 | // taskgroup must be nested inside a task construct. A cancellation |
| 1946 | // point construct for which construct-type-clause is not taskgroup must |
| 1947 | // be closely nested inside an OpenMP construct that matches the type |
| 1948 | // specified in construct-type-clause. |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 1949 | // A cancel construct for which construct-type-clause is taskgroup must be |
| 1950 | // nested inside a task construct. A cancel construct for which |
| 1951 | // construct-type-clause is not taskgroup must be closely nested inside an |
| 1952 | // OpenMP construct that matches the type specified in |
| 1953 | // construct-type-clause. |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1954 | NestingProhibited = |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 1955 | !((CancelRegion == OMPD_parallel && |
| 1956 | (ParentRegion == OMPD_parallel || |
| 1957 | ParentRegion == OMPD_target_parallel)) || |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1958 | (CancelRegion == OMPD_for && |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 1959 | (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || |
| 1960 | ParentRegion == OMPD_target_parallel_for)) || |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1961 | (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || |
| 1962 | (CancelRegion == OMPD_sections && |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1963 | (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || |
| 1964 | ParentRegion == OMPD_parallel_sections))); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1965 | } else if (CurrentRegion == OMPD_master) { |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1966 | // OpenMP [2.16, Nesting of Regions] |
| 1967 | // A master region may not be closely nested inside a worksharing, |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1968 | // atomic, or explicit task region. |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1969 | NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 1970 | isOpenMPTaskingDirective(ParentRegion); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1971 | } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { |
| 1972 | // OpenMP [2.16, Nesting of Regions] |
| 1973 | // A critical region may not be nested (closely or otherwise) inside a |
| 1974 | // critical region with the same name. Note that this restriction is not |
| 1975 | // sufficient to prevent deadlock. |
| 1976 | SourceLocation PreviousCriticalLoc; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 1977 | bool DeadLock = Stack->hasDirective( |
| 1978 | [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, |
| 1979 | const DeclarationNameInfo &DNI, |
| 1980 | SourceLocation Loc) -> bool { |
| 1981 | if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { |
| 1982 | PreviousCriticalLoc = Loc; |
| 1983 | return true; |
| 1984 | } else |
| 1985 | return false; |
| 1986 | }, |
| 1987 | false /* skip top directive */); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1988 | if (DeadLock) { |
| 1989 | SemaRef.Diag(StartLoc, |
| 1990 | diag::err_omp_prohibited_region_critical_same_name) |
| 1991 | << CurrentName.getName(); |
| 1992 | if (PreviousCriticalLoc.isValid()) |
| 1993 | SemaRef.Diag(PreviousCriticalLoc, |
| 1994 | diag::note_omp_previous_critical_region); |
| 1995 | return true; |
| 1996 | } |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1997 | } else if (CurrentRegion == OMPD_barrier) { |
| 1998 | // OpenMP [2.16, Nesting of Regions] |
| 1999 | // A barrier region may not be closely nested inside a worksharing, |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2000 | // explicit task, critical, ordered, atomic, or master region. |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 2001 | NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || |
| 2002 | isOpenMPTaskingDirective(ParentRegion) || |
| 2003 | ParentRegion == OMPD_master || |
| 2004 | ParentRegion == OMPD_critical || |
| 2005 | ParentRegion == OMPD_ordered; |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2006 | } else if (isOpenMPWorksharingDirective(CurrentRegion) && |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2007 | !isOpenMPParallelDirective(CurrentRegion) && |
| 2008 | !isOpenMPTeamsDirective(CurrentRegion)) { |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2009 | // OpenMP [2.16, Nesting of Regions] |
| 2010 | // A worksharing region may not be closely nested inside a worksharing, |
| 2011 | // explicit task, critical, ordered, atomic, or master region. |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 2012 | NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || |
| 2013 | isOpenMPTaskingDirective(ParentRegion) || |
| 2014 | ParentRegion == OMPD_master || |
| 2015 | ParentRegion == OMPD_critical || |
| 2016 | ParentRegion == OMPD_ordered; |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2017 | Recommend = ShouldBeInParallelRegion; |
| 2018 | } else if (CurrentRegion == OMPD_ordered) { |
| 2019 | // OpenMP [2.16, Nesting of Regions] |
| 2020 | // An ordered region may not be closely nested inside a critical, |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2021 | // atomic, or explicit task region. |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2022 | // An ordered region must be closely nested inside a loop region (or |
| 2023 | // parallel loop region) with an ordered clause. |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2024 | // OpenMP [2.8.1,simd Construct, Restrictions] |
| 2025 | // An ordered construct with the simd clause is the only OpenMP construct |
| 2026 | // that can appear in the simd region. |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2027 | NestingProhibited = ParentRegion == OMPD_critical || |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 2028 | isOpenMPTaskingDirective(ParentRegion) || |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2029 | !(isOpenMPSimdDirective(ParentRegion) || |
| 2030 | Stack->isParentOrderedRegion()); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2031 | Recommend = ShouldBeInOrderedRegion; |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2032 | } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2033 | // OpenMP [2.16, Nesting of Regions] |
| 2034 | // If specified, a teams construct must be contained within a target |
| 2035 | // construct. |
| 2036 | NestingProhibited = ParentRegion != OMPD_target; |
Kelvin Li | 2b51f72 | 2016-07-26 04:32:50 +0000 | [diff] [blame] | 2037 | OrphanSeen = ParentRegion == OMPD_unknown; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2038 | Recommend = ShouldBeInTargetRegion; |
| 2039 | Stack->setParentTeamsRegionLoc(Stack->getConstructLoc()); |
| 2040 | } |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2041 | if (!NestingProhibited && |
| 2042 | !isOpenMPTargetExecutionDirective(CurrentRegion) && |
| 2043 | !isOpenMPTargetDataManagementDirective(CurrentRegion) && |
| 2044 | (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2045 | // OpenMP [2.16, Nesting of Regions] |
| 2046 | // distribute, parallel, parallel sections, parallel workshare, and the |
| 2047 | // parallel loop and parallel loop SIMD constructs are the only OpenMP |
| 2048 | // constructs that can be closely nested in the teams region. |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2049 | NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && |
| 2050 | !isOpenMPDistributeDirective(CurrentRegion); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2051 | Recommend = ShouldBeInParallelRegion; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2052 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2053 | if (!NestingProhibited && |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2054 | isOpenMPNestingDistributeDirective(CurrentRegion)) { |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2055 | // OpenMP 4.5 [2.17 Nesting of Regions] |
| 2056 | // The region associated with the distribute construct must be strictly |
| 2057 | // nested inside a teams region |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2058 | NestingProhibited = |
| 2059 | (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2060 | Recommend = ShouldBeInTeamsRegion; |
| 2061 | } |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 2062 | if (!NestingProhibited && |
| 2063 | (isOpenMPTargetExecutionDirective(CurrentRegion) || |
| 2064 | isOpenMPTargetDataManagementDirective(CurrentRegion))) { |
| 2065 | // OpenMP 4.5 [2.17 Nesting of Regions] |
| 2066 | // If a target, target update, target data, target enter data, or |
| 2067 | // target exit data construct is encountered during execution of a |
| 2068 | // target region, the behavior is unspecified. |
| 2069 | NestingProhibited = Stack->hasDirective( |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 2070 | [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, |
| 2071 | SourceLocation) -> bool { |
Arpith Chacko Jacob | 3d58f26 | 2016-02-02 04:00:47 +0000 | [diff] [blame] | 2072 | if (isOpenMPTargetExecutionDirective(K)) { |
| 2073 | OffendingRegion = K; |
| 2074 | return true; |
| 2075 | } else |
| 2076 | return false; |
| 2077 | }, |
| 2078 | false /* don't skip top directive */); |
| 2079 | CloseNesting = false; |
| 2080 | } |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2081 | if (NestingProhibited) { |
Kelvin Li | 2b51f72 | 2016-07-26 04:32:50 +0000 | [diff] [blame] | 2082 | if (OrphanSeen) { |
| 2083 | SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) |
| 2084 | << getOpenMPDirectiveName(CurrentRegion) << Recommend; |
| 2085 | } else { |
| 2086 | SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) |
| 2087 | << CloseNesting << getOpenMPDirectiveName(OffendingRegion) |
| 2088 | << Recommend << getOpenMPDirectiveName(CurrentRegion); |
| 2089 | } |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2090 | return true; |
| 2091 | } |
| 2092 | } |
| 2093 | return false; |
| 2094 | } |
| 2095 | |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2096 | static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, |
| 2097 | ArrayRef<OMPClause *> Clauses, |
| 2098 | ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { |
| 2099 | bool ErrorFound = false; |
| 2100 | unsigned NamedModifiersNumber = 0; |
| 2101 | SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( |
| 2102 | OMPD_unknown + 1); |
Alexey Bataev | ecb156a | 2015-09-15 17:23:56 +0000 | [diff] [blame] | 2103 | SmallVector<SourceLocation, 4> NameModifierLoc; |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2104 | for (const auto *C : Clauses) { |
| 2105 | if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { |
| 2106 | // At most one if clause without a directive-name-modifier can appear on |
| 2107 | // the directive. |
| 2108 | OpenMPDirectiveKind CurNM = IC->getNameModifier(); |
| 2109 | if (FoundNameModifiers[CurNM]) { |
| 2110 | S.Diag(C->getLocStart(), diag::err_omp_more_one_clause) |
| 2111 | << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) |
| 2112 | << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); |
| 2113 | ErrorFound = true; |
Alexey Bataev | ecb156a | 2015-09-15 17:23:56 +0000 | [diff] [blame] | 2114 | } else if (CurNM != OMPD_unknown) { |
| 2115 | NameModifierLoc.push_back(IC->getNameModifierLoc()); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2116 | ++NamedModifiersNumber; |
Alexey Bataev | ecb156a | 2015-09-15 17:23:56 +0000 | [diff] [blame] | 2117 | } |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2118 | FoundNameModifiers[CurNM] = IC; |
| 2119 | if (CurNM == OMPD_unknown) |
| 2120 | continue; |
| 2121 | // Check if the specified name modifier is allowed for the current |
| 2122 | // directive. |
| 2123 | // At most one if clause with the particular directive-name-modifier can |
| 2124 | // appear on the directive. |
| 2125 | bool MatchFound = false; |
| 2126 | for (auto NM : AllowedNameModifiers) { |
| 2127 | if (CurNM == NM) { |
| 2128 | MatchFound = true; |
| 2129 | break; |
| 2130 | } |
| 2131 | } |
| 2132 | if (!MatchFound) { |
| 2133 | S.Diag(IC->getNameModifierLoc(), |
| 2134 | diag::err_omp_wrong_if_directive_name_modifier) |
| 2135 | << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); |
| 2136 | ErrorFound = true; |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | // If any if clause on the directive includes a directive-name-modifier then |
| 2141 | // all if clauses on the directive must include a directive-name-modifier. |
| 2142 | if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { |
| 2143 | if (NamedModifiersNumber == AllowedNameModifiers.size()) { |
| 2144 | S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(), |
| 2145 | diag::err_omp_no_more_if_clause); |
| 2146 | } else { |
| 2147 | std::string Values; |
| 2148 | std::string Sep(", "); |
| 2149 | unsigned AllowedCnt = 0; |
| 2150 | unsigned TotalAllowedNum = |
| 2151 | AllowedNameModifiers.size() - NamedModifiersNumber; |
| 2152 | for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; |
| 2153 | ++Cnt) { |
| 2154 | OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; |
| 2155 | if (!FoundNameModifiers[NM]) { |
| 2156 | Values += "'"; |
| 2157 | Values += getOpenMPDirectiveName(NM); |
| 2158 | Values += "'"; |
| 2159 | if (AllowedCnt + 2 == TotalAllowedNum) |
| 2160 | Values += " or "; |
| 2161 | else if (AllowedCnt + 1 != TotalAllowedNum) |
| 2162 | Values += Sep; |
| 2163 | ++AllowedCnt; |
| 2164 | } |
| 2165 | } |
| 2166 | S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(), |
| 2167 | diag::err_omp_unnamed_if_clause) |
| 2168 | << (TotalAllowedNum > 1) << Values; |
| 2169 | } |
Alexey Bataev | ecb156a | 2015-09-15 17:23:56 +0000 | [diff] [blame] | 2170 | for (auto Loc : NameModifierLoc) { |
| 2171 | S.Diag(Loc, diag::note_omp_previous_named_if_clause); |
| 2172 | } |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2173 | ErrorFound = true; |
| 2174 | } |
| 2175 | return ErrorFound; |
| 2176 | } |
| 2177 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2178 | StmtResult Sema::ActOnOpenMPExecutableDirective( |
| 2179 | OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, |
| 2180 | OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, |
| 2181 | Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2182 | StmtResult Res = StmtError(); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2183 | if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, |
| 2184 | StartLoc)) |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2185 | return StmtError(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2186 | |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2187 | llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 2188 | llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2189 | bool ErrorFound = false; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2190 | ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2191 | if (AStmt) { |
| 2192 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 2193 | |
| 2194 | // Check default data sharing attributes for referenced variables. |
| 2195 | DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); |
| 2196 | DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); |
| 2197 | if (DSAChecker.isErrorFound()) |
| 2198 | return StmtError(); |
| 2199 | // Generate list of implicitly defined firstprivate variables. |
| 2200 | VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2201 | |
| 2202 | if (!DSAChecker.getImplicitFirstprivate().empty()) { |
| 2203 | if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( |
| 2204 | DSAChecker.getImplicitFirstprivate(), SourceLocation(), |
| 2205 | SourceLocation(), SourceLocation())) { |
| 2206 | ClausesWithImplicit.push_back(Implicit); |
| 2207 | ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != |
| 2208 | DSAChecker.getImplicitFirstprivate().size(); |
| 2209 | } else |
| 2210 | ErrorFound = true; |
| 2211 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2212 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2213 | |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2214 | llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2215 | switch (Kind) { |
| 2216 | case OMPD_parallel: |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2217 | Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2218 | EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2219 | AllowedNameModifiers.push_back(OMPD_parallel); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2220 | break; |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2221 | case OMPD_simd: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2222 | Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, |
| 2223 | VarsWithInheritedDSA); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2224 | break; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2225 | case OMPD_for: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2226 | Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, |
| 2227 | VarsWithInheritedDSA); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2228 | break; |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2229 | case OMPD_for_simd: |
| 2230 | Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2231 | EndLoc, VarsWithInheritedDSA); |
| 2232 | break; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2233 | case OMPD_sections: |
| 2234 | Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2235 | EndLoc); |
| 2236 | break; |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2237 | case OMPD_section: |
| 2238 | assert(ClausesWithImplicit.empty() && |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2239 | "No clauses are allowed for 'omp section' directive"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2240 | Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); |
| 2241 | break; |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2242 | case OMPD_single: |
| 2243 | Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2244 | EndLoc); |
| 2245 | break; |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2246 | case OMPD_master: |
| 2247 | assert(ClausesWithImplicit.empty() && |
| 2248 | "No clauses are allowed for 'omp master' directive"); |
| 2249 | Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); |
| 2250 | break; |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2251 | case OMPD_critical: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2252 | Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, |
| 2253 | StartLoc, EndLoc); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2254 | break; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2255 | case OMPD_parallel_for: |
| 2256 | Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2257 | EndLoc, VarsWithInheritedDSA); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2258 | AllowedNameModifiers.push_back(OMPD_parallel); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2259 | break; |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2260 | case OMPD_parallel_for_simd: |
| 2261 | Res = ActOnOpenMPParallelForSimdDirective( |
| 2262 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2263 | AllowedNameModifiers.push_back(OMPD_parallel); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2264 | break; |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2265 | case OMPD_parallel_sections: |
| 2266 | Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, |
| 2267 | StartLoc, EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2268 | AllowedNameModifiers.push_back(OMPD_parallel); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2269 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2270 | case OMPD_task: |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2271 | Res = |
| 2272 | ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2273 | AllowedNameModifiers.push_back(OMPD_task); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2274 | break; |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2275 | case OMPD_taskyield: |
| 2276 | assert(ClausesWithImplicit.empty() && |
| 2277 | "No clauses are allowed for 'omp taskyield' directive"); |
| 2278 | assert(AStmt == nullptr && |
| 2279 | "No associated statement allowed for 'omp taskyield' directive"); |
| 2280 | Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); |
| 2281 | break; |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2282 | case OMPD_barrier: |
| 2283 | assert(ClausesWithImplicit.empty() && |
| 2284 | "No clauses are allowed for 'omp barrier' directive"); |
| 2285 | assert(AStmt == nullptr && |
| 2286 | "No associated statement allowed for 'omp barrier' directive"); |
| 2287 | Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); |
| 2288 | break; |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2289 | case OMPD_taskwait: |
| 2290 | assert(ClausesWithImplicit.empty() && |
| 2291 | "No clauses are allowed for 'omp taskwait' directive"); |
| 2292 | assert(AStmt == nullptr && |
| 2293 | "No associated statement allowed for 'omp taskwait' directive"); |
| 2294 | Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); |
| 2295 | break; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2296 | case OMPD_taskgroup: |
| 2297 | assert(ClausesWithImplicit.empty() && |
| 2298 | "No clauses are allowed for 'omp taskgroup' directive"); |
| 2299 | Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc); |
| 2300 | break; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2301 | case OMPD_flush: |
| 2302 | assert(AStmt == nullptr && |
| 2303 | "No associated statement allowed for 'omp flush' directive"); |
| 2304 | Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); |
| 2305 | break; |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2306 | case OMPD_ordered: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2307 | Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2308 | EndLoc); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2309 | break; |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2310 | case OMPD_atomic: |
| 2311 | Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2312 | EndLoc); |
| 2313 | break; |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2314 | case OMPD_teams: |
| 2315 | Res = |
| 2316 | ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); |
| 2317 | break; |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2318 | case OMPD_target: |
| 2319 | Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2320 | EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2321 | AllowedNameModifiers.push_back(OMPD_target); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2322 | break; |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2323 | case OMPD_target_parallel: |
| 2324 | Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, |
| 2325 | StartLoc, EndLoc); |
| 2326 | AllowedNameModifiers.push_back(OMPD_target); |
| 2327 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2328 | break; |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2329 | case OMPD_target_parallel_for: |
| 2330 | Res = ActOnOpenMPTargetParallelForDirective( |
| 2331 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2332 | AllowedNameModifiers.push_back(OMPD_target); |
| 2333 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2334 | break; |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2335 | case OMPD_cancellation_point: |
| 2336 | assert(ClausesWithImplicit.empty() && |
| 2337 | "No clauses are allowed for 'omp cancellation point' directive"); |
| 2338 | assert(AStmt == nullptr && "No associated statement allowed for 'omp " |
| 2339 | "cancellation point' directive"); |
| 2340 | Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); |
| 2341 | break; |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2342 | case OMPD_cancel: |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2343 | assert(AStmt == nullptr && |
| 2344 | "No associated statement allowed for 'omp cancel' directive"); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 2345 | Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, |
| 2346 | CancelRegion); |
| 2347 | AllowedNameModifiers.push_back(OMPD_cancel); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2348 | break; |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2349 | case OMPD_target_data: |
| 2350 | Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2351 | EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2352 | AllowedNameModifiers.push_back(OMPD_target_data); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2353 | break; |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2354 | case OMPD_target_enter_data: |
| 2355 | Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, |
| 2356 | EndLoc); |
| 2357 | AllowedNameModifiers.push_back(OMPD_target_enter_data); |
| 2358 | break; |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2359 | case OMPD_target_exit_data: |
| 2360 | Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, |
| 2361 | EndLoc); |
| 2362 | AllowedNameModifiers.push_back(OMPD_target_exit_data); |
| 2363 | break; |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2364 | case OMPD_taskloop: |
| 2365 | Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2366 | EndLoc, VarsWithInheritedDSA); |
| 2367 | AllowedNameModifiers.push_back(OMPD_taskloop); |
| 2368 | break; |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2369 | case OMPD_taskloop_simd: |
| 2370 | Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2371 | EndLoc, VarsWithInheritedDSA); |
| 2372 | AllowedNameModifiers.push_back(OMPD_taskloop); |
| 2373 | break; |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2374 | case OMPD_distribute: |
| 2375 | Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2376 | EndLoc, VarsWithInheritedDSA); |
| 2377 | break; |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 2378 | case OMPD_target_update: |
| 2379 | assert(!AStmt && "Statement is not allowed for target update"); |
| 2380 | Res = |
| 2381 | ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc); |
| 2382 | AllowedNameModifiers.push_back(OMPD_target_update); |
| 2383 | break; |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2384 | case OMPD_distribute_parallel_for: |
| 2385 | Res = ActOnOpenMPDistributeParallelForDirective( |
| 2386 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2387 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2388 | break; |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2389 | case OMPD_distribute_parallel_for_simd: |
| 2390 | Res = ActOnOpenMPDistributeParallelForSimdDirective( |
| 2391 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2392 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2393 | break; |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2394 | case OMPD_distribute_simd: |
| 2395 | Res = ActOnOpenMPDistributeSimdDirective( |
| 2396 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2397 | break; |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2398 | case OMPD_target_parallel_for_simd: |
| 2399 | Res = ActOnOpenMPTargetParallelForSimdDirective( |
| 2400 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2401 | AllowedNameModifiers.push_back(OMPD_target); |
| 2402 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2403 | break; |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2404 | case OMPD_target_simd: |
| 2405 | Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2406 | EndLoc, VarsWithInheritedDSA); |
| 2407 | AllowedNameModifiers.push_back(OMPD_target); |
| 2408 | break; |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2409 | case OMPD_teams_distribute: |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2410 | Res = ActOnOpenMPTeamsDistributeDirective( |
| 2411 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2412 | break; |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2413 | case OMPD_teams_distribute_simd: |
| 2414 | Res = ActOnOpenMPTeamsDistributeSimdDirective( |
| 2415 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2416 | break; |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2417 | case OMPD_teams_distribute_parallel_for_simd: |
| 2418 | Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( |
| 2419 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2420 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2421 | break; |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2422 | case OMPD_teams_distribute_parallel_for: |
| 2423 | Res = ActOnOpenMPTeamsDistributeParallelForDirective( |
| 2424 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2425 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2426 | break; |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2427 | case OMPD_target_teams: |
| 2428 | Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 2429 | EndLoc); |
| 2430 | AllowedNameModifiers.push_back(OMPD_target); |
| 2431 | break; |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2432 | case OMPD_target_teams_distribute: |
| 2433 | Res = ActOnOpenMPTargetTeamsDistributeDirective( |
| 2434 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2435 | AllowedNameModifiers.push_back(OMPD_target); |
| 2436 | break; |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2437 | case OMPD_target_teams_distribute_parallel_for: |
| 2438 | Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( |
| 2439 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2440 | AllowedNameModifiers.push_back(OMPD_target); |
| 2441 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2442 | break; |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2443 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 2444 | Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( |
| 2445 | ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); |
| 2446 | AllowedNameModifiers.push_back(OMPD_target); |
| 2447 | AllowedNameModifiers.push_back(OMPD_parallel); |
| 2448 | break; |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 2449 | case OMPD_declare_target: |
| 2450 | case OMPD_end_declare_target: |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2451 | case OMPD_threadprivate: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2452 | case OMPD_declare_reduction: |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2453 | case OMPD_declare_simd: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2454 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 2455 | case OMPD_unknown: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2456 | llvm_unreachable("Unknown OpenMP directive"); |
| 2457 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2458 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2459 | for (auto P : VarsWithInheritedDSA) { |
| 2460 | Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) |
| 2461 | << P.first << P.second->getSourceRange(); |
| 2462 | } |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2463 | ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; |
| 2464 | |
| 2465 | if (!AllowedNameModifiers.empty()) |
| 2466 | ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || |
| 2467 | ErrorFound; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2468 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2469 | if (ErrorFound) |
| 2470 | return StmtError(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2471 | return Res; |
| 2472 | } |
| 2473 | |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2474 | Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( |
| 2475 | DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 2476 | ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2477 | ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, |
| 2478 | ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 2479 | assert(Aligneds.size() == Alignments.size()); |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2480 | assert(Linears.size() == LinModifiers.size()); |
| 2481 | assert(Linears.size() == Steps.size()); |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2482 | if (!DG || DG.get().isNull()) |
| 2483 | return DeclGroupPtrTy(); |
| 2484 | |
| 2485 | if (!DG.get().isSingleDecl()) { |
Alexey Bataev | 20dfd77 | 2016-04-04 10:12:15 +0000 | [diff] [blame] | 2486 | Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2487 | return DG; |
| 2488 | } |
| 2489 | auto *ADecl = DG.get().getSingleDecl(); |
| 2490 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) |
| 2491 | ADecl = FTD->getTemplatedDecl(); |
| 2492 | |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2493 | auto *FD = dyn_cast<FunctionDecl>(ADecl); |
| 2494 | if (!FD) { |
| 2495 | Diag(ADecl->getLocation(), diag::err_omp_function_expected); |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2496 | return DeclGroupPtrTy(); |
| 2497 | } |
| 2498 | |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 2499 | // OpenMP [2.8.2, declare simd construct, Description] |
| 2500 | // The parameter of the simdlen clause must be a constant positive integer |
| 2501 | // expression. |
| 2502 | ExprResult SL; |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2503 | if (Simdlen) |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 2504 | SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2505 | // OpenMP [2.8.2, declare simd construct, Description] |
| 2506 | // The special this pointer can be used as if was one of the arguments to the |
| 2507 | // function in any of the linear, aligned, or uniform clauses. |
| 2508 | // The uniform clause declares one or more arguments to have an invariant |
| 2509 | // value for all concurrent invocations of the function in the execution of a |
| 2510 | // single SIMD loop. |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2511 | llvm::DenseMap<Decl *, Expr *> UniformedArgs; |
| 2512 | Expr *UniformedLinearThis = nullptr; |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2513 | for (auto *E : Uniforms) { |
| 2514 | E = E->IgnoreParenImpCasts(); |
| 2515 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2516 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 2517 | if (FD->getNumParams() > PVD->getFunctionScopeIndex() && |
| 2518 | FD->getParamDecl(PVD->getFunctionScopeIndex()) |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2519 | ->getCanonicalDecl() == PVD->getCanonicalDecl()) { |
| 2520 | UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E)); |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2521 | continue; |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2522 | } |
| 2523 | if (isa<CXXThisExpr>(E)) { |
| 2524 | UniformedLinearThis = E; |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2525 | continue; |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2526 | } |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2527 | Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) |
| 2528 | << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 2529 | } |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 2530 | // OpenMP [2.8.2, declare simd construct, Description] |
| 2531 | // The aligned clause declares that the object to which each list item points |
| 2532 | // is aligned to the number of bytes expressed in the optional parameter of |
| 2533 | // the aligned clause. |
| 2534 | // The special this pointer can be used as if was one of the arguments to the |
| 2535 | // function in any of the linear, aligned, or uniform clauses. |
| 2536 | // The type of list items appearing in the aligned clause must be array, |
| 2537 | // pointer, reference to array, or reference to pointer. |
| 2538 | llvm::DenseMap<Decl *, Expr *> AlignedArgs; |
| 2539 | Expr *AlignedThis = nullptr; |
| 2540 | for (auto *E : Aligneds) { |
| 2541 | E = E->IgnoreParenImpCasts(); |
| 2542 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2543 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 2544 | auto *CanonPVD = PVD->getCanonicalDecl(); |
| 2545 | if (FD->getNumParams() > PVD->getFunctionScopeIndex() && |
| 2546 | FD->getParamDecl(PVD->getFunctionScopeIndex()) |
| 2547 | ->getCanonicalDecl() == CanonPVD) { |
| 2548 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 2549 | // A list-item cannot appear in more than one aligned clause. |
| 2550 | if (AlignedArgs.count(CanonPVD) > 0) { |
| 2551 | Diag(E->getExprLoc(), diag::err_omp_aligned_twice) |
| 2552 | << 1 << E->getSourceRange(); |
| 2553 | Diag(AlignedArgs[CanonPVD]->getExprLoc(), |
| 2554 | diag::note_omp_explicit_dsa) |
| 2555 | << getOpenMPClauseName(OMPC_aligned); |
| 2556 | continue; |
| 2557 | } |
| 2558 | AlignedArgs[CanonPVD] = E; |
| 2559 | QualType QTy = PVD->getType() |
| 2560 | .getNonReferenceType() |
| 2561 | .getUnqualifiedType() |
| 2562 | .getCanonicalType(); |
| 2563 | const Type *Ty = QTy.getTypePtrOrNull(); |
| 2564 | if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { |
| 2565 | Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) |
| 2566 | << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); |
| 2567 | Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; |
| 2568 | } |
| 2569 | continue; |
| 2570 | } |
| 2571 | } |
| 2572 | if (isa<CXXThisExpr>(E)) { |
| 2573 | if (AlignedThis) { |
| 2574 | Diag(E->getExprLoc(), diag::err_omp_aligned_twice) |
| 2575 | << 2 << E->getSourceRange(); |
| 2576 | Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) |
| 2577 | << getOpenMPClauseName(OMPC_aligned); |
| 2578 | } |
| 2579 | AlignedThis = E; |
| 2580 | continue; |
| 2581 | } |
| 2582 | Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) |
| 2583 | << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); |
| 2584 | } |
| 2585 | // The optional parameter of the aligned clause, alignment, must be a constant |
| 2586 | // positive integer expression. If no optional parameter is specified, |
| 2587 | // implementation-defined default alignments for SIMD instructions on the |
| 2588 | // target platforms are assumed. |
| 2589 | SmallVector<Expr *, 4> NewAligns; |
| 2590 | for (auto *E : Alignments) { |
| 2591 | ExprResult Align; |
| 2592 | if (E) |
| 2593 | Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); |
| 2594 | NewAligns.push_back(Align.get()); |
| 2595 | } |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2596 | // OpenMP [2.8.2, declare simd construct, Description] |
| 2597 | // The linear clause declares one or more list items to be private to a SIMD |
| 2598 | // lane and to have a linear relationship with respect to the iteration space |
| 2599 | // of a loop. |
| 2600 | // The special this pointer can be used as if was one of the arguments to the |
| 2601 | // function in any of the linear, aligned, or uniform clauses. |
| 2602 | // When a linear-step expression is specified in a linear clause it must be |
| 2603 | // either a constant integer expression or an integer-typed parameter that is |
| 2604 | // specified in a uniform clause on the directive. |
| 2605 | llvm::DenseMap<Decl *, Expr *> LinearArgs; |
| 2606 | const bool IsUniformedThis = UniformedLinearThis != nullptr; |
| 2607 | auto MI = LinModifiers.begin(); |
| 2608 | for (auto *E : Linears) { |
| 2609 | auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); |
| 2610 | ++MI; |
| 2611 | E = E->IgnoreParenImpCasts(); |
| 2612 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2613 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 2614 | auto *CanonPVD = PVD->getCanonicalDecl(); |
| 2615 | if (FD->getNumParams() > PVD->getFunctionScopeIndex() && |
| 2616 | FD->getParamDecl(PVD->getFunctionScopeIndex()) |
| 2617 | ->getCanonicalDecl() == CanonPVD) { |
| 2618 | // OpenMP [2.15.3.7, linear Clause, Restrictions] |
| 2619 | // A list-item cannot appear in more than one linear clause. |
| 2620 | if (LinearArgs.count(CanonPVD) > 0) { |
| 2621 | Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) |
| 2622 | << getOpenMPClauseName(OMPC_linear) |
| 2623 | << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); |
| 2624 | Diag(LinearArgs[CanonPVD]->getExprLoc(), |
| 2625 | diag::note_omp_explicit_dsa) |
| 2626 | << getOpenMPClauseName(OMPC_linear); |
| 2627 | continue; |
| 2628 | } |
| 2629 | // Each argument can appear in at most one uniform or linear clause. |
| 2630 | if (UniformedArgs.count(CanonPVD) > 0) { |
| 2631 | Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) |
| 2632 | << getOpenMPClauseName(OMPC_linear) |
| 2633 | << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); |
| 2634 | Diag(UniformedArgs[CanonPVD]->getExprLoc(), |
| 2635 | diag::note_omp_explicit_dsa) |
| 2636 | << getOpenMPClauseName(OMPC_uniform); |
| 2637 | continue; |
| 2638 | } |
| 2639 | LinearArgs[CanonPVD] = E; |
| 2640 | if (E->isValueDependent() || E->isTypeDependent() || |
| 2641 | E->isInstantiationDependent() || |
| 2642 | E->containsUnexpandedParameterPack()) |
| 2643 | continue; |
| 2644 | (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, |
| 2645 | PVD->getOriginalType()); |
| 2646 | continue; |
| 2647 | } |
| 2648 | } |
| 2649 | if (isa<CXXThisExpr>(E)) { |
| 2650 | if (UniformedLinearThis) { |
| 2651 | Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) |
| 2652 | << getOpenMPClauseName(OMPC_linear) |
| 2653 | << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) |
| 2654 | << E->getSourceRange(); |
| 2655 | Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) |
| 2656 | << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform |
| 2657 | : OMPC_linear); |
| 2658 | continue; |
| 2659 | } |
| 2660 | UniformedLinearThis = E; |
| 2661 | if (E->isValueDependent() || E->isTypeDependent() || |
| 2662 | E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) |
| 2663 | continue; |
| 2664 | (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, |
| 2665 | E->getType()); |
| 2666 | continue; |
| 2667 | } |
| 2668 | Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) |
| 2669 | << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); |
| 2670 | } |
| 2671 | Expr *Step = nullptr; |
| 2672 | Expr *NewStep = nullptr; |
| 2673 | SmallVector<Expr *, 4> NewSteps; |
| 2674 | for (auto *E : Steps) { |
| 2675 | // Skip the same step expression, it was checked already. |
| 2676 | if (Step == E || !E) { |
| 2677 | NewSteps.push_back(E ? NewStep : nullptr); |
| 2678 | continue; |
| 2679 | } |
| 2680 | Step = E; |
| 2681 | if (auto *DRE = dyn_cast<DeclRefExpr>(Step)) |
| 2682 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 2683 | auto *CanonPVD = PVD->getCanonicalDecl(); |
| 2684 | if (UniformedArgs.count(CanonPVD) == 0) { |
| 2685 | Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) |
| 2686 | << Step->getSourceRange(); |
| 2687 | } else if (E->isValueDependent() || E->isTypeDependent() || |
| 2688 | E->isInstantiationDependent() || |
| 2689 | E->containsUnexpandedParameterPack() || |
| 2690 | CanonPVD->getType()->hasIntegerRepresentation()) |
| 2691 | NewSteps.push_back(Step); |
| 2692 | else { |
| 2693 | Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) |
| 2694 | << Step->getSourceRange(); |
| 2695 | } |
| 2696 | continue; |
| 2697 | } |
| 2698 | NewStep = Step; |
| 2699 | if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && |
| 2700 | !Step->isInstantiationDependent() && |
| 2701 | !Step->containsUnexpandedParameterPack()) { |
| 2702 | NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) |
| 2703 | .get(); |
| 2704 | if (NewStep) |
| 2705 | NewStep = VerifyIntegerConstantExpression(NewStep).get(); |
| 2706 | } |
| 2707 | NewSteps.push_back(NewStep); |
| 2708 | } |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 2709 | auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( |
| 2710 | Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 2711 | Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 2712 | const_cast<Expr **>(NewAligns.data()), NewAligns.size(), |
| 2713 | const_cast<Expr **>(Linears.data()), Linears.size(), |
| 2714 | const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), |
| 2715 | NewSteps.data(), NewSteps.size(), SR); |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 2716 | ADecl->addAttr(NewAttr); |
| 2717 | return ConvertDeclToDeclGroup(ADecl); |
| 2718 | } |
| 2719 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2720 | StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, |
| 2721 | Stmt *AStmt, |
| 2722 | SourceLocation StartLoc, |
| 2723 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 2724 | if (!AStmt) |
| 2725 | return StmtError(); |
| 2726 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2727 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 2728 | // 1.2.2 OpenMP Language Terminology |
| 2729 | // Structured block - An executable statement with a single entry at the |
| 2730 | // top and a single exit at the bottom. |
| 2731 | // The point of exit cannot be a branch out of the structured block. |
| 2732 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 2733 | CS->getCapturedDecl()->setNothrow(); |
| 2734 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2735 | getCurFunction()->setHasBranchProtectedScope(); |
| 2736 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2737 | return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, |
| 2738 | DSAStack->isCancelRegion()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2741 | namespace { |
| 2742 | /// \brief Helper class for checking canonical form of the OpenMP loops and |
| 2743 | /// extracting iteration space of each loop in the loop nest, that will be used |
| 2744 | /// for IR generation. |
| 2745 | class OpenMPIterationSpaceChecker { |
| 2746 | /// \brief Reference to Sema. |
| 2747 | Sema &SemaRef; |
| 2748 | /// \brief A location for diagnostics (when there is no some better location). |
| 2749 | SourceLocation DefaultLoc; |
| 2750 | /// \brief A location for diagnostics (when increment is not compatible). |
| 2751 | SourceLocation ConditionLoc; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2752 | /// \brief A source location for referring to loop init later. |
| 2753 | SourceRange InitSrcRange; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2754 | /// \brief A source location for referring to condition later. |
| 2755 | SourceRange ConditionSrcRange; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2756 | /// \brief A source location for referring to increment later. |
| 2757 | SourceRange IncrementSrcRange; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2758 | /// \brief Loop variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2759 | ValueDecl *LCDecl = nullptr; |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2760 | /// \brief Reference to loop variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2761 | Expr *LCRef = nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2762 | /// \brief Lower bound (initializer for the var). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2763 | Expr *LB = nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2764 | /// \brief Upper bound. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2765 | Expr *UB = nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2766 | /// \brief Loop step (increment). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2767 | Expr *Step = nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2768 | /// \brief This flag is true when condition is one of: |
| 2769 | /// Var < UB |
| 2770 | /// Var <= UB |
| 2771 | /// UB > Var |
| 2772 | /// UB >= Var |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2773 | bool TestIsLessOp = false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2774 | /// \brief This flag is true when condition is strict ( < or > ). |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2775 | bool TestIsStrictOp = false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2776 | /// \brief This flag is true when step is subtracted on each iteration. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2777 | bool SubtractStep = false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2778 | |
| 2779 | public: |
| 2780 | OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2781 | : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2782 | /// \brief Check init-expr for canonical loop form and save loop counter |
| 2783 | /// variable - #Var and its initialization value - #LB. |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2784 | bool CheckInit(Stmt *S, bool EmitDiags = true); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2785 | /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags |
| 2786 | /// for less/greater and for strict/non-strict comparison. |
| 2787 | bool CheckCond(Expr *S); |
| 2788 | /// \brief Check incr-expr for canonical loop form and return true if it |
| 2789 | /// does not conform, otherwise save loop step (#Step). |
| 2790 | bool CheckInc(Expr *S); |
| 2791 | /// \brief Return the loop counter variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2792 | ValueDecl *GetLoopDecl() const { return LCDecl; } |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2793 | /// \brief Return the reference expression to loop counter variable. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2794 | Expr *GetLoopDeclRefExpr() const { return LCRef; } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2795 | /// \brief Source range of the loop init. |
| 2796 | SourceRange GetInitSrcRange() const { return InitSrcRange; } |
| 2797 | /// \brief Source range of the loop condition. |
| 2798 | SourceRange GetConditionSrcRange() const { return ConditionSrcRange; } |
| 2799 | /// \brief Source range of the loop increment. |
| 2800 | SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; } |
| 2801 | /// \brief True if the step should be subtracted. |
| 2802 | bool ShouldSubtractStep() const { return SubtractStep; } |
| 2803 | /// \brief Build the expression to calculate the number of iterations. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2804 | Expr * |
| 2805 | BuildNumIterations(Scope *S, const bool LimitedType, |
| 2806 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2807 | /// \brief Build the precondition expression for the loops. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2808 | Expr *BuildPreCond(Scope *S, Expr *Cond, |
| 2809 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2810 | /// \brief Build reference expression to the counter be used for codegen. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2811 | DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures, |
| 2812 | DSAStackTy &DSA) const; |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 2813 | /// \brief Build reference expression to the private counter be used for |
| 2814 | /// codegen. |
| 2815 | Expr *BuildPrivateCounterVar() const; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2816 | /// \brief Build initialization of the counter be used for codegen. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2817 | Expr *BuildCounterInit() const; |
| 2818 | /// \brief Build step of the counter be used for codegen. |
| 2819 | Expr *BuildCounterStep() const; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2820 | /// \brief Return true if any expression is dependent. |
| 2821 | bool Dependent() const; |
| 2822 | |
| 2823 | private: |
| 2824 | /// \brief Check the right-hand side of an assignment in the increment |
| 2825 | /// expression. |
| 2826 | bool CheckIncRHS(Expr *RHS); |
| 2827 | /// \brief Helper to set loop counter variable and its initializer. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2828 | bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2829 | /// \brief Helper to set upper bound. |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 2830 | bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, |
Craig Topper | 9cd5e4f | 2015-09-21 01:23:32 +0000 | [diff] [blame] | 2831 | SourceLocation SL); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2832 | /// \brief Helper to set loop increment. |
| 2833 | bool SetStep(Expr *NewStep, bool Subtract); |
| 2834 | }; |
| 2835 | |
| 2836 | bool OpenMPIterationSpaceChecker::Dependent() const { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2837 | if (!LCDecl) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2838 | assert(!LB && !UB && !Step); |
| 2839 | return false; |
| 2840 | } |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2841 | return LCDecl->getType()->isDependentType() || |
| 2842 | (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || |
| 2843 | (Step && Step->isValueDependent()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2846 | static Expr *getExprAsWritten(Expr *E) { |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 2847 | if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E)) |
| 2848 | E = ExprTemp->getSubExpr(); |
| 2849 | |
| 2850 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) |
| 2851 | E = MTE->GetTemporaryExpr(); |
| 2852 | |
| 2853 | while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 2854 | E = Binder->getSubExpr(); |
| 2855 | |
| 2856 | if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 2857 | E = ICE->getSubExprAsWritten(); |
| 2858 | return E->IgnoreParens(); |
| 2859 | } |
| 2860 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2861 | bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl, |
| 2862 | Expr *NewLCRefExpr, |
| 2863 | Expr *NewLB) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2864 | // State consistency checking to ensure correct usage. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2865 | assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && |
Alexey Bataev | caf09b0 | 2014-07-25 06:27:47 +0000 | [diff] [blame] | 2866 | UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2867 | if (!NewLCDecl || !NewLB) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2868 | return true; |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2869 | LCDecl = getCanonicalDecl(NewLCDecl); |
| 2870 | LCRef = NewLCRefExpr; |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 2871 | if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) |
| 2872 | if (const CXXConstructorDecl *Ctor = CE->getConstructor()) |
Alexey Bataev | 0d08a7f | 2015-07-16 04:19:43 +0000 | [diff] [blame] | 2873 | if ((Ctor->isCopyOrMoveConstructor() || |
| 2874 | Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && |
| 2875 | CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 2876 | NewLB = CE->getArg(0)->IgnoreParenImpCasts(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2877 | LB = NewLB; |
| 2878 | return false; |
| 2879 | } |
| 2880 | |
| 2881 | bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 2882 | SourceRange SR, SourceLocation SL) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2883 | // State consistency checking to ensure correct usage. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2884 | assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && |
| 2885 | Step == nullptr && !TestIsLessOp && !TestIsStrictOp); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2886 | if (!NewUB) |
| 2887 | return true; |
| 2888 | UB = NewUB; |
| 2889 | TestIsLessOp = LessOp; |
| 2890 | TestIsStrictOp = StrictOp; |
| 2891 | ConditionSrcRange = SR; |
| 2892 | ConditionLoc = SL; |
| 2893 | return false; |
| 2894 | } |
| 2895 | |
| 2896 | bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { |
| 2897 | // State consistency checking to ensure correct usage. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2898 | assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2899 | if (!NewStep) |
| 2900 | return true; |
| 2901 | if (!NewStep->isValueDependent()) { |
| 2902 | // Check that the step is integer expression. |
| 2903 | SourceLocation StepLoc = NewStep->getLocStart(); |
| 2904 | ExprResult Val = |
| 2905 | SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); |
| 2906 | if (Val.isInvalid()) |
| 2907 | return true; |
| 2908 | NewStep = Val.get(); |
| 2909 | |
| 2910 | // OpenMP [2.6, Canonical Loop Form, Restrictions] |
| 2911 | // If test-expr is of form var relational-op b and relational-op is < or |
| 2912 | // <= then incr-expr must cause var to increase on each iteration of the |
| 2913 | // loop. If test-expr is of form var relational-op b and relational-op is |
| 2914 | // > or >= then incr-expr must cause var to decrease on each iteration of |
| 2915 | // the loop. |
| 2916 | // If test-expr is of form b relational-op var and relational-op is < or |
| 2917 | // <= then incr-expr must cause var to decrease on each iteration of the |
| 2918 | // loop. If test-expr is of form b relational-op var and relational-op is |
| 2919 | // > or >= then incr-expr must cause var to increase on each iteration of |
| 2920 | // the loop. |
| 2921 | llvm::APSInt Result; |
| 2922 | bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); |
| 2923 | bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); |
| 2924 | bool IsConstNeg = |
| 2925 | IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2926 | bool IsConstPos = |
| 2927 | IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2928 | bool IsConstZero = IsConstant && !Result.getBoolValue(); |
| 2929 | if (UB && (IsConstZero || |
| 2930 | (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2931 | : (IsConstPos || (IsUnsigned && !Subtract))))) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2932 | SemaRef.Diag(NewStep->getExprLoc(), |
| 2933 | diag::err_omp_loop_incr_not_compatible) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2934 | << LCDecl << TestIsLessOp << NewStep->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2935 | SemaRef.Diag(ConditionLoc, |
| 2936 | diag::note_omp_loop_cond_requres_compatible_incr) |
| 2937 | << TestIsLessOp << ConditionSrcRange; |
| 2938 | return true; |
| 2939 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2940 | if (TestIsLessOp == Subtract) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2941 | NewStep = |
| 2942 | SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) |
| 2943 | .get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2944 | Subtract = !Subtract; |
| 2945 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | Step = NewStep; |
| 2949 | SubtractStep = Subtract; |
| 2950 | return false; |
| 2951 | } |
| 2952 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2953 | bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2954 | // Check init-expr for canonical loop form and save loop counter |
| 2955 | // variable - #Var and its initialization value - #LB. |
| 2956 | // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: |
| 2957 | // var = lb |
| 2958 | // integer-type var = lb |
| 2959 | // random-access-iterator-type var = lb |
| 2960 | // pointer-type var = lb |
| 2961 | // |
| 2962 | if (!S) { |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2963 | if (EmitDiags) { |
| 2964 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); |
| 2965 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2966 | return true; |
| 2967 | } |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 2968 | if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) |
| 2969 | if (!ExprTemp->cleanupsHaveSideEffects()) |
| 2970 | S = ExprTemp->getSubExpr(); |
| 2971 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2972 | InitSrcRange = S->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2973 | if (Expr *E = dyn_cast<Expr>(S)) |
| 2974 | S = E->IgnoreParens(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2975 | if (auto *BO = dyn_cast<BinaryOperator>(S)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2976 | if (BO->getOpcode() == BO_Assign) { |
| 2977 | auto *LHS = BO->getLHS()->IgnoreParens(); |
| 2978 | if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { |
| 2979 | if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) |
| 2980 | if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) |
| 2981 | return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); |
| 2982 | return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); |
| 2983 | } |
| 2984 | if (auto *ME = dyn_cast<MemberExpr>(LHS)) { |
| 2985 | if (ME->isArrow() && |
| 2986 | isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) |
| 2987 | return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); |
| 2988 | } |
| 2989 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2990 | } else if (auto *DS = dyn_cast<DeclStmt>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2991 | if (DS->isSingleDecl()) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 2992 | if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 2993 | if (Var->hasInit() && !Var->getType()->isReferenceType()) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2994 | // Accept non-canonical init form here but emit ext. warning. |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 2995 | if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2996 | SemaRef.Diag(S->getLocStart(), |
| 2997 | diag::ext_omp_loop_not_canonical_init) |
| 2998 | << S->getSourceRange(); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 2999 | return SetLCDeclAndLB(Var, nullptr, Var->getInit()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3000 | } |
| 3001 | } |
| 3002 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3003 | } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3004 | if (CE->getOperator() == OO_Equal) { |
| 3005 | auto *LHS = CE->getArg(0); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3006 | if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3007 | if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) |
| 3008 | if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) |
| 3009 | return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); |
| 3010 | return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); |
| 3011 | } |
| 3012 | if (auto *ME = dyn_cast<MemberExpr>(LHS)) { |
| 3013 | if (ME->isArrow() && |
| 3014 | isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) |
| 3015 | return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); |
| 3016 | } |
| 3017 | } |
| 3018 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3019 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3020 | if (Dependent() || SemaRef.CurContext->isDependentContext()) |
| 3021 | return false; |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3022 | if (EmitDiags) { |
| 3023 | SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) |
| 3024 | << S->getSourceRange(); |
| 3025 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3026 | return true; |
| 3027 | } |
| 3028 | |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3029 | /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3030 | /// variable (which may be the loop variable) if possible. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3031 | static const ValueDecl *GetInitLCDecl(Expr *E) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3032 | if (!E) |
Craig Topper | 4b56692 | 2014-06-09 02:04:02 +0000 | [diff] [blame] | 3033 | return nullptr; |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3034 | E = getExprAsWritten(E); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3035 | if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) |
| 3036 | if (const CXXConstructorDecl *Ctor = CE->getConstructor()) |
Alexey Bataev | 0d08a7f | 2015-07-16 04:19:43 +0000 | [diff] [blame] | 3037 | if ((Ctor->isCopyOrMoveConstructor() || |
| 3038 | Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && |
| 3039 | CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3040 | E = CE->getArg(0)->IgnoreParenImpCasts(); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3041 | if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { |
| 3042 | if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 3043 | if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) |
| 3044 | if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) |
| 3045 | return getCanonicalDecl(ME->getMemberDecl()); |
| 3046 | return getCanonicalDecl(VD); |
| 3047 | } |
| 3048 | } |
| 3049 | if (auto *ME = dyn_cast_or_null<MemberExpr>(E)) |
| 3050 | if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) |
| 3051 | return getCanonicalDecl(ME->getMemberDecl()); |
| 3052 | return nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { |
| 3056 | // Check test-expr for canonical form, save upper-bound UB, flags for |
| 3057 | // less/greater and for strict/non-strict comparison. |
| 3058 | // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: |
| 3059 | // var relational-op b |
| 3060 | // b relational-op var |
| 3061 | // |
| 3062 | if (!S) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3063 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3064 | return true; |
| 3065 | } |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3066 | S = getExprAsWritten(S); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3067 | SourceLocation CondLoc = S->getLocStart(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3068 | if (auto *BO = dyn_cast<BinaryOperator>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3069 | if (BO->isRelationalOp()) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3070 | if (GetInitLCDecl(BO->getLHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3071 | return SetUB(BO->getRHS(), |
| 3072 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), |
| 3073 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), |
| 3074 | BO->getSourceRange(), BO->getOperatorLoc()); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3075 | if (GetInitLCDecl(BO->getRHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3076 | return SetUB(BO->getLHS(), |
| 3077 | (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), |
| 3078 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), |
| 3079 | BO->getSourceRange(), BO->getOperatorLoc()); |
| 3080 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3081 | } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3082 | if (CE->getNumArgs() == 2) { |
| 3083 | auto Op = CE->getOperator(); |
| 3084 | switch (Op) { |
| 3085 | case OO_Greater: |
| 3086 | case OO_GreaterEqual: |
| 3087 | case OO_Less: |
| 3088 | case OO_LessEqual: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3089 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3090 | return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, |
| 3091 | Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), |
| 3092 | CE->getOperatorLoc()); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3093 | if (GetInitLCDecl(CE->getArg(1)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3094 | return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, |
| 3095 | Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), |
| 3096 | CE->getOperatorLoc()); |
| 3097 | break; |
| 3098 | default: |
| 3099 | break; |
| 3100 | } |
| 3101 | } |
| 3102 | } |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3103 | if (Dependent() || SemaRef.CurContext->isDependentContext()) |
| 3104 | return false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3105 | SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3106 | << S->getSourceRange() << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3107 | return true; |
| 3108 | } |
| 3109 | |
| 3110 | bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { |
| 3111 | // RHS of canonical loop form increment can be: |
| 3112 | // var + incr |
| 3113 | // incr + var |
| 3114 | // var - incr |
| 3115 | // |
| 3116 | RHS = RHS->IgnoreParenImpCasts(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3117 | if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3118 | if (BO->isAdditiveOp()) { |
| 3119 | bool IsAdd = BO->getOpcode() == BO_Add; |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3120 | if (GetInitLCDecl(BO->getLHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3121 | return SetStep(BO->getRHS(), !IsAdd); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3122 | if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3123 | return SetStep(BO->getLHS(), false); |
| 3124 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3125 | } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3126 | bool IsAdd = CE->getOperator() == OO_Plus; |
| 3127 | if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3128 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3129 | return SetStep(CE->getArg(1), !IsAdd); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3130 | if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3131 | return SetStep(CE->getArg(0), false); |
| 3132 | } |
| 3133 | } |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3134 | if (Dependent() || SemaRef.CurContext->isDependentContext()) |
| 3135 | return false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3136 | SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3137 | << RHS->getSourceRange() << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3138 | return true; |
| 3139 | } |
| 3140 | |
| 3141 | bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { |
| 3142 | // Check incr-expr for canonical loop form and return true if it |
| 3143 | // does not conform. |
| 3144 | // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: |
| 3145 | // ++var |
| 3146 | // var++ |
| 3147 | // --var |
| 3148 | // var-- |
| 3149 | // var += incr |
| 3150 | // var -= incr |
| 3151 | // var = var + incr |
| 3152 | // var = incr + var |
| 3153 | // var = var - incr |
| 3154 | // |
| 3155 | if (!S) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3156 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3157 | return true; |
| 3158 | } |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 3159 | if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) |
| 3160 | if (!ExprTemp->cleanupsHaveSideEffects()) |
| 3161 | S = ExprTemp->getSubExpr(); |
| 3162 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3163 | IncrementSrcRange = S->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3164 | S = S->IgnoreParens(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3165 | if (auto *UO = dyn_cast<UnaryOperator>(S)) { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3166 | if (UO->isIncrementDecrementOp() && |
| 3167 | GetInitLCDecl(UO->getSubExpr()) == LCDecl) |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3168 | return SetStep(SemaRef |
| 3169 | .ActOnIntegerConstant(UO->getLocStart(), |
| 3170 | (UO->isDecrementOp() ? -1 : 1)) |
| 3171 | .get(), |
| 3172 | false); |
| 3173 | } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3174 | switch (BO->getOpcode()) { |
| 3175 | case BO_AddAssign: |
| 3176 | case BO_SubAssign: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3177 | if (GetInitLCDecl(BO->getLHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3178 | return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); |
| 3179 | break; |
| 3180 | case BO_Assign: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3181 | if (GetInitLCDecl(BO->getLHS()) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3182 | return CheckIncRHS(BO->getRHS()); |
| 3183 | break; |
| 3184 | default: |
| 3185 | break; |
| 3186 | } |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3187 | } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3188 | switch (CE->getOperator()) { |
| 3189 | case OO_PlusPlus: |
| 3190 | case OO_MinusMinus: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3191 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3192 | return SetStep(SemaRef |
| 3193 | .ActOnIntegerConstant( |
| 3194 | CE->getLocStart(), |
| 3195 | ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) |
| 3196 | .get(), |
| 3197 | false); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3198 | break; |
| 3199 | case OO_PlusEqual: |
| 3200 | case OO_MinusEqual: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3201 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3202 | return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); |
| 3203 | break; |
| 3204 | case OO_Equal: |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3205 | if (GetInitLCDecl(CE->getArg(0)) == LCDecl) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3206 | return CheckIncRHS(CE->getArg(1)); |
| 3207 | break; |
| 3208 | default: |
| 3209 | break; |
| 3210 | } |
| 3211 | } |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3212 | if (Dependent() || SemaRef.CurContext->isDependentContext()) |
| 3213 | return false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3214 | SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3215 | << S->getSourceRange() << LCDecl; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3216 | return true; |
| 3217 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3218 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3219 | static ExprResult |
| 3220 | tryBuildCapture(Sema &SemaRef, Expr *Capture, |
| 3221 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 3222 | if (SemaRef.CurContext->isDependentContext()) |
| 3223 | return ExprResult(Capture); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3224 | if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) |
| 3225 | return SemaRef.PerformImplicitConversion( |
| 3226 | Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, |
| 3227 | /*AllowExplicit=*/true); |
| 3228 | auto I = Captures.find(Capture); |
| 3229 | if (I != Captures.end()) |
| 3230 | return buildCapture(SemaRef, Capture, I->second); |
| 3231 | DeclRefExpr *Ref = nullptr; |
| 3232 | ExprResult Res = buildCapture(SemaRef, Capture, Ref); |
| 3233 | Captures[Capture] = Ref; |
| 3234 | return Res; |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3237 | /// \brief Build the expression to calculate the number of iterations. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3238 | Expr *OpenMPIterationSpaceChecker::BuildNumIterations( |
| 3239 | Scope *S, const bool LimitedType, |
| 3240 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3241 | ExprResult Diff; |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3242 | auto VarType = LCDecl->getType().getNonReferenceType(); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3243 | if (VarType->isIntegerType() || VarType->isPointerType() || |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3244 | SemaRef.getLangOpts().CPlusPlus) { |
| 3245 | // Upper - Lower |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3246 | auto *UBExpr = TestIsLessOp ? UB : LB; |
| 3247 | auto *LBExpr = TestIsLessOp ? LB : UB; |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3248 | Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); |
| 3249 | Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3250 | if (!Upper || !Lower) |
| 3251 | return nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3252 | |
| 3253 | Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); |
| 3254 | |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3255 | if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3256 | // BuildBinOp already emitted error, this one is to point user to upper |
| 3257 | // and lower bound, and to tell what is passed to 'operator-'. |
| 3258 | SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) |
| 3259 | << Upper->getSourceRange() << Lower->getSourceRange(); |
| 3260 | return nullptr; |
| 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | if (!Diff.isUsable()) |
| 3265 | return nullptr; |
| 3266 | |
| 3267 | // Upper - Lower [- 1] |
| 3268 | if (TestIsStrictOp) |
| 3269 | Diff = SemaRef.BuildBinOp( |
| 3270 | S, DefaultLoc, BO_Sub, Diff.get(), |
| 3271 | SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); |
| 3272 | if (!Diff.isUsable()) |
| 3273 | return nullptr; |
| 3274 | |
| 3275 | // Upper - Lower [- 1] + Step |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3276 | auto NewStep = tryBuildCapture(SemaRef, Step, Captures); |
| 3277 | if (!NewStep.isUsable()) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3278 | return nullptr; |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3279 | Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3280 | if (!Diff.isUsable()) |
| 3281 | return nullptr; |
| 3282 | |
| 3283 | // Parentheses (for dumping/debugging purposes only). |
| 3284 | Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); |
| 3285 | if (!Diff.isUsable()) |
| 3286 | return nullptr; |
| 3287 | |
| 3288 | // (Upper - Lower [- 1] + Step) / Step |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3289 | Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3290 | if (!Diff.isUsable()) |
| 3291 | return nullptr; |
| 3292 | |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3293 | // OpenMP runtime requires 32-bit or 64-bit loop variables. |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3294 | QualType Type = Diff.get()->getType(); |
| 3295 | auto &C = SemaRef.Context; |
| 3296 | bool UseVarType = VarType->hasIntegerRepresentation() && |
| 3297 | C.getTypeSize(Type) > C.getTypeSize(VarType); |
| 3298 | if (!Type->isIntegerType() || UseVarType) { |
| 3299 | unsigned NewSize = |
| 3300 | UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); |
| 3301 | bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() |
| 3302 | : Type->hasSignedIntegerRepresentation(); |
| 3303 | Type = C.getIntTypeForBitwidth(NewSize, IsSigned); |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3304 | if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { |
| 3305 | Diff = SemaRef.PerformImplicitConversion( |
| 3306 | Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); |
| 3307 | if (!Diff.isUsable()) |
| 3308 | return nullptr; |
| 3309 | } |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3310 | } |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3311 | if (LimitedType) { |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3312 | unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; |
| 3313 | if (NewSize != C.getTypeSize(Type)) { |
| 3314 | if (NewSize < C.getTypeSize(Type)) { |
| 3315 | assert(NewSize == 64 && "incorrect loop var size"); |
| 3316 | SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) |
| 3317 | << InitSrcRange << ConditionSrcRange; |
| 3318 | } |
| 3319 | QualType NewType = C.getIntTypeForBitwidth( |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3320 | NewSize, Type->hasSignedIntegerRepresentation() || |
| 3321 | C.getTypeSize(Type) < NewSize); |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3322 | if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { |
| 3323 | Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, |
| 3324 | Sema::AA_Converting, true); |
| 3325 | if (!Diff.isUsable()) |
| 3326 | return nullptr; |
| 3327 | } |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3328 | } |
| 3329 | } |
| 3330 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3331 | return Diff.get(); |
| 3332 | } |
| 3333 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3334 | Expr *OpenMPIterationSpaceChecker::BuildPreCond( |
| 3335 | Scope *S, Expr *Cond, |
| 3336 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3337 | // Try to build LB <op> UB, where <op> is <, >, <=, or >=. |
| 3338 | bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); |
| 3339 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3340 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3341 | auto NewLB = tryBuildCapture(SemaRef, LB, Captures); |
| 3342 | auto NewUB = tryBuildCapture(SemaRef, UB, Captures); |
| 3343 | if (!NewLB.isUsable() || !NewUB.isUsable()) |
| 3344 | return nullptr; |
| 3345 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3346 | auto CondExpr = SemaRef.BuildBinOp( |
| 3347 | S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) |
| 3348 | : (TestIsStrictOp ? BO_GT : BO_GE), |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3349 | NewLB.get(), NewUB.get()); |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3350 | if (CondExpr.isUsable()) { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3351 | if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), |
| 3352 | SemaRef.Context.BoolTy)) |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3353 | CondExpr = SemaRef.PerformImplicitConversion( |
| 3354 | CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, |
| 3355 | /*AllowExplicit=*/true); |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 3356 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3357 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); |
| 3358 | // Otherwise use original loop conditon and evaluate it in runtime. |
| 3359 | return CondExpr.isUsable() ? CondExpr.get() : Cond; |
| 3360 | } |
| 3361 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3362 | /// \brief Build reference expression to the counter be used for codegen. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3363 | DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3364 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3365 | auto *VD = dyn_cast<VarDecl>(LCDecl); |
| 3366 | if (!VD) { |
| 3367 | VD = SemaRef.IsOpenMPCapturedDecl(LCDecl); |
| 3368 | auto *Ref = buildDeclRefExpr( |
| 3369 | SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3370 | DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false); |
| 3371 | // If the loop control decl is explicitly marked as private, do not mark it |
| 3372 | // as captured again. |
| 3373 | if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) |
| 3374 | Captures.insert(std::make_pair(LCRef, Ref)); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3375 | return Ref; |
| 3376 | } |
| 3377 | return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3378 | DefaultLoc); |
| 3379 | } |
| 3380 | |
| 3381 | Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const { |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3382 | if (LCDecl && !LCDecl->isInvalidDecl()) { |
| 3383 | auto Type = LCDecl->getType().getNonReferenceType(); |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 3384 | auto *PrivateVar = |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3385 | buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(), |
| 3386 | LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3387 | if (PrivateVar->isInvalidDecl()) |
| 3388 | return nullptr; |
| 3389 | return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); |
| 3390 | } |
| 3391 | return nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 3394 | /// \brief Build initialization of the counter to be used for codegen. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3395 | Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; } |
| 3396 | |
| 3397 | /// \brief Build step of the counter be used for codegen. |
| 3398 | Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; } |
| 3399 | |
| 3400 | /// \brief Iteration space of a single for loop. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3401 | struct LoopIterationSpace final { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3402 | /// \brief Condition of the loop. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3403 | Expr *PreCond = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3404 | /// \brief This expression calculates the number of iterations in the loop. |
| 3405 | /// It is always possible to calculate it before starting the loop. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3406 | Expr *NumIterations = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3407 | /// \brief The loop counter variable. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3408 | Expr *CounterVar = nullptr; |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3409 | /// \brief Private loop counter variable. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3410 | Expr *PrivateCounterVar = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3411 | /// \brief This is initializer for the initial value of #CounterVar. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3412 | Expr *CounterInit = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3413 | /// \brief This is step for the #CounterVar used to generate its update: |
| 3414 | /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3415 | Expr *CounterStep = nullptr; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3416 | /// \brief Should step be subtracted? |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3417 | bool Subtract = false; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3418 | /// \brief Source range of the loop init. |
| 3419 | SourceRange InitSrcRange; |
| 3420 | /// \brief Source range of the loop condition. |
| 3421 | SourceRange CondSrcRange; |
| 3422 | /// \brief Source range of the loop increment. |
| 3423 | SourceRange IncSrcRange; |
| 3424 | }; |
| 3425 | |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3426 | } // namespace |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3427 | |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3428 | void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { |
| 3429 | assert(getLangOpts().OpenMP && "OpenMP is not active."); |
| 3430 | assert(Init && "Expected loop in canonical form."); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 3431 | unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); |
| 3432 | if (AssociatedLoops > 0 && |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3433 | isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { |
| 3434 | OpenMPIterationSpaceChecker ISC(*this, ForLoc); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3435 | if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) { |
| 3436 | if (auto *D = ISC.GetLoopDecl()) { |
| 3437 | auto *VD = dyn_cast<VarDecl>(D); |
| 3438 | if (!VD) { |
| 3439 | if (auto *Private = IsOpenMPCapturedDecl(D)) |
| 3440 | VD = Private; |
| 3441 | else { |
| 3442 | auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(), |
| 3443 | /*WithInit=*/false); |
| 3444 | VD = cast<VarDecl>(Ref->getDecl()); |
| 3445 | } |
| 3446 | } |
| 3447 | DSAStack->addLoopControlVariable(D, VD); |
| 3448 | } |
| 3449 | } |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 3450 | DSAStack->setAssociatedLoops(AssociatedLoops - 1); |
Alexey Bataev | 9c82103 | 2015-04-30 04:23:23 +0000 | [diff] [blame] | 3451 | } |
| 3452 | } |
| 3453 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3454 | /// \brief Called on a for stmt to check and extract its iteration space |
| 3455 | /// for further processing (such as collapsing). |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3456 | static bool CheckOpenMPIterationSpace( |
| 3457 | OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, |
| 3458 | unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3459 | Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 3460 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3461 | LoopIterationSpace &ResultIterSpace, |
| 3462 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3463 | // OpenMP [2.6, Canonical Loop Form] |
| 3464 | // for (init-expr; test-expr; incr-expr) structured-block |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3465 | auto *For = dyn_cast_or_null<ForStmt>(S); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3466 | if (!For) { |
| 3467 | SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3468 | << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) |
| 3469 | << getOpenMPDirectiveName(DKind) << NestedLoopCount |
| 3470 | << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; |
| 3471 | if (NestedLoopCount > 1) { |
| 3472 | if (CollapseLoopCountExpr && OrderedLoopCountExpr) |
| 3473 | SemaRef.Diag(DSA.getConstructLoc(), |
| 3474 | diag::note_omp_collapse_ordered_expr) |
| 3475 | << 2 << CollapseLoopCountExpr->getSourceRange() |
| 3476 | << OrderedLoopCountExpr->getSourceRange(); |
| 3477 | else if (CollapseLoopCountExpr) |
| 3478 | SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), |
| 3479 | diag::note_omp_collapse_ordered_expr) |
| 3480 | << 0 << CollapseLoopCountExpr->getSourceRange(); |
| 3481 | else |
| 3482 | SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), |
| 3483 | diag::note_omp_collapse_ordered_expr) |
| 3484 | << 1 << OrderedLoopCountExpr->getSourceRange(); |
| 3485 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3486 | return true; |
| 3487 | } |
| 3488 | assert(For->getBody()); |
| 3489 | |
| 3490 | OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); |
| 3491 | |
| 3492 | // Check init. |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 3493 | auto Init = For->getInit(); |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3494 | if (ISC.CheckInit(Init)) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3495 | return true; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3496 | |
| 3497 | bool HasErrors = false; |
| 3498 | |
| 3499 | // Check loop variable's type. |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3500 | if (auto *LCDecl = ISC.GetLoopDecl()) { |
| 3501 | auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3502 | |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3503 | // OpenMP [2.6, Canonical Loop Form] |
| 3504 | // Var is one of the following: |
| 3505 | // A variable of signed or unsigned integer type. |
| 3506 | // For C++, a variable of a random access iterator type. |
| 3507 | // For C, a variable of a pointer type. |
| 3508 | auto VarType = LCDecl->getType().getNonReferenceType(); |
| 3509 | if (!VarType->isDependentType() && !VarType->isIntegerType() && |
| 3510 | !VarType->isPointerType() && |
| 3511 | !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { |
| 3512 | SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) |
| 3513 | << SemaRef.getLangOpts().CPlusPlus; |
| 3514 | HasErrors = true; |
| 3515 | } |
| 3516 | |
| 3517 | // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in |
| 3518 | // a Construct |
| 3519 | // The loop iteration variable(s) in the associated for-loop(s) of a for or |
| 3520 | // parallel for construct is (are) private. |
| 3521 | // The loop iteration variable in the associated for-loop of a simd |
| 3522 | // construct with just one associated for-loop is linear with a |
| 3523 | // constant-linear-step that is the increment of the associated for-loop. |
| 3524 | // Exclude loop var from the list of variables with implicitly defined data |
| 3525 | // sharing attributes. |
| 3526 | VarsWithImplicitDSA.erase(LCDecl); |
| 3527 | |
| 3528 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 3529 | // in a Construct, C/C++]. |
| 3530 | // The loop iteration variable in the associated for-loop of a simd |
| 3531 | // construct with just one associated for-loop may be listed in a linear |
| 3532 | // clause with a constant-linear-step that is the increment of the |
| 3533 | // associated for-loop. |
| 3534 | // The loop iteration variable(s) in the associated for-loop(s) of a for or |
| 3535 | // parallel for construct may be listed in a private or lastprivate clause. |
| 3536 | DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); |
| 3537 | // If LoopVarRefExpr is nullptr it means the corresponding loop variable is |
| 3538 | // declared in the loop and it is predetermined as a private. |
| 3539 | auto PredeterminedCKind = |
| 3540 | isOpenMPSimdDirective(DKind) |
| 3541 | ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) |
| 3542 | : OMPC_private; |
| 3543 | if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && |
| 3544 | DVar.CKind != PredeterminedCKind) || |
| 3545 | ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || |
| 3546 | isOpenMPDistributeDirective(DKind)) && |
| 3547 | !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && |
| 3548 | DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && |
| 3549 | (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { |
| 3550 | SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) |
| 3551 | << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) |
| 3552 | << getOpenMPClauseName(PredeterminedCKind); |
| 3553 | if (DVar.RefExpr == nullptr) |
| 3554 | DVar.CKind = PredeterminedCKind; |
| 3555 | ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); |
| 3556 | HasErrors = true; |
| 3557 | } else if (LoopDeclRefExpr != nullptr) { |
| 3558 | // Make the loop iteration variable private (for worksharing constructs), |
| 3559 | // linear (for simd directives with the only one associated loop) or |
| 3560 | // lastprivate (for simd directives with several collapsed or ordered |
| 3561 | // loops). |
| 3562 | if (DVar.CKind == OMPC_unknown) |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 3563 | DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, |
| 3564 | [](OpenMPDirectiveKind) -> bool { return true; }, |
Alexey Bataev | c6ad97a | 2016-04-01 09:23:34 +0000 | [diff] [blame] | 3565 | /*FromParent=*/false); |
| 3566 | DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); |
| 3567 | } |
| 3568 | |
| 3569 | assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); |
| 3570 | |
| 3571 | // Check test-expr. |
| 3572 | HasErrors |= ISC.CheckCond(For->getCond()); |
| 3573 | |
| 3574 | // Check incr-expr. |
| 3575 | HasErrors |= ISC.CheckInc(For->getInc()); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3578 | if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3579 | return HasErrors; |
| 3580 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3581 | // Build the loop's iteration space representation. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3582 | ResultIterSpace.PreCond = |
| 3583 | ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures); |
Alexander Musman | 174b3ca | 2014-10-06 11:16:29 +0000 | [diff] [blame] | 3584 | ResultIterSpace.NumIterations = ISC.BuildNumIterations( |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3585 | DSA.getCurScope(), |
| 3586 | (isOpenMPWorksharingDirective(DKind) || |
| 3587 | isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), |
| 3588 | Captures); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3589 | ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3590 | ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3591 | ResultIterSpace.CounterInit = ISC.BuildCounterInit(); |
| 3592 | ResultIterSpace.CounterStep = ISC.BuildCounterStep(); |
| 3593 | ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange(); |
| 3594 | ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange(); |
| 3595 | ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange(); |
| 3596 | ResultIterSpace.Subtract = ISC.ShouldSubtractStep(); |
| 3597 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3598 | HasErrors |= (ResultIterSpace.PreCond == nullptr || |
| 3599 | ResultIterSpace.NumIterations == nullptr || |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3600 | ResultIterSpace.CounterVar == nullptr || |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 3601 | ResultIterSpace.PrivateCounterVar == nullptr || |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3602 | ResultIterSpace.CounterInit == nullptr || |
| 3603 | ResultIterSpace.CounterStep == nullptr); |
| 3604 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3605 | return HasErrors; |
| 3606 | } |
| 3607 | |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3608 | /// \brief Build 'VarRef = Start. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3609 | static ExprResult |
| 3610 | BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, |
| 3611 | ExprResult Start, |
| 3612 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3613 | // Build 'VarRef = Start. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3614 | auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); |
| 3615 | if (!NewStart.isUsable()) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3616 | return ExprError(); |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3617 | if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3618 | VarRef.get()->getType())) { |
| 3619 | NewStart = SemaRef.PerformImplicitConversion( |
| 3620 | NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, |
| 3621 | /*AllowExplicit=*/true); |
| 3622 | if (!NewStart.isUsable()) |
| 3623 | return ExprError(); |
| 3624 | } |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3625 | |
| 3626 | auto Init = |
| 3627 | SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); |
| 3628 | return Init; |
| 3629 | } |
| 3630 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3631 | /// \brief Build 'VarRef = Start + Iter * Step'. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3632 | static ExprResult |
| 3633 | BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc, |
| 3634 | ExprResult VarRef, ExprResult Start, ExprResult Iter, |
| 3635 | ExprResult Step, bool Subtract, |
| 3636 | llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3637 | // Add parentheses (for debugging purposes only). |
| 3638 | Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); |
| 3639 | if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || |
| 3640 | !Step.isUsable()) |
| 3641 | return ExprError(); |
| 3642 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3643 | ExprResult NewStep = Step; |
| 3644 | if (Captures) |
| 3645 | NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3646 | if (NewStep.isInvalid()) |
| 3647 | return ExprError(); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3648 | ExprResult Update = |
| 3649 | SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3650 | if (!Update.isUsable()) |
| 3651 | return ExprError(); |
| 3652 | |
Alexey Bataev | c0214e0 | 2016-02-16 12:13:49 +0000 | [diff] [blame] | 3653 | // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or |
| 3654 | // 'VarRef = Start (+|-) Iter * Step'. |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3655 | ExprResult NewStart = Start; |
| 3656 | if (Captures) |
| 3657 | NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3658 | if (NewStart.isInvalid()) |
| 3659 | return ExprError(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3660 | |
Alexey Bataev | c0214e0 | 2016-02-16 12:13:49 +0000 | [diff] [blame] | 3661 | // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. |
| 3662 | ExprResult SavedUpdate = Update; |
| 3663 | ExprResult UpdateVal; |
| 3664 | if (VarRef.get()->getType()->isOverloadableType() || |
| 3665 | NewStart.get()->getType()->isOverloadableType() || |
| 3666 | Update.get()->getType()->isOverloadableType()) { |
| 3667 | bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); |
| 3668 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); |
| 3669 | Update = |
| 3670 | SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); |
| 3671 | if (Update.isUsable()) { |
| 3672 | UpdateVal = |
| 3673 | SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, |
| 3674 | VarRef.get(), SavedUpdate.get()); |
| 3675 | if (UpdateVal.isUsable()) { |
| 3676 | Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), |
| 3677 | UpdateVal.get()); |
| 3678 | } |
| 3679 | } |
| 3680 | SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); |
| 3681 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3682 | |
Alexey Bataev | c0214e0 | 2016-02-16 12:13:49 +0000 | [diff] [blame] | 3683 | // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. |
| 3684 | if (!Update.isUsable() || !UpdateVal.isUsable()) { |
| 3685 | Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, |
| 3686 | NewStart.get(), SavedUpdate.get()); |
| 3687 | if (!Update.isUsable()) |
| 3688 | return ExprError(); |
| 3689 | |
Alexey Bataev | 11481f5 | 2016-02-17 10:29:05 +0000 | [diff] [blame] | 3690 | if (!SemaRef.Context.hasSameType(Update.get()->getType(), |
| 3691 | VarRef.get()->getType())) { |
| 3692 | Update = SemaRef.PerformImplicitConversion( |
| 3693 | Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); |
| 3694 | if (!Update.isUsable()) |
| 3695 | return ExprError(); |
| 3696 | } |
Alexey Bataev | c0214e0 | 2016-02-16 12:13:49 +0000 | [diff] [blame] | 3697 | |
| 3698 | Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); |
| 3699 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3700 | return Update; |
| 3701 | } |
| 3702 | |
| 3703 | /// \brief Convert integer expression \a E to make it have at least \a Bits |
| 3704 | /// bits. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3705 | static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3706 | if (E == nullptr) |
| 3707 | return ExprError(); |
| 3708 | auto &C = SemaRef.Context; |
| 3709 | QualType OldType = E->getType(); |
| 3710 | unsigned HasBits = C.getTypeSize(OldType); |
| 3711 | if (HasBits >= Bits) |
| 3712 | return ExprResult(E); |
| 3713 | // OK to convert to signed, because new type has more bits than old. |
| 3714 | QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); |
| 3715 | return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, |
| 3716 | true); |
| 3717 | } |
| 3718 | |
| 3719 | /// \brief Check if the given expression \a E is a constant integer that fits |
| 3720 | /// into \a Bits bits. |
| 3721 | static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) { |
| 3722 | if (E == nullptr) |
| 3723 | return false; |
| 3724 | llvm::APSInt Result; |
| 3725 | if (E->isIntegerConstantExpr(Result, SemaRef.Context)) |
| 3726 | return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); |
| 3727 | return false; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3730 | /// Build preinits statement for the given declarations. |
| 3731 | static Stmt *buildPreInits(ASTContext &Context, |
| 3732 | SmallVectorImpl<Decl *> &PreInits) { |
| 3733 | if (!PreInits.empty()) { |
| 3734 | return new (Context) DeclStmt( |
| 3735 | DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), |
| 3736 | SourceLocation(), SourceLocation()); |
| 3737 | } |
| 3738 | return nullptr; |
| 3739 | } |
| 3740 | |
| 3741 | /// Build preinits statement for the given declarations. |
| 3742 | static Stmt *buildPreInits(ASTContext &Context, |
| 3743 | llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { |
| 3744 | if (!Captures.empty()) { |
| 3745 | SmallVector<Decl *, 16> PreInits; |
| 3746 | for (auto &Pair : Captures) |
| 3747 | PreInits.push_back(Pair.second->getDecl()); |
| 3748 | return buildPreInits(Context, PreInits); |
| 3749 | } |
| 3750 | return nullptr; |
| 3751 | } |
| 3752 | |
| 3753 | /// Build postupdate expression for the given list of postupdates expressions. |
| 3754 | static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { |
| 3755 | Expr *PostUpdate = nullptr; |
| 3756 | if (!PostUpdates.empty()) { |
| 3757 | for (auto *E : PostUpdates) { |
| 3758 | Expr *ConvE = S.BuildCStyleCastExpr( |
| 3759 | E->getExprLoc(), |
| 3760 | S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), |
| 3761 | E->getExprLoc(), E) |
| 3762 | .get(); |
| 3763 | PostUpdate = PostUpdate |
| 3764 | ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, |
| 3765 | PostUpdate, ConvE) |
| 3766 | .get() |
| 3767 | : ConvE; |
| 3768 | } |
| 3769 | } |
| 3770 | return PostUpdate; |
| 3771 | } |
| 3772 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3773 | /// \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] | 3774 | /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, |
| 3775 | /// number of collapsed loops otherwise. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3776 | static unsigned |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3777 | CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, |
| 3778 | Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, |
| 3779 | DSAStackTy &DSA, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 3780 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3781 | OMPLoopDirective::HelperExprs &Built) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3782 | unsigned NestedLoopCount = 1; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3783 | if (CollapseLoopCountExpr) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3784 | // Found 'collapse' clause - calculate collapse number. |
| 3785 | llvm::APSInt Result; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3786 | if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) |
Alexey Bataev | 7b6bc88 | 2015-11-26 07:50:39 +0000 | [diff] [blame] | 3787 | NestedLoopCount = Result.getLimitedValue(); |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3788 | } |
| 3789 | if (OrderedLoopCountExpr) { |
| 3790 | // Found 'ordered' clause - calculate collapse number. |
| 3791 | llvm::APSInt Result; |
Alexey Bataev | 7b6bc88 | 2015-11-26 07:50:39 +0000 | [diff] [blame] | 3792 | if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { |
| 3793 | if (Result.getLimitedValue() < NestedLoopCount) { |
| 3794 | SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), |
| 3795 | diag::err_omp_wrong_ordered_loop_count) |
| 3796 | << OrderedLoopCountExpr->getSourceRange(); |
| 3797 | SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), |
| 3798 | diag::note_collapse_loop_count) |
| 3799 | << CollapseLoopCountExpr->getSourceRange(); |
| 3800 | } |
| 3801 | NestedLoopCount = Result.getLimitedValue(); |
| 3802 | } |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3803 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3804 | // This is helper routine for loop directives (e.g., 'for', 'simd', |
| 3805 | // 'for simd', etc.). |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3806 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3807 | SmallVector<LoopIterationSpace, 4> IterSpaces; |
| 3808 | IterSpaces.resize(NestedLoopCount); |
| 3809 | Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3810 | for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 3811 | if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3812 | NestedLoopCount, CollapseLoopCountExpr, |
| 3813 | OrderedLoopCountExpr, VarsWithImplicitDSA, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3814 | IterSpaces[Cnt], Captures)) |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 3815 | return 0; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3816 | // 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] | 3817 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 3818 | // All loops associated with the construct must be perfectly nested; that |
| 3819 | // is, there must be no intervening code nor any OpenMP directive between |
| 3820 | // any two loops. |
| 3821 | CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3824 | Built.clear(/* size */ NestedLoopCount); |
| 3825 | |
| 3826 | if (SemaRef.CurContext->isDependentContext()) |
| 3827 | return NestedLoopCount; |
| 3828 | |
| 3829 | // An example of what is generated for the following code: |
| 3830 | // |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3831 | // #pragma omp simd collapse(2) ordered(2) |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3832 | // for (i = 0; i < NI; ++i) |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 3833 | // for (k = 0; k < NK; ++k) |
| 3834 | // for (j = J0; j < NJ; j+=2) { |
| 3835 | // <loop body> |
| 3836 | // } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3837 | // |
| 3838 | // We generate the code below. |
| 3839 | // Note: the loop body may be outlined in CodeGen. |
| 3840 | // Note: some counters may be C++ classes, operator- is used to find number of |
| 3841 | // iterations and operator+= to calculate counter value. |
| 3842 | // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 |
| 3843 | // or i64 is currently supported). |
| 3844 | // |
| 3845 | // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) |
| 3846 | // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { |
| 3847 | // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); |
| 3848 | // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; |
| 3849 | // // similar updates for vars in clauses (e.g. 'linear') |
| 3850 | // <loop body (using local i and j)> |
| 3851 | // } |
| 3852 | // i = NI; // assign final values of counters |
| 3853 | // j = NJ; |
| 3854 | // |
| 3855 | |
| 3856 | // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are |
| 3857 | // the iteration counts of the collapsed for loops. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3858 | // Precondition tests if there is at least one iteration (all conditions are |
| 3859 | // true). |
| 3860 | auto PreCond = ExprResult(IterSpaces[0].PreCond); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3861 | auto N0 = IterSpaces[0].NumIterations; |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3862 | ExprResult LastIteration32 = WidenIterationCount( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3863 | 32 /* Bits */, SemaRef |
| 3864 | .PerformImplicitConversion( |
| 3865 | N0->IgnoreImpCasts(), N0->getType(), |
| 3866 | Sema::AA_Converting, /*AllowExplicit=*/true) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3867 | .get(), |
| 3868 | SemaRef); |
| 3869 | ExprResult LastIteration64 = WidenIterationCount( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3870 | 64 /* Bits */, SemaRef |
| 3871 | .PerformImplicitConversion( |
| 3872 | N0->IgnoreImpCasts(), N0->getType(), |
| 3873 | Sema::AA_Converting, /*AllowExplicit=*/true) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3874 | .get(), |
| 3875 | SemaRef); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3876 | |
| 3877 | if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) |
| 3878 | return NestedLoopCount; |
| 3879 | |
| 3880 | auto &C = SemaRef.Context; |
| 3881 | bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; |
| 3882 | |
| 3883 | Scope *CurScope = DSA.getCurScope(); |
| 3884 | for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3885 | if (PreCond.isUsable()) { |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 3886 | PreCond = |
| 3887 | SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, |
| 3888 | PreCond.get(), IterSpaces[Cnt].PreCond); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 3889 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3890 | auto N = IterSpaces[Cnt].NumIterations; |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 3891 | SourceLocation Loc = N->getExprLoc(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3892 | AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; |
| 3893 | if (LastIteration32.isUsable()) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3894 | LastIteration32 = SemaRef.BuildBinOp( |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 3895 | CurScope, Loc, BO_Mul, LastIteration32.get(), |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3896 | SemaRef |
| 3897 | .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), |
| 3898 | Sema::AA_Converting, |
| 3899 | /*AllowExplicit=*/true) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3900 | .get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3901 | if (LastIteration64.isUsable()) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3902 | LastIteration64 = SemaRef.BuildBinOp( |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 3903 | CurScope, Loc, BO_Mul, LastIteration64.get(), |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3904 | SemaRef |
| 3905 | .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), |
| 3906 | Sema::AA_Converting, |
| 3907 | /*AllowExplicit=*/true) |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 3908 | .get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | // Choose either the 32-bit or 64-bit version. |
| 3912 | ExprResult LastIteration = LastIteration64; |
| 3913 | if (LastIteration32.isUsable() && |
| 3914 | C.getTypeSize(LastIteration32.get()->getType()) == 32 && |
| 3915 | (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || |
| 3916 | FitsInto( |
| 3917 | 32 /* Bits */, |
| 3918 | LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), |
| 3919 | LastIteration64.get(), SemaRef))) |
| 3920 | LastIteration = LastIteration32; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3921 | QualType VType = LastIteration.get()->getType(); |
| 3922 | QualType RealVType = VType; |
| 3923 | QualType StrideVType = VType; |
| 3924 | if (isOpenMPTaskLoopDirective(DKind)) { |
| 3925 | VType = |
| 3926 | SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 3927 | StrideVType = |
| 3928 | SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 3929 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3930 | |
| 3931 | if (!LastIteration.isUsable()) |
| 3932 | return 0; |
| 3933 | |
| 3934 | // Save the number of iterations. |
| 3935 | ExprResult NumIterations = LastIteration; |
| 3936 | { |
| 3937 | LastIteration = SemaRef.BuildBinOp( |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 3938 | CurScope, LastIteration.get()->getExprLoc(), BO_Sub, |
| 3939 | LastIteration.get(), |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3940 | SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); |
| 3941 | if (!LastIteration.isUsable()) |
| 3942 | return 0; |
| 3943 | } |
| 3944 | |
| 3945 | // Calculate the last iteration number beforehand instead of doing this on |
| 3946 | // each iteration. Do not do this if the number of iterations may be kfold-ed. |
| 3947 | llvm::APSInt Result; |
| 3948 | bool IsConstant = |
| 3949 | LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); |
| 3950 | ExprResult CalcLastIteration; |
| 3951 | if (!IsConstant) { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3952 | ExprResult SaveRef = |
| 3953 | tryBuildCapture(SemaRef, LastIteration.get(), Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3954 | LastIteration = SaveRef; |
| 3955 | |
| 3956 | // Prepare SaveRef + 1. |
| 3957 | NumIterations = SemaRef.BuildBinOp( |
Alexey Bataev | a7206b9 | 2016-12-20 16:51:02 +0000 | [diff] [blame] | 3958 | CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 3959 | SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); |
| 3960 | if (!NumIterations.isUsable()) |
| 3961 | return 0; |
| 3962 | } |
| 3963 | |
| 3964 | SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); |
| 3965 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 3966 | // Build variables passed into runtime, necessary for worksharing directives. |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 3967 | ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB; |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3968 | if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || |
| 3969 | isOpenMPDistributeDirective(DKind)) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3970 | // Lower bound variable, initialized with zero. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 3971 | VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); |
| 3972 | LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3973 | SemaRef.AddInitializerToDecl( |
| 3974 | LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), |
| 3975 | /*DirectInit*/ false, /*TypeMayContainAuto*/ false); |
| 3976 | |
| 3977 | // Upper bound variable, initialized with last iteration number. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 3978 | VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); |
| 3979 | UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3980 | SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), |
| 3981 | /*DirectInit*/ false, |
| 3982 | /*TypeMayContainAuto*/ false); |
| 3983 | |
| 3984 | // A 32-bit variable-flag where runtime returns 1 for the last iteration. |
| 3985 | // This will be used to implement clause 'lastprivate'. |
| 3986 | QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 3987 | VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); |
| 3988 | IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3989 | SemaRef.AddInitializerToDecl( |
| 3990 | ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), |
| 3991 | /*DirectInit*/ false, /*TypeMayContainAuto*/ false); |
| 3992 | |
| 3993 | // Stride variable returned by runtime (we initialize it to 1 by default). |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3994 | VarDecl *STDecl = |
| 3995 | buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); |
| 3996 | ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3997 | SemaRef.AddInitializerToDecl( |
| 3998 | STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), |
| 3999 | /*DirectInit*/ false, /*TypeMayContainAuto*/ false); |
| 4000 | |
| 4001 | // Build expression: UB = min(UB, LastIteration) |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4002 | // It is necessary for CodeGen of directives with static scheduling. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4003 | ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, |
| 4004 | UB.get(), LastIteration.get()); |
| 4005 | ExprResult CondOp = SemaRef.ActOnConditionalOp( |
| 4006 | InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get()); |
| 4007 | EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), |
| 4008 | CondOp.get()); |
| 4009 | EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 4010 | |
| 4011 | // If we have a combined directive that combines 'distribute', 'for' or |
| 4012 | // 'simd' we need to be able to access the bounds of the schedule of the |
| 4013 | // enclosing region. E.g. in 'distribute parallel for' the bounds obtained |
| 4014 | // by scheduling 'distribute' have to be passed to the schedule of 'for'. |
| 4015 | if (isOpenMPLoopBoundSharingDirective(DKind)) { |
| 4016 | auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); |
| 4017 | |
| 4018 | // We expect to have at least 2 more parameters than the 'parallel' |
| 4019 | // directive does - the lower and upper bounds of the previous schedule. |
| 4020 | assert(CD->getNumParams() >= 4 && |
| 4021 | "Unexpected number of parameters in loop combined directive"); |
| 4022 | |
| 4023 | // Set the proper type for the bounds given what we learned from the |
| 4024 | // enclosed loops. |
| 4025 | auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2); |
| 4026 | auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3); |
| 4027 | |
| 4028 | // Previous lower and upper bounds are obtained from the region |
| 4029 | // parameters. |
| 4030 | PrevLB = |
| 4031 | buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); |
| 4032 | PrevUB = |
| 4033 | buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); |
| 4034 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | // Build the iteration variable and its initialization before loop. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4038 | ExprResult IV; |
| 4039 | ExprResult Init; |
| 4040 | { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4041 | VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); |
| 4042 | IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4043 | Expr *RHS = |
| 4044 | (isOpenMPWorksharingDirective(DKind) || |
| 4045 | isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) |
| 4046 | ? LB.get() |
| 4047 | : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4048 | Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); |
| 4049 | Init = SemaRef.ActOnFinishFullExpr(Init.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4052 | // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4053 | SourceLocation CondLoc; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4054 | ExprResult Cond = |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 4055 | (isOpenMPWorksharingDirective(DKind) || |
| 4056 | isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4057 | ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) |
| 4058 | : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), |
| 4059 | NumIterations.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4060 | |
| 4061 | // Loop increment (IV = IV + 1) |
| 4062 | SourceLocation IncLoc; |
| 4063 | ExprResult Inc = |
| 4064 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), |
| 4065 | SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); |
| 4066 | if (!Inc.isUsable()) |
| 4067 | return 0; |
| 4068 | Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4069 | Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); |
| 4070 | if (!Inc.isUsable()) |
| 4071 | return 0; |
| 4072 | |
| 4073 | // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). |
| 4074 | // Used for directives with static scheduling. |
| 4075 | ExprResult NextLB, NextUB; |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 4076 | if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || |
| 4077 | isOpenMPDistributeDirective(DKind)) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4078 | // LB + ST |
| 4079 | NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); |
| 4080 | if (!NextLB.isUsable()) |
| 4081 | return 0; |
| 4082 | // LB = LB + ST |
| 4083 | NextLB = |
| 4084 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); |
| 4085 | NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); |
| 4086 | if (!NextLB.isUsable()) |
| 4087 | return 0; |
| 4088 | // UB + ST |
| 4089 | NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); |
| 4090 | if (!NextUB.isUsable()) |
| 4091 | return 0; |
| 4092 | // UB = UB + ST |
| 4093 | NextUB = |
| 4094 | SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); |
| 4095 | NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); |
| 4096 | if (!NextUB.isUsable()) |
| 4097 | return 0; |
| 4098 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4099 | |
| 4100 | // Build updates and final values of the loop counters. |
| 4101 | bool HasErrors = false; |
| 4102 | Built.Counters.resize(NestedLoopCount); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4103 | Built.Inits.resize(NestedLoopCount); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4104 | Built.Updates.resize(NestedLoopCount); |
| 4105 | Built.Finals.resize(NestedLoopCount); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4106 | SmallVector<Expr *, 4> LoopMultipliers; |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4107 | { |
| 4108 | ExprResult Div; |
| 4109 | // Go from inner nested loop to outer. |
| 4110 | for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { |
| 4111 | LoopIterationSpace &IS = IterSpaces[Cnt]; |
| 4112 | SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); |
| 4113 | // Build: Iter = (IV / Div) % IS.NumIters |
| 4114 | // where Div is product of previous iterations' IS.NumIters. |
| 4115 | ExprResult Iter; |
| 4116 | if (Div.isUsable()) { |
| 4117 | Iter = |
| 4118 | SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); |
| 4119 | } else { |
| 4120 | Iter = IV; |
| 4121 | assert((Cnt == (int)NestedLoopCount - 1) && |
| 4122 | "unusable div expected on first iteration only"); |
| 4123 | } |
| 4124 | |
| 4125 | if (Cnt != 0 && Iter.isUsable()) |
| 4126 | Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), |
| 4127 | IS.NumIterations); |
| 4128 | if (!Iter.isUsable()) { |
| 4129 | HasErrors = true; |
| 4130 | break; |
| 4131 | } |
| 4132 | |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 4133 | // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4134 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); |
| 4135 | auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(), |
| 4136 | IS.CounterVar->getExprLoc(), |
| 4137 | /*RefersToCapture=*/true); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4138 | ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4139 | IS.CounterInit, Captures); |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4140 | if (!Init.isUsable()) { |
| 4141 | HasErrors = true; |
| 4142 | break; |
| 4143 | } |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4144 | ExprResult Update = BuildCounterUpdate( |
| 4145 | SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, |
| 4146 | IS.CounterStep, IS.Subtract, &Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4147 | if (!Update.isUsable()) { |
| 4148 | HasErrors = true; |
| 4149 | break; |
| 4150 | } |
| 4151 | |
| 4152 | // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step |
| 4153 | ExprResult Final = BuildCounterUpdate( |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 4154 | SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4155 | IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4156 | if (!Final.isUsable()) { |
| 4157 | HasErrors = true; |
| 4158 | break; |
| 4159 | } |
| 4160 | |
| 4161 | // Build Div for the next iteration: Div <- Div * IS.NumIters |
| 4162 | if (Cnt != 0) { |
| 4163 | if (Div.isUnset()) |
| 4164 | Div = IS.NumIterations; |
| 4165 | else |
| 4166 | Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), |
| 4167 | IS.NumIterations); |
| 4168 | |
| 4169 | // Add parentheses (for debugging purposes only). |
| 4170 | if (Div.isUsable()) |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4171 | Div = tryBuildCapture(SemaRef, Div.get(), Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4172 | if (!Div.isUsable()) { |
| 4173 | HasErrors = true; |
| 4174 | break; |
| 4175 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4176 | LoopMultipliers.push_back(Div.get()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4177 | } |
| 4178 | if (!Update.isUsable() || !Final.isUsable()) { |
| 4179 | HasErrors = true; |
| 4180 | break; |
| 4181 | } |
| 4182 | // Save results |
| 4183 | Built.Counters[Cnt] = IS.CounterVar; |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 4184 | Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 4185 | Built.Inits[Cnt] = Init.get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4186 | Built.Updates[Cnt] = Update.get(); |
| 4187 | Built.Finals[Cnt] = Final.get(); |
| 4188 | } |
| 4189 | } |
| 4190 | |
| 4191 | if (HasErrors) |
| 4192 | return 0; |
| 4193 | |
| 4194 | // Save results |
| 4195 | Built.IterationVarRef = IV.get(); |
| 4196 | Built.LastIteration = LastIteration.get(); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 4197 | Built.NumIterations = NumIterations.get(); |
Alexey Bataev | 3bed68c | 2015-07-15 12:14:07 +0000 | [diff] [blame] | 4198 | Built.CalcLastIteration = |
| 4199 | SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4200 | Built.PreCond = PreCond.get(); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 4201 | Built.PreInits = buildPreInits(C, Captures); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4202 | Built.Cond = Cond.get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4203 | Built.Init = Init.get(); |
| 4204 | Built.Inc = Inc.get(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4205 | Built.LB = LB.get(); |
| 4206 | Built.UB = UB.get(); |
| 4207 | Built.IL = IL.get(); |
| 4208 | Built.ST = ST.get(); |
| 4209 | Built.EUB = EUB.get(); |
| 4210 | Built.NLB = NextLB.get(); |
| 4211 | Built.NUB = NextUB.get(); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 4212 | Built.PrevLB = PrevLB.get(); |
| 4213 | Built.PrevUB = PrevUB.get(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4214 | |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4215 | Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get(); |
| 4216 | // Fill data for doacross depend clauses. |
| 4217 | for (auto Pair : DSA.getDoacrossDependClauses()) { |
| 4218 | if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) |
| 4219 | Pair.first->setCounterValue(CounterVal); |
| 4220 | else { |
| 4221 | if (NestedLoopCount != Pair.second.size() || |
| 4222 | NestedLoopCount != LoopMultipliers.size() + 1) { |
| 4223 | // Erroneous case - clause has some problems. |
| 4224 | Pair.first->setCounterValue(CounterVal); |
| 4225 | continue; |
| 4226 | } |
| 4227 | assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink); |
| 4228 | auto I = Pair.second.rbegin(); |
| 4229 | auto IS = IterSpaces.rbegin(); |
| 4230 | auto ILM = LoopMultipliers.rbegin(); |
| 4231 | Expr *UpCounterVal = CounterVal; |
| 4232 | Expr *Multiplier = nullptr; |
| 4233 | for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { |
| 4234 | if (I->first) { |
| 4235 | assert(IS->CounterStep); |
| 4236 | Expr *NormalizedOffset = |
| 4237 | SemaRef |
| 4238 | .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div, |
| 4239 | I->first, IS->CounterStep) |
| 4240 | .get(); |
| 4241 | if (Multiplier) { |
| 4242 | NormalizedOffset = |
| 4243 | SemaRef |
| 4244 | .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul, |
| 4245 | NormalizedOffset, Multiplier) |
| 4246 | .get(); |
| 4247 | } |
| 4248 | assert(I->second == OO_Plus || I->second == OO_Minus); |
| 4249 | BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4250 | UpCounterVal = SemaRef |
| 4251 | .BuildBinOp(CurScope, I->first->getExprLoc(), BOK, |
| 4252 | UpCounterVal, NormalizedOffset) |
| 4253 | .get(); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4254 | } |
| 4255 | Multiplier = *ILM; |
| 4256 | ++I; |
| 4257 | ++IS; |
| 4258 | ++ILM; |
| 4259 | } |
| 4260 | Pair.first->setCounterValue(UpCounterVal); |
| 4261 | } |
| 4262 | } |
| 4263 | |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 4264 | return NestedLoopCount; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4267 | static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 4268 | auto CollapseClauses = |
| 4269 | OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); |
| 4270 | if (CollapseClauses.begin() != CollapseClauses.end()) |
| 4271 | return (*CollapseClauses.begin())->getNumForLoops(); |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 4272 | return nullptr; |
| 4273 | } |
| 4274 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4275 | static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 4276 | auto OrderedClauses = |
| 4277 | OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); |
| 4278 | if (OrderedClauses.begin() != OrderedClauses.end()) |
| 4279 | return (*OrderedClauses.begin())->getNumForLoops(); |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4280 | return nullptr; |
| 4281 | } |
| 4282 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 4283 | static bool checkSimdlenSafelenSpecified(Sema &S, |
| 4284 | const ArrayRef<OMPClause *> Clauses) { |
| 4285 | OMPSafelenClause *Safelen = nullptr; |
| 4286 | OMPSimdlenClause *Simdlen = nullptr; |
| 4287 | |
| 4288 | for (auto *Clause : Clauses) { |
| 4289 | if (Clause->getClauseKind() == OMPC_safelen) |
| 4290 | Safelen = cast<OMPSafelenClause>(Clause); |
| 4291 | else if (Clause->getClauseKind() == OMPC_simdlen) |
| 4292 | Simdlen = cast<OMPSimdlenClause>(Clause); |
| 4293 | if (Safelen && Simdlen) |
| 4294 | break; |
| 4295 | } |
| 4296 | |
| 4297 | if (Simdlen && Safelen) { |
| 4298 | llvm::APSInt SimdlenRes, SafelenRes; |
| 4299 | auto SimdlenLength = Simdlen->getSimdlen(); |
| 4300 | auto SafelenLength = Safelen->getSafelen(); |
| 4301 | if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || |
| 4302 | SimdlenLength->isInstantiationDependent() || |
| 4303 | SimdlenLength->containsUnexpandedParameterPack()) |
| 4304 | return false; |
| 4305 | if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || |
| 4306 | SafelenLength->isInstantiationDependent() || |
| 4307 | SafelenLength->containsUnexpandedParameterPack()) |
| 4308 | return false; |
| 4309 | SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context); |
| 4310 | SafelenLength->EvaluateAsInt(SafelenRes, S.Context); |
| 4311 | // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] |
| 4312 | // If both simdlen and safelen clauses are specified, the value of the |
| 4313 | // simdlen parameter must be less than or equal to the value of the safelen |
| 4314 | // parameter. |
| 4315 | if (SimdlenRes > SafelenRes) { |
| 4316 | S.Diag(SimdlenLength->getExprLoc(), |
| 4317 | diag::err_omp_wrong_simdlen_safelen_values) |
| 4318 | << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); |
| 4319 | return true; |
| 4320 | } |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 4321 | } |
| 4322 | return false; |
| 4323 | } |
| 4324 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4325 | StmtResult Sema::ActOnOpenMPSimdDirective( |
| 4326 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4327 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4328 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4329 | if (!AStmt) |
| 4330 | return StmtError(); |
| 4331 | |
| 4332 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4333 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4334 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4335 | // define the nested loops number. |
| 4336 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 4337 | OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), |
| 4338 | AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 4339 | if (NestedLoopCount == 0) |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4340 | return StmtError(); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4341 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4342 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 4343 | "omp simd loop exprs were not built"); |
| 4344 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 4345 | if (!CurContext->isDependentContext()) { |
| 4346 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4347 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4348 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 4349 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4350 | B.NumIterations, *this, CurScope, |
| 4351 | DSAStack)) |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 4352 | return StmtError(); |
| 4353 | } |
| 4354 | } |
| 4355 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 4356 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 4357 | return StmtError(); |
| 4358 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4359 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4360 | return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
| 4361 | Clauses, AStmt, B); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4364 | StmtResult Sema::ActOnOpenMPForDirective( |
| 4365 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4366 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4367 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4368 | if (!AStmt) |
| 4369 | return StmtError(); |
| 4370 | |
| 4371 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4372 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4373 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4374 | // define the nested loops number. |
| 4375 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 4376 | OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), |
| 4377 | AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 4378 | if (NestedLoopCount == 0) |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 4379 | return StmtError(); |
| 4380 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4381 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 4382 | "omp for loop exprs were not built"); |
| 4383 | |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4384 | if (!CurContext->isDependentContext()) { |
| 4385 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4386 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4387 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4388 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4389 | B.NumIterations, *this, CurScope, |
| 4390 | DSAStack)) |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4391 | return StmtError(); |
| 4392 | } |
| 4393 | } |
| 4394 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 4395 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4396 | return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4397 | Clauses, AStmt, B, DSAStack->isCancelRegion()); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 4398 | } |
| 4399 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4400 | StmtResult Sema::ActOnOpenMPForSimdDirective( |
| 4401 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4402 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4403 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4404 | if (!AStmt) |
| 4405 | return StmtError(); |
| 4406 | |
| 4407 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4408 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4409 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4410 | // define the nested loops number. |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4411 | unsigned NestedLoopCount = |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4412 | CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), |
| 4413 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 4414 | VarsWithImplicitDSA, B); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4415 | if (NestedLoopCount == 0) |
| 4416 | return StmtError(); |
| 4417 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4418 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 4419 | "omp for simd loop exprs were not built"); |
| 4420 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 4421 | if (!CurContext->isDependentContext()) { |
| 4422 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4423 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4424 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 4425 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4426 | B.NumIterations, *this, CurScope, |
| 4427 | DSAStack)) |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 4428 | return StmtError(); |
| 4429 | } |
| 4430 | } |
| 4431 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 4432 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 4433 | return StmtError(); |
| 4434 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4435 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4436 | return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
| 4437 | Clauses, AStmt, B); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4440 | StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, |
| 4441 | Stmt *AStmt, |
| 4442 | SourceLocation StartLoc, |
| 4443 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4444 | if (!AStmt) |
| 4445 | return StmtError(); |
| 4446 | |
| 4447 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4448 | auto BaseStmt = AStmt; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4449 | while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4450 | BaseStmt = CS->getCapturedStmt(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4451 | if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4452 | auto S = C->children(); |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4453 | if (S.begin() == S.end()) |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4454 | return StmtError(); |
| 4455 | // All associated statements must be '#pragma omp section' except for |
| 4456 | // the first one. |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4457 | for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4458 | if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { |
| 4459 | if (SectionStmt) |
| 4460 | Diag(SectionStmt->getLocStart(), |
| 4461 | diag::err_omp_sections_substmt_not_section); |
| 4462 | return StmtError(); |
| 4463 | } |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4464 | cast<OMPSectionDirective>(SectionStmt) |
| 4465 | ->setHasCancel(DSAStack->isCancelRegion()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4466 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4467 | } else { |
| 4468 | Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); |
| 4469 | return StmtError(); |
| 4470 | } |
| 4471 | |
| 4472 | getCurFunction()->setHasBranchProtectedScope(); |
| 4473 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4474 | return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, |
| 4475 | DSAStack->isCancelRegion()); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4478 | StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, |
| 4479 | SourceLocation StartLoc, |
| 4480 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4481 | if (!AStmt) |
| 4482 | return StmtError(); |
| 4483 | |
| 4484 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4485 | |
| 4486 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4487 | DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4488 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4489 | return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, |
| 4490 | DSAStack->isCancelRegion()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 4493 | StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, |
| 4494 | Stmt *AStmt, |
| 4495 | SourceLocation StartLoc, |
| 4496 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4497 | if (!AStmt) |
| 4498 | return StmtError(); |
| 4499 | |
| 4500 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | 74a05c9 | 2014-07-15 02:55:09 +0000 | [diff] [blame] | 4501 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 4502 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | 74a05c9 | 2014-07-15 02:55:09 +0000 | [diff] [blame] | 4503 | |
Alexey Bataev | 3255bf3 | 2015-01-19 05:20:46 +0000 | [diff] [blame] | 4504 | // OpenMP [2.7.3, single Construct, Restrictions] |
| 4505 | // The copyprivate clause must not be used with the nowait clause. |
| 4506 | OMPClause *Nowait = nullptr; |
| 4507 | OMPClause *Copyprivate = nullptr; |
| 4508 | for (auto *Clause : Clauses) { |
| 4509 | if (Clause->getClauseKind() == OMPC_nowait) |
| 4510 | Nowait = Clause; |
| 4511 | else if (Clause->getClauseKind() == OMPC_copyprivate) |
| 4512 | Copyprivate = Clause; |
| 4513 | if (Copyprivate && Nowait) { |
| 4514 | Diag(Copyprivate->getLocStart(), |
| 4515 | diag::err_omp_single_copyprivate_with_nowait); |
| 4516 | Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); |
| 4517 | return StmtError(); |
| 4518 | } |
| 4519 | } |
| 4520 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 4521 | return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 4522 | } |
| 4523 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 4524 | StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, |
| 4525 | SourceLocation StartLoc, |
| 4526 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4527 | if (!AStmt) |
| 4528 | return StmtError(); |
| 4529 | |
| 4530 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 4531 | |
| 4532 | getCurFunction()->setHasBranchProtectedScope(); |
| 4533 | |
| 4534 | return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 4535 | } |
| 4536 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 4537 | StmtResult Sema::ActOnOpenMPCriticalDirective( |
| 4538 | const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, |
| 4539 | Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4540 | if (!AStmt) |
| 4541 | return StmtError(); |
| 4542 | |
| 4543 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 4544 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 4545 | bool ErrorFound = false; |
| 4546 | llvm::APSInt Hint; |
| 4547 | SourceLocation HintLoc; |
| 4548 | bool DependentHint = false; |
| 4549 | for (auto *C : Clauses) { |
| 4550 | if (C->getClauseKind() == OMPC_hint) { |
| 4551 | if (!DirName.getName()) { |
| 4552 | Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name); |
| 4553 | ErrorFound = true; |
| 4554 | } |
| 4555 | Expr *E = cast<OMPHintClause>(C)->getHint(); |
| 4556 | if (E->isTypeDependent() || E->isValueDependent() || |
| 4557 | E->isInstantiationDependent()) |
| 4558 | DependentHint = true; |
| 4559 | else { |
| 4560 | Hint = E->EvaluateKnownConstInt(Context); |
| 4561 | HintLoc = C->getLocStart(); |
| 4562 | } |
| 4563 | } |
| 4564 | } |
| 4565 | if (ErrorFound) |
| 4566 | return StmtError(); |
| 4567 | auto Pair = DSAStack->getCriticalWithHint(DirName); |
| 4568 | if (Pair.first && DirName.getName() && !DependentHint) { |
| 4569 | if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { |
| 4570 | Diag(StartLoc, diag::err_omp_critical_with_hint); |
| 4571 | if (HintLoc.isValid()) { |
| 4572 | Diag(HintLoc, diag::note_omp_critical_hint_here) |
| 4573 | << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); |
| 4574 | } else |
| 4575 | Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; |
| 4576 | if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) { |
| 4577 | Diag(C->getLocStart(), diag::note_omp_critical_hint_here) |
| 4578 | << 1 |
| 4579 | << C->getHint()->EvaluateKnownConstInt(Context).toString( |
| 4580 | /*Radix=*/10, /*Signed=*/false); |
| 4581 | } else |
| 4582 | Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1; |
| 4583 | } |
| 4584 | } |
| 4585 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 4586 | getCurFunction()->setHasBranchProtectedScope(); |
| 4587 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 4588 | auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, |
| 4589 | Clauses, AStmt); |
| 4590 | if (!Pair.first && DirName.getName() && !DependentHint) |
| 4591 | DSAStack->addCriticalWithHint(Dir, Hint); |
| 4592 | return Dir; |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4595 | StmtResult Sema::ActOnOpenMPParallelForDirective( |
| 4596 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4597 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4598 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4599 | if (!AStmt) |
| 4600 | return StmtError(); |
| 4601 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4602 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 4603 | // 1.2.2 OpenMP Language Terminology |
| 4604 | // Structured block - An executable statement with a single entry at the |
| 4605 | // top and a single exit at the bottom. |
| 4606 | // The point of exit cannot be a branch out of the structured block. |
| 4607 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 4608 | CS->getCapturedDecl()->setNothrow(); |
| 4609 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4610 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4611 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4612 | // define the nested loops number. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4613 | unsigned NestedLoopCount = |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4614 | CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), |
| 4615 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 4616 | VarsWithImplicitDSA, B); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4617 | if (NestedLoopCount == 0) |
| 4618 | return StmtError(); |
| 4619 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4620 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 4621 | "omp parallel for loop exprs were not built"); |
| 4622 | |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4623 | if (!CurContext->isDependentContext()) { |
| 4624 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4625 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4626 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4627 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4628 | B.NumIterations, *this, CurScope, |
| 4629 | DSAStack)) |
Alexey Bataev | 54acd40 | 2015-08-04 11:18:19 +0000 | [diff] [blame] | 4630 | return StmtError(); |
| 4631 | } |
| 4632 | } |
| 4633 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4634 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4635 | return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4636 | NestedLoopCount, Clauses, AStmt, B, |
| 4637 | DSAStack->isCancelRegion()); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4640 | StmtResult Sema::ActOnOpenMPParallelForSimdDirective( |
| 4641 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 4642 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 4643 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4644 | if (!AStmt) |
| 4645 | return StmtError(); |
| 4646 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4647 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 4648 | // 1.2.2 OpenMP Language Terminology |
| 4649 | // Structured block - An executable statement with a single entry at the |
| 4650 | // top and a single exit at the bottom. |
| 4651 | // The point of exit cannot be a branch out of the structured block. |
| 4652 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 4653 | CS->getCapturedDecl()->setNothrow(); |
| 4654 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4655 | OMPLoopDirective::HelperExprs B; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4656 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 4657 | // define the nested loops number. |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4658 | unsigned NestedLoopCount = |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 4659 | CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), |
| 4660 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 4661 | VarsWithImplicitDSA, B); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4662 | if (NestedLoopCount == 0) |
| 4663 | return StmtError(); |
| 4664 | |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 4665 | if (!CurContext->isDependentContext()) { |
| 4666 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 4667 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4668 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 4669 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 4670 | B.NumIterations, *this, CurScope, |
| 4671 | DSAStack)) |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 4672 | return StmtError(); |
| 4673 | } |
| 4674 | } |
| 4675 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 4676 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 4677 | return StmtError(); |
| 4678 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4679 | getCurFunction()->setHasBranchProtectedScope(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 4680 | return OMPParallelForSimdDirective::Create( |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 4681 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4682 | } |
| 4683 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4684 | StmtResult |
| 4685 | Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, |
| 4686 | Stmt *AStmt, SourceLocation StartLoc, |
| 4687 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4688 | if (!AStmt) |
| 4689 | return StmtError(); |
| 4690 | |
| 4691 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4692 | auto BaseStmt = AStmt; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4693 | while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4694 | BaseStmt = CS->getCapturedStmt(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4695 | if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4696 | auto S = C->children(); |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4697 | if (S.begin() == S.end()) |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4698 | return StmtError(); |
| 4699 | // All associated statements must be '#pragma omp section' except for |
| 4700 | // the first one. |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4701 | for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4702 | if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { |
| 4703 | if (SectionStmt) |
| 4704 | Diag(SectionStmt->getLocStart(), |
| 4705 | diag::err_omp_parallel_sections_substmt_not_section); |
| 4706 | return StmtError(); |
| 4707 | } |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4708 | cast<OMPSectionDirective>(SectionStmt) |
| 4709 | ->setHasCancel(DSAStack->isCancelRegion()); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4710 | } |
| 4711 | } else { |
| 4712 | Diag(AStmt->getLocStart(), |
| 4713 | diag::err_omp_parallel_sections_not_compound_stmt); |
| 4714 | return StmtError(); |
| 4715 | } |
| 4716 | |
| 4717 | getCurFunction()->setHasBranchProtectedScope(); |
| 4718 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4719 | return OMPParallelSectionsDirective::Create( |
| 4720 | Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 4723 | StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, |
| 4724 | Stmt *AStmt, SourceLocation StartLoc, |
| 4725 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4726 | if (!AStmt) |
| 4727 | return StmtError(); |
| 4728 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 4729 | auto *CS = cast<CapturedStmt>(AStmt); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 4730 | // 1.2.2 OpenMP Language Terminology |
| 4731 | // Structured block - An executable statement with a single entry at the |
| 4732 | // top and a single exit at the bottom. |
| 4733 | // The point of exit cannot be a branch out of the structured block. |
| 4734 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 4735 | CS->getCapturedDecl()->setNothrow(); |
| 4736 | |
| 4737 | getCurFunction()->setHasBranchProtectedScope(); |
| 4738 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4739 | return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, |
| 4740 | DSAStack->isCancelRegion()); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 4743 | StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, |
| 4744 | SourceLocation EndLoc) { |
| 4745 | return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); |
| 4746 | } |
| 4747 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 4748 | StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, |
| 4749 | SourceLocation EndLoc) { |
| 4750 | return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); |
| 4751 | } |
| 4752 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 4753 | StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, |
| 4754 | SourceLocation EndLoc) { |
| 4755 | return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); |
| 4756 | } |
| 4757 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 4758 | StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt, |
| 4759 | SourceLocation StartLoc, |
| 4760 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 4761 | if (!AStmt) |
| 4762 | return StmtError(); |
| 4763 | |
| 4764 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 4765 | |
| 4766 | getCurFunction()->setHasBranchProtectedScope(); |
| 4767 | |
| 4768 | return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 4769 | } |
| 4770 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 4771 | StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, |
| 4772 | SourceLocation StartLoc, |
| 4773 | SourceLocation EndLoc) { |
| 4774 | assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); |
| 4775 | return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); |
| 4776 | } |
| 4777 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 4778 | StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, |
| 4779 | Stmt *AStmt, |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 4780 | SourceLocation StartLoc, |
| 4781 | SourceLocation EndLoc) { |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 4782 | OMPClause *DependFound = nullptr; |
| 4783 | OMPClause *DependSourceClause = nullptr; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 4784 | OMPClause *DependSinkClause = nullptr; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 4785 | bool ErrorFound = false; |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 4786 | OMPThreadsClause *TC = nullptr; |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 4787 | OMPSIMDClause *SC = nullptr; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 4788 | for (auto *C : Clauses) { |
| 4789 | if (auto *DC = dyn_cast<OMPDependClause>(C)) { |
| 4790 | DependFound = C; |
| 4791 | if (DC->getDependencyKind() == OMPC_DEPEND_source) { |
| 4792 | if (DependSourceClause) { |
| 4793 | Diag(C->getLocStart(), diag::err_omp_more_one_clause) |
| 4794 | << getOpenMPDirectiveName(OMPD_ordered) |
| 4795 | << getOpenMPClauseName(OMPC_depend) << 2; |
| 4796 | ErrorFound = true; |
| 4797 | } else |
| 4798 | DependSourceClause = C; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 4799 | if (DependSinkClause) { |
| 4800 | Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) |
| 4801 | << 0; |
| 4802 | ErrorFound = true; |
| 4803 | } |
| 4804 | } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { |
| 4805 | if (DependSourceClause) { |
| 4806 | Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) |
| 4807 | << 1; |
| 4808 | ErrorFound = true; |
| 4809 | } |
| 4810 | DependSinkClause = C; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 4811 | } |
| 4812 | } else if (C->getClauseKind() == OMPC_threads) |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 4813 | TC = cast<OMPThreadsClause>(C); |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 4814 | else if (C->getClauseKind() == OMPC_simd) |
| 4815 | SC = cast<OMPSIMDClause>(C); |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 4816 | } |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 4817 | if (!ErrorFound && !SC && |
| 4818 | isOpenMPSimdDirective(DSAStack->getParentDirective())) { |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 4819 | // OpenMP [2.8.1,simd Construct, Restrictions] |
| 4820 | // An ordered construct with the simd clause is the only OpenMP construct |
| 4821 | // that can appear in the simd region. |
| 4822 | Diag(StartLoc, diag::err_omp_prohibited_region_simd); |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 4823 | ErrorFound = true; |
| 4824 | } else if (DependFound && (TC || SC)) { |
| 4825 | Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd) |
| 4826 | << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); |
| 4827 | ErrorFound = true; |
| 4828 | } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) { |
| 4829 | Diag(DependFound->getLocStart(), |
| 4830 | diag::err_omp_ordered_directive_without_param); |
| 4831 | ErrorFound = true; |
| 4832 | } else if (TC || Clauses.empty()) { |
| 4833 | if (auto *Param = DSAStack->getParentOrderedRegionParam()) { |
| 4834 | SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc; |
| 4835 | Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) |
| 4836 | << (TC != nullptr); |
| 4837 | Diag(Param->getLocStart(), diag::note_omp_ordered_param); |
| 4838 | ErrorFound = true; |
| 4839 | } |
| 4840 | } |
| 4841 | if ((!AStmt && !DependFound) || ErrorFound) |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 4842 | return StmtError(); |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 4843 | |
| 4844 | if (AStmt) { |
| 4845 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 4846 | |
| 4847 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 4848 | } |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 4849 | |
| 4850 | return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 4851 | } |
| 4852 | |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4853 | namespace { |
| 4854 | /// \brief Helper class for checking expression in 'omp atomic [update]' |
| 4855 | /// construct. |
| 4856 | class OpenMPAtomicUpdateChecker { |
| 4857 | /// \brief Error results for atomic update expressions. |
| 4858 | enum ExprAnalysisErrorCode { |
| 4859 | /// \brief A statement is not an expression statement. |
| 4860 | NotAnExpression, |
| 4861 | /// \brief Expression is not builtin binary or unary operation. |
| 4862 | NotABinaryOrUnaryExpression, |
| 4863 | /// \brief Unary operation is not post-/pre- increment/decrement operation. |
| 4864 | NotAnUnaryIncDecExpression, |
| 4865 | /// \brief An expression is not of scalar type. |
| 4866 | NotAScalarType, |
| 4867 | /// \brief A binary operation is not an assignment operation. |
| 4868 | NotAnAssignmentOp, |
| 4869 | /// \brief RHS part of the binary operation is not a binary expression. |
| 4870 | NotABinaryExpression, |
| 4871 | /// \brief RHS part is not additive/multiplicative/shift/biwise binary |
| 4872 | /// expression. |
| 4873 | NotABinaryOperator, |
| 4874 | /// \brief RHS binary operation does not have reference to the updated LHS |
| 4875 | /// part. |
| 4876 | NotAnUpdateExpression, |
| 4877 | /// \brief No errors is found. |
| 4878 | NoError |
| 4879 | }; |
| 4880 | /// \brief Reference to Sema. |
| 4881 | Sema &SemaRef; |
| 4882 | /// \brief A location for note diagnostics (when error is found). |
| 4883 | SourceLocation NoteLoc; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4884 | /// \brief 'x' lvalue part of the source atomic expression. |
| 4885 | Expr *X; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4886 | /// \brief 'expr' rvalue part of the source atomic expression. |
| 4887 | Expr *E; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4888 | /// \brief Helper expression of the form |
| 4889 | /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or |
| 4890 | /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. |
| 4891 | Expr *UpdateExpr; |
| 4892 | /// \brief Is 'x' a LHS in a RHS part of full update expression. It is |
| 4893 | /// important for non-associative operations. |
| 4894 | bool IsXLHSInRHSPart; |
| 4895 | BinaryOperatorKind Op; |
| 4896 | SourceLocation OpLoc; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4897 | /// \brief true if the source expression is a postfix unary operation, false |
| 4898 | /// if it is a prefix unary operation. |
| 4899 | bool IsPostfixUpdate; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4900 | |
| 4901 | public: |
| 4902 | OpenMPAtomicUpdateChecker(Sema &SemaRef) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4903 | : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4904 | IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4905 | /// \brief Check specified statement that it is suitable for 'atomic update' |
| 4906 | /// constructs and extract 'x', 'expr' and Operation from the original |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4907 | /// expression. If DiagId and NoteId == 0, then only check is performed |
| 4908 | /// without error notification. |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4909 | /// \param DiagId Diagnostic which should be emitted if error is found. |
| 4910 | /// \param NoteId Diagnostic note for the main error message. |
| 4911 | /// \return true if statement is not an update expression, false otherwise. |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4912 | bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4913 | /// \brief Return the 'x' lvalue part of the source atomic expression. |
| 4914 | Expr *getX() const { return X; } |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4915 | /// \brief Return the 'expr' rvalue part of the source atomic expression. |
| 4916 | Expr *getExpr() const { return E; } |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4917 | /// \brief Return the update expression used in calculation of the updated |
| 4918 | /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or |
| 4919 | /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. |
| 4920 | Expr *getUpdateExpr() const { return UpdateExpr; } |
| 4921 | /// \brief Return true if 'x' is LHS in RHS part of full update expression, |
| 4922 | /// false otherwise. |
| 4923 | bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } |
| 4924 | |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4925 | /// \brief true if the source expression is a postfix unary operation, false |
| 4926 | /// if it is a prefix unary operation. |
| 4927 | bool isPostfixUpdate() const { return IsPostfixUpdate; } |
| 4928 | |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4929 | private: |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4930 | bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, |
| 4931 | unsigned NoteId = 0); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4932 | }; |
| 4933 | } // namespace |
| 4934 | |
| 4935 | bool OpenMPAtomicUpdateChecker::checkBinaryOperation( |
| 4936 | BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { |
| 4937 | ExprAnalysisErrorCode ErrorFound = NoError; |
| 4938 | SourceLocation ErrorLoc, NoteLoc; |
| 4939 | SourceRange ErrorRange, NoteRange; |
| 4940 | // Allowed constructs are: |
| 4941 | // x = x binop expr; |
| 4942 | // x = expr binop x; |
| 4943 | if (AtomicBinOp->getOpcode() == BO_Assign) { |
| 4944 | X = AtomicBinOp->getLHS(); |
| 4945 | if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( |
| 4946 | AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { |
| 4947 | if (AtomicInnerBinOp->isMultiplicativeOp() || |
| 4948 | AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || |
| 4949 | AtomicInnerBinOp->isBitwiseOp()) { |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4950 | Op = AtomicInnerBinOp->getOpcode(); |
| 4951 | OpLoc = AtomicInnerBinOp->getOperatorLoc(); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4952 | auto *LHS = AtomicInnerBinOp->getLHS(); |
| 4953 | auto *RHS = AtomicInnerBinOp->getRHS(); |
| 4954 | llvm::FoldingSetNodeID XId, LHSId, RHSId; |
| 4955 | X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), |
| 4956 | /*Canonical=*/true); |
| 4957 | LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), |
| 4958 | /*Canonical=*/true); |
| 4959 | RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), |
| 4960 | /*Canonical=*/true); |
| 4961 | if (XId == LHSId) { |
| 4962 | E = RHS; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4963 | IsXLHSInRHSPart = true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4964 | } else if (XId == RHSId) { |
| 4965 | E = LHS; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4966 | IsXLHSInRHSPart = false; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4967 | } else { |
| 4968 | ErrorLoc = AtomicInnerBinOp->getExprLoc(); |
| 4969 | ErrorRange = AtomicInnerBinOp->getSourceRange(); |
| 4970 | NoteLoc = X->getExprLoc(); |
| 4971 | NoteRange = X->getSourceRange(); |
| 4972 | ErrorFound = NotAnUpdateExpression; |
| 4973 | } |
| 4974 | } else { |
| 4975 | ErrorLoc = AtomicInnerBinOp->getExprLoc(); |
| 4976 | ErrorRange = AtomicInnerBinOp->getSourceRange(); |
| 4977 | NoteLoc = AtomicInnerBinOp->getOperatorLoc(); |
| 4978 | NoteRange = SourceRange(NoteLoc, NoteLoc); |
| 4979 | ErrorFound = NotABinaryOperator; |
| 4980 | } |
| 4981 | } else { |
| 4982 | NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); |
| 4983 | NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); |
| 4984 | ErrorFound = NotABinaryExpression; |
| 4985 | } |
| 4986 | } else { |
| 4987 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 4988 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 4989 | NoteLoc = AtomicBinOp->getOperatorLoc(); |
| 4990 | NoteRange = SourceRange(NoteLoc, NoteLoc); |
| 4991 | ErrorFound = NotAnAssignmentOp; |
| 4992 | } |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 4993 | if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 4994 | SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; |
| 4995 | SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; |
| 4996 | return true; |
| 4997 | } else if (SemaRef.CurContext->isDependentContext()) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4998 | E = X = UpdateExpr = nullptr; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4999 | return ErrorFound != NoError; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, |
| 5003 | unsigned NoteId) { |
| 5004 | ExprAnalysisErrorCode ErrorFound = NoError; |
| 5005 | SourceLocation ErrorLoc, NoteLoc; |
| 5006 | SourceRange ErrorRange, NoteRange; |
| 5007 | // Allowed constructs are: |
| 5008 | // x++; |
| 5009 | // x--; |
| 5010 | // ++x; |
| 5011 | // --x; |
| 5012 | // x binop= expr; |
| 5013 | // x = x binop expr; |
| 5014 | // x = expr binop x; |
| 5015 | if (auto *AtomicBody = dyn_cast<Expr>(S)) { |
| 5016 | AtomicBody = AtomicBody->IgnoreParenImpCasts(); |
| 5017 | if (AtomicBody->getType()->isScalarType() || |
| 5018 | AtomicBody->isInstantiationDependent()) { |
| 5019 | if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( |
| 5020 | AtomicBody->IgnoreParenImpCasts())) { |
| 5021 | // Check for Compound Assignment Operation |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5022 | Op = BinaryOperator::getOpForCompoundAssignment( |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5023 | AtomicCompAssignOp->getOpcode()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5024 | OpLoc = AtomicCompAssignOp->getOperatorLoc(); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5025 | E = AtomicCompAssignOp->getRHS(); |
Kelvin Li | 4f161cf | 2016-07-20 19:41:17 +0000 | [diff] [blame] | 5026 | X = AtomicCompAssignOp->getLHS()->IgnoreParens(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5027 | IsXLHSInRHSPart = true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5028 | } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( |
| 5029 | AtomicBody->IgnoreParenImpCasts())) { |
| 5030 | // Check for Binary Operation |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5031 | if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5032 | return true; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5033 | } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( |
| 5034 | AtomicBody->IgnoreParenImpCasts())) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5035 | // Check for Unary Operation |
| 5036 | if (AtomicUnaryOp->isIncrementDecrementOp()) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5037 | IsPostfixUpdate = AtomicUnaryOp->isPostfix(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5038 | Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; |
| 5039 | OpLoc = AtomicUnaryOp->getOperatorLoc(); |
Kelvin Li | 4f161cf | 2016-07-20 19:41:17 +0000 | [diff] [blame] | 5040 | X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5041 | E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); |
| 5042 | IsXLHSInRHSPart = true; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5043 | } else { |
| 5044 | ErrorFound = NotAnUnaryIncDecExpression; |
| 5045 | ErrorLoc = AtomicUnaryOp->getExprLoc(); |
| 5046 | ErrorRange = AtomicUnaryOp->getSourceRange(); |
| 5047 | NoteLoc = AtomicUnaryOp->getOperatorLoc(); |
| 5048 | NoteRange = SourceRange(NoteLoc, NoteLoc); |
| 5049 | } |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5050 | } else if (!AtomicBody->isInstantiationDependent()) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5051 | ErrorFound = NotABinaryOrUnaryExpression; |
| 5052 | NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); |
| 5053 | NoteRange = ErrorRange = AtomicBody->getSourceRange(); |
| 5054 | } |
| 5055 | } else { |
| 5056 | ErrorFound = NotAScalarType; |
| 5057 | NoteLoc = ErrorLoc = AtomicBody->getLocStart(); |
| 5058 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
| 5059 | } |
| 5060 | } else { |
| 5061 | ErrorFound = NotAnExpression; |
| 5062 | NoteLoc = ErrorLoc = S->getLocStart(); |
| 5063 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
| 5064 | } |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5065 | if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5066 | SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; |
| 5067 | SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; |
| 5068 | return true; |
| 5069 | } else if (SemaRef.CurContext->isDependentContext()) |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5070 | E = X = UpdateExpr = nullptr; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 5071 | if (ErrorFound == NoError && E && X) { |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5072 | // Build an update expression of form 'OpaqueValueExpr(x) binop |
| 5073 | // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop |
| 5074 | // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. |
| 5075 | auto *OVEX = new (SemaRef.getASTContext()) |
| 5076 | OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); |
| 5077 | auto *OVEExpr = new (SemaRef.getASTContext()) |
| 5078 | OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); |
| 5079 | auto Update = |
| 5080 | SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, |
| 5081 | IsXLHSInRHSPart ? OVEExpr : OVEX); |
| 5082 | if (Update.isInvalid()) |
| 5083 | return true; |
| 5084 | Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), |
| 5085 | Sema::AA_Casting); |
| 5086 | if (Update.isInvalid()) |
| 5087 | return true; |
| 5088 | UpdateExpr = Update.get(); |
| 5089 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 5090 | return ErrorFound != NoError; |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5091 | } |
| 5092 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5093 | StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, |
| 5094 | Stmt *AStmt, |
| 5095 | SourceLocation StartLoc, |
| 5096 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 5097 | if (!AStmt) |
| 5098 | return StmtError(); |
| 5099 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5100 | auto *CS = cast<CapturedStmt>(AStmt); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5101 | // 1.2.2 OpenMP Language Terminology |
| 5102 | // Structured block - An executable statement with a single entry at the |
| 5103 | // top and a single exit at the bottom. |
| 5104 | // The point of exit cannot be a branch out of the structured block. |
| 5105 | // longjmp() and throw() must not violate the entry/exit criteria. |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5106 | OpenMPClauseKind AtomicKind = OMPC_unknown; |
| 5107 | SourceLocation AtomicKindLoc; |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 5108 | for (auto *C : Clauses) { |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 5109 | if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 5110 | C->getClauseKind() == OMPC_update || |
| 5111 | C->getClauseKind() == OMPC_capture) { |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5112 | if (AtomicKind != OMPC_unknown) { |
| 5113 | Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) |
| 5114 | << SourceRange(C->getLocStart(), C->getLocEnd()); |
| 5115 | Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) |
| 5116 | << getOpenMPClauseName(AtomicKind); |
| 5117 | } else { |
| 5118 | AtomicKind = C->getClauseKind(); |
| 5119 | AtomicKindLoc = C->getLocStart(); |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 5120 | } |
| 5121 | } |
| 5122 | } |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5123 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 5124 | auto Body = CS->getCapturedStmt(); |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 5125 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) |
| 5126 | Body = EWC->getSubExpr(); |
| 5127 | |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5128 | Expr *X = nullptr; |
| 5129 | Expr *V = nullptr; |
| 5130 | Expr *E = nullptr; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5131 | Expr *UE = nullptr; |
| 5132 | bool IsXLHSInRHSPart = false; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5133 | bool IsPostfixUpdate = false; |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5134 | // OpenMP [2.12.6, atomic Construct] |
| 5135 | // In the next expressions: |
| 5136 | // * x and v (as applicable) are both l-value expressions with scalar type. |
| 5137 | // * During the execution of an atomic region, multiple syntactic |
| 5138 | // occurrences of x must designate the same storage location. |
| 5139 | // * Neither of v and expr (as applicable) may access the storage location |
| 5140 | // designated by x. |
| 5141 | // * Neither of x and expr (as applicable) may access the storage location |
| 5142 | // designated by v. |
| 5143 | // * expr is an expression with scalar type. |
| 5144 | // * binop is one of +, *, -, /, &, ^, |, <<, or >>. |
| 5145 | // * binop, binop=, ++, and -- are not overloaded operators. |
| 5146 | // * The expression x binop expr must be numerically equivalent to x binop |
| 5147 | // (expr). This requirement is satisfied if the operators in expr have |
| 5148 | // precedence greater than binop, or by using parentheses around expr or |
| 5149 | // subexpressions of expr. |
| 5150 | // * The expression expr binop x must be numerically equivalent to (expr) |
| 5151 | // binop x. This requirement is satisfied if the operators in expr have |
| 5152 | // precedence equal to or greater than binop, or by using parentheses around |
| 5153 | // expr or subexpressions of expr. |
| 5154 | // * For forms that allow multiple occurrences of x, the number of times |
| 5155 | // that x is evaluated is unspecified. |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5156 | if (AtomicKind == OMPC_read) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5157 | enum { |
| 5158 | NotAnExpression, |
| 5159 | NotAnAssignmentOp, |
| 5160 | NotAScalarType, |
| 5161 | NotAnLValue, |
| 5162 | NoError |
| 5163 | } ErrorFound = NoError; |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5164 | SourceLocation ErrorLoc, NoteLoc; |
| 5165 | SourceRange ErrorRange, NoteRange; |
| 5166 | // If clause is read: |
| 5167 | // v = x; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5168 | if (auto *AtomicBody = dyn_cast<Expr>(Body)) { |
| 5169 | auto *AtomicBinOp = |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5170 | dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); |
| 5171 | if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { |
| 5172 | X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); |
| 5173 | V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); |
| 5174 | if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && |
| 5175 | (V->isInstantiationDependent() || V->getType()->isScalarType())) { |
| 5176 | if (!X->isLValue() || !V->isLValue()) { |
| 5177 | auto NotLValueExpr = X->isLValue() ? V : X; |
| 5178 | ErrorFound = NotAnLValue; |
| 5179 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 5180 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 5181 | NoteLoc = NotLValueExpr->getExprLoc(); |
| 5182 | NoteRange = NotLValueExpr->getSourceRange(); |
| 5183 | } |
| 5184 | } else if (!X->isInstantiationDependent() || |
| 5185 | !V->isInstantiationDependent()) { |
| 5186 | auto NotScalarExpr = |
| 5187 | (X->isInstantiationDependent() || X->getType()->isScalarType()) |
| 5188 | ? V |
| 5189 | : X; |
| 5190 | ErrorFound = NotAScalarType; |
| 5191 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 5192 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 5193 | NoteLoc = NotScalarExpr->getExprLoc(); |
| 5194 | NoteRange = NotScalarExpr->getSourceRange(); |
| 5195 | } |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5196 | } else if (!AtomicBody->isInstantiationDependent()) { |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5197 | ErrorFound = NotAnAssignmentOp; |
| 5198 | ErrorLoc = AtomicBody->getExprLoc(); |
| 5199 | ErrorRange = AtomicBody->getSourceRange(); |
| 5200 | NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() |
| 5201 | : AtomicBody->getExprLoc(); |
| 5202 | NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() |
| 5203 | : AtomicBody->getSourceRange(); |
| 5204 | } |
| 5205 | } else { |
| 5206 | ErrorFound = NotAnExpression; |
| 5207 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 5208 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5209 | } |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5210 | if (ErrorFound != NoError) { |
| 5211 | Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) |
| 5212 | << ErrorRange; |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5213 | Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound |
| 5214 | << NoteRange; |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5215 | return StmtError(); |
| 5216 | } else if (CurContext->isDependentContext()) |
| 5217 | V = X = nullptr; |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5218 | } else if (AtomicKind == OMPC_write) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5219 | enum { |
| 5220 | NotAnExpression, |
| 5221 | NotAnAssignmentOp, |
| 5222 | NotAScalarType, |
| 5223 | NotAnLValue, |
| 5224 | NoError |
| 5225 | } ErrorFound = NoError; |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5226 | SourceLocation ErrorLoc, NoteLoc; |
| 5227 | SourceRange ErrorRange, NoteRange; |
| 5228 | // If clause is write: |
| 5229 | // x = expr; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5230 | if (auto *AtomicBody = dyn_cast<Expr>(Body)) { |
| 5231 | auto *AtomicBinOp = |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5232 | dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); |
| 5233 | if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 5234 | X = AtomicBinOp->getLHS(); |
| 5235 | E = AtomicBinOp->getRHS(); |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5236 | if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && |
| 5237 | (E->isInstantiationDependent() || E->getType()->isScalarType())) { |
| 5238 | if (!X->isLValue()) { |
| 5239 | ErrorFound = NotAnLValue; |
| 5240 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 5241 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 5242 | NoteLoc = X->getExprLoc(); |
| 5243 | NoteRange = X->getSourceRange(); |
| 5244 | } |
| 5245 | } else if (!X->isInstantiationDependent() || |
| 5246 | !E->isInstantiationDependent()) { |
| 5247 | auto NotScalarExpr = |
| 5248 | (X->isInstantiationDependent() || X->getType()->isScalarType()) |
| 5249 | ? E |
| 5250 | : X; |
| 5251 | ErrorFound = NotAScalarType; |
| 5252 | ErrorLoc = AtomicBinOp->getExprLoc(); |
| 5253 | ErrorRange = AtomicBinOp->getSourceRange(); |
| 5254 | NoteLoc = NotScalarExpr->getExprLoc(); |
| 5255 | NoteRange = NotScalarExpr->getSourceRange(); |
| 5256 | } |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5257 | } else if (!AtomicBody->isInstantiationDependent()) { |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5258 | ErrorFound = NotAnAssignmentOp; |
| 5259 | ErrorLoc = AtomicBody->getExprLoc(); |
| 5260 | ErrorRange = AtomicBody->getSourceRange(); |
| 5261 | NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() |
| 5262 | : AtomicBody->getExprLoc(); |
| 5263 | NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() |
| 5264 | : AtomicBody->getSourceRange(); |
| 5265 | } |
| 5266 | } else { |
| 5267 | ErrorFound = NotAnExpression; |
| 5268 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 5269 | NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5270 | } |
Alexey Bataev | f33eba6 | 2014-11-28 07:21:40 +0000 | [diff] [blame] | 5271 | if (ErrorFound != NoError) { |
| 5272 | Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) |
| 5273 | << ErrorRange; |
| 5274 | Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound |
| 5275 | << NoteRange; |
| 5276 | return StmtError(); |
| 5277 | } else if (CurContext->isDependentContext()) |
| 5278 | E = X = nullptr; |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 5279 | } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5280 | // If clause is update: |
| 5281 | // x++; |
| 5282 | // x--; |
| 5283 | // ++x; |
| 5284 | // --x; |
| 5285 | // x binop= expr; |
| 5286 | // x = x binop expr; |
| 5287 | // x = expr binop x; |
| 5288 | OpenMPAtomicUpdateChecker Checker(*this); |
| 5289 | if (Checker.checkStatement( |
| 5290 | Body, (AtomicKind == OMPC_update) |
| 5291 | ? diag::err_omp_atomic_update_not_expression_statement |
| 5292 | : diag::err_omp_atomic_not_expression_statement, |
| 5293 | diag::note_omp_atomic_update)) |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 5294 | return StmtError(); |
Alexey Bataev | 1d160b1 | 2015-03-13 12:27:31 +0000 | [diff] [blame] | 5295 | if (!CurContext->isDependentContext()) { |
| 5296 | E = Checker.getExpr(); |
| 5297 | X = Checker.getX(); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 5298 | UE = Checker.getUpdateExpr(); |
| 5299 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 5300 | } |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 5301 | } else if (AtomicKind == OMPC_capture) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5302 | enum { |
| 5303 | NotAnAssignmentOp, |
| 5304 | NotACompoundStatement, |
| 5305 | NotTwoSubstatements, |
| 5306 | NotASpecificExpression, |
| 5307 | NoError |
| 5308 | } ErrorFound = NoError; |
| 5309 | SourceLocation ErrorLoc, NoteLoc; |
| 5310 | SourceRange ErrorRange, NoteRange; |
| 5311 | if (auto *AtomicBody = dyn_cast<Expr>(Body)) { |
| 5312 | // If clause is a capture: |
| 5313 | // v = x++; |
| 5314 | // v = x--; |
| 5315 | // v = ++x; |
| 5316 | // v = --x; |
| 5317 | // v = x binop= expr; |
| 5318 | // v = x = x binop expr; |
| 5319 | // v = x = expr binop x; |
| 5320 | auto *AtomicBinOp = |
| 5321 | dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); |
| 5322 | if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { |
| 5323 | V = AtomicBinOp->getLHS(); |
| 5324 | Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); |
| 5325 | OpenMPAtomicUpdateChecker Checker(*this); |
| 5326 | if (Checker.checkStatement( |
| 5327 | Body, diag::err_omp_atomic_capture_not_expression_statement, |
| 5328 | diag::note_omp_atomic_update)) |
| 5329 | return StmtError(); |
| 5330 | E = Checker.getExpr(); |
| 5331 | X = Checker.getX(); |
| 5332 | UE = Checker.getUpdateExpr(); |
| 5333 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
| 5334 | IsPostfixUpdate = Checker.isPostfixUpdate(); |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5335 | } else if (!AtomicBody->isInstantiationDependent()) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5336 | ErrorLoc = AtomicBody->getExprLoc(); |
| 5337 | ErrorRange = AtomicBody->getSourceRange(); |
| 5338 | NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() |
| 5339 | : AtomicBody->getExprLoc(); |
| 5340 | NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() |
| 5341 | : AtomicBody->getSourceRange(); |
| 5342 | ErrorFound = NotAnAssignmentOp; |
| 5343 | } |
| 5344 | if (ErrorFound != NoError) { |
| 5345 | Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) |
| 5346 | << ErrorRange; |
| 5347 | Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; |
| 5348 | return StmtError(); |
| 5349 | } else if (CurContext->isDependentContext()) { |
| 5350 | UE = V = E = X = nullptr; |
| 5351 | } |
| 5352 | } else { |
| 5353 | // If clause is a capture: |
| 5354 | // { v = x; x = expr; } |
| 5355 | // { v = x; x++; } |
| 5356 | // { v = x; x--; } |
| 5357 | // { v = x; ++x; } |
| 5358 | // { v = x; --x; } |
| 5359 | // { v = x; x binop= expr; } |
| 5360 | // { v = x; x = x binop expr; } |
| 5361 | // { v = x; x = expr binop x; } |
| 5362 | // { x++; v = x; } |
| 5363 | // { x--; v = x; } |
| 5364 | // { ++x; v = x; } |
| 5365 | // { --x; v = x; } |
| 5366 | // { x binop= expr; v = x; } |
| 5367 | // { x = x binop expr; v = x; } |
| 5368 | // { x = expr binop x; v = x; } |
| 5369 | if (auto *CS = dyn_cast<CompoundStmt>(Body)) { |
| 5370 | // Check that this is { expr1; expr2; } |
| 5371 | if (CS->size() == 2) { |
| 5372 | auto *First = CS->body_front(); |
| 5373 | auto *Second = CS->body_back(); |
| 5374 | if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) |
| 5375 | First = EWC->getSubExpr()->IgnoreParenImpCasts(); |
| 5376 | if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) |
| 5377 | Second = EWC->getSubExpr()->IgnoreParenImpCasts(); |
| 5378 | // Need to find what subexpression is 'v' and what is 'x'. |
| 5379 | OpenMPAtomicUpdateChecker Checker(*this); |
| 5380 | bool IsUpdateExprFound = !Checker.checkStatement(Second); |
| 5381 | BinaryOperator *BinOp = nullptr; |
| 5382 | if (IsUpdateExprFound) { |
| 5383 | BinOp = dyn_cast<BinaryOperator>(First); |
| 5384 | IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; |
| 5385 | } |
| 5386 | if (IsUpdateExprFound && !CurContext->isDependentContext()) { |
| 5387 | // { v = x; x++; } |
| 5388 | // { v = x; x--; } |
| 5389 | // { v = x; ++x; } |
| 5390 | // { v = x; --x; } |
| 5391 | // { v = x; x binop= expr; } |
| 5392 | // { v = x; x = x binop expr; } |
| 5393 | // { v = x; x = expr binop x; } |
| 5394 | // Check that the first expression has form v = x. |
| 5395 | auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); |
| 5396 | llvm::FoldingSetNodeID XId, PossibleXId; |
| 5397 | Checker.getX()->Profile(XId, Context, /*Canonical=*/true); |
| 5398 | PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); |
| 5399 | IsUpdateExprFound = XId == PossibleXId; |
| 5400 | if (IsUpdateExprFound) { |
| 5401 | V = BinOp->getLHS(); |
| 5402 | X = Checker.getX(); |
| 5403 | E = Checker.getExpr(); |
| 5404 | UE = Checker.getUpdateExpr(); |
| 5405 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 5406 | IsPostfixUpdate = true; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5407 | } |
| 5408 | } |
| 5409 | if (!IsUpdateExprFound) { |
| 5410 | IsUpdateExprFound = !Checker.checkStatement(First); |
| 5411 | BinOp = nullptr; |
| 5412 | if (IsUpdateExprFound) { |
| 5413 | BinOp = dyn_cast<BinaryOperator>(Second); |
| 5414 | IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; |
| 5415 | } |
| 5416 | if (IsUpdateExprFound && !CurContext->isDependentContext()) { |
| 5417 | // { x++; v = x; } |
| 5418 | // { x--; v = x; } |
| 5419 | // { ++x; v = x; } |
| 5420 | // { --x; v = x; } |
| 5421 | // { x binop= expr; v = x; } |
| 5422 | // { x = x binop expr; v = x; } |
| 5423 | // { x = expr binop x; v = x; } |
| 5424 | // Check that the second expression has form v = x. |
| 5425 | auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); |
| 5426 | llvm::FoldingSetNodeID XId, PossibleXId; |
| 5427 | Checker.getX()->Profile(XId, Context, /*Canonical=*/true); |
| 5428 | PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); |
| 5429 | IsUpdateExprFound = XId == PossibleXId; |
| 5430 | if (IsUpdateExprFound) { |
| 5431 | V = BinOp->getLHS(); |
| 5432 | X = Checker.getX(); |
| 5433 | E = Checker.getExpr(); |
| 5434 | UE = Checker.getUpdateExpr(); |
| 5435 | IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 5436 | IsPostfixUpdate = false; |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5437 | } |
| 5438 | } |
| 5439 | } |
| 5440 | if (!IsUpdateExprFound) { |
| 5441 | // { v = x; x = expr; } |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5442 | auto *FirstExpr = dyn_cast<Expr>(First); |
| 5443 | auto *SecondExpr = dyn_cast<Expr>(Second); |
| 5444 | if (!FirstExpr || !SecondExpr || |
| 5445 | !(FirstExpr->isInstantiationDependent() || |
| 5446 | SecondExpr->isInstantiationDependent())) { |
| 5447 | auto *FirstBinOp = dyn_cast<BinaryOperator>(First); |
| 5448 | if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5449 | ErrorFound = NotAnAssignmentOp; |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5450 | NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() |
| 5451 | : First->getLocStart(); |
| 5452 | NoteRange = ErrorRange = FirstBinOp |
| 5453 | ? FirstBinOp->getSourceRange() |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5454 | : SourceRange(ErrorLoc, ErrorLoc); |
| 5455 | } else { |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5456 | auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); |
| 5457 | if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { |
| 5458 | ErrorFound = NotAnAssignmentOp; |
| 5459 | NoteLoc = ErrorLoc = SecondBinOp |
| 5460 | ? SecondBinOp->getOperatorLoc() |
| 5461 | : Second->getLocStart(); |
| 5462 | NoteRange = ErrorRange = |
| 5463 | SecondBinOp ? SecondBinOp->getSourceRange() |
| 5464 | : SourceRange(ErrorLoc, ErrorLoc); |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5465 | } else { |
Alexey Bataev | 5a19547 | 2015-09-04 12:55:50 +0000 | [diff] [blame] | 5466 | auto *PossibleXRHSInFirst = |
| 5467 | FirstBinOp->getRHS()->IgnoreParenImpCasts(); |
| 5468 | auto *PossibleXLHSInSecond = |
| 5469 | SecondBinOp->getLHS()->IgnoreParenImpCasts(); |
| 5470 | llvm::FoldingSetNodeID X1Id, X2Id; |
| 5471 | PossibleXRHSInFirst->Profile(X1Id, Context, |
| 5472 | /*Canonical=*/true); |
| 5473 | PossibleXLHSInSecond->Profile(X2Id, Context, |
| 5474 | /*Canonical=*/true); |
| 5475 | IsUpdateExprFound = X1Id == X2Id; |
| 5476 | if (IsUpdateExprFound) { |
| 5477 | V = FirstBinOp->getLHS(); |
| 5478 | X = SecondBinOp->getLHS(); |
| 5479 | E = SecondBinOp->getRHS(); |
| 5480 | UE = nullptr; |
| 5481 | IsXLHSInRHSPart = false; |
| 5482 | IsPostfixUpdate = true; |
| 5483 | } else { |
| 5484 | ErrorFound = NotASpecificExpression; |
| 5485 | ErrorLoc = FirstBinOp->getExprLoc(); |
| 5486 | ErrorRange = FirstBinOp->getSourceRange(); |
| 5487 | NoteLoc = SecondBinOp->getLHS()->getExprLoc(); |
| 5488 | NoteRange = SecondBinOp->getRHS()->getSourceRange(); |
| 5489 | } |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5490 | } |
| 5491 | } |
| 5492 | } |
| 5493 | } |
| 5494 | } else { |
| 5495 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 5496 | NoteRange = ErrorRange = |
| 5497 | SourceRange(Body->getLocStart(), Body->getLocStart()); |
| 5498 | ErrorFound = NotTwoSubstatements; |
| 5499 | } |
| 5500 | } else { |
| 5501 | NoteLoc = ErrorLoc = Body->getLocStart(); |
| 5502 | NoteRange = ErrorRange = |
| 5503 | SourceRange(Body->getLocStart(), Body->getLocStart()); |
| 5504 | ErrorFound = NotACompoundStatement; |
| 5505 | } |
| 5506 | if (ErrorFound != NoError) { |
| 5507 | Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) |
| 5508 | << ErrorRange; |
| 5509 | Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; |
| 5510 | return StmtError(); |
| 5511 | } else if (CurContext->isDependentContext()) { |
| 5512 | UE = V = E = X = nullptr; |
| 5513 | } |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 5514 | } |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 5515 | } |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5516 | |
| 5517 | getCurFunction()->setHasBranchProtectedScope(); |
| 5518 | |
Alexey Bataev | 62cec44 | 2014-11-18 10:14:22 +0000 | [diff] [blame] | 5519 | return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, |
Alexey Bataev | b78ca83 | 2015-04-01 03:33:17 +0000 | [diff] [blame] | 5520 | X, V, E, UE, IsXLHSInRHSPart, |
| 5521 | IsPostfixUpdate); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5524 | StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, |
| 5525 | Stmt *AStmt, |
| 5526 | SourceLocation StartLoc, |
| 5527 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 5528 | if (!AStmt) |
| 5529 | return StmtError(); |
| 5530 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 5531 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5532 | // 1.2.2 OpenMP Language Terminology |
| 5533 | // Structured block - An executable statement with a single entry at the |
| 5534 | // top and a single exit at the bottom. |
| 5535 | // The point of exit cannot be a branch out of the structured block. |
| 5536 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5537 | CS->getCapturedDecl()->setNothrow(); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5538 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5539 | // OpenMP [2.16, Nesting of Regions] |
| 5540 | // If specified, a teams construct must be contained within a target |
| 5541 | // construct. That target construct must contain no statements or directives |
| 5542 | // outside of the teams construct. |
| 5543 | if (DSAStack->hasInnerTeamsRegion()) { |
| 5544 | auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true); |
| 5545 | bool OMPTeamsFound = true; |
| 5546 | if (auto *CS = dyn_cast<CompoundStmt>(S)) { |
| 5547 | auto I = CS->body_begin(); |
| 5548 | while (I != CS->body_end()) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5549 | auto *OED = dyn_cast<OMPExecutableDirective>(*I); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5550 | if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { |
| 5551 | OMPTeamsFound = false; |
| 5552 | break; |
| 5553 | } |
| 5554 | ++I; |
| 5555 | } |
| 5556 | assert(I != CS->body_end() && "Not found statement"); |
| 5557 | S = *I; |
Kelvin Li | 3834dce | 2016-06-27 19:15:43 +0000 | [diff] [blame] | 5558 | } else { |
| 5559 | auto *OED = dyn_cast<OMPExecutableDirective>(S); |
| 5560 | OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5561 | } |
| 5562 | if (!OMPTeamsFound) { |
| 5563 | Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); |
| 5564 | Diag(DSAStack->getInnerTeamsRegionLoc(), |
| 5565 | diag::note_omp_nested_teams_construct_here); |
| 5566 | Diag(S->getLocStart(), diag::note_omp_nested_statement_here) |
| 5567 | << isa<OMPExecutableDirective>(S); |
| 5568 | return StmtError(); |
| 5569 | } |
| 5570 | } |
| 5571 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5572 | getCurFunction()->setHasBranchProtectedScope(); |
| 5573 | |
| 5574 | return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 5575 | } |
| 5576 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 5577 | StmtResult |
| 5578 | Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, |
| 5579 | Stmt *AStmt, SourceLocation StartLoc, |
| 5580 | SourceLocation EndLoc) { |
| 5581 | if (!AStmt) |
| 5582 | return StmtError(); |
| 5583 | |
| 5584 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5585 | // 1.2.2 OpenMP Language Terminology |
| 5586 | // Structured block - An executable statement with a single entry at the |
| 5587 | // top and a single exit at the bottom. |
| 5588 | // The point of exit cannot be a branch out of the structured block. |
| 5589 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5590 | CS->getCapturedDecl()->setNothrow(); |
| 5591 | |
| 5592 | getCurFunction()->setHasBranchProtectedScope(); |
| 5593 | |
| 5594 | return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 5595 | AStmt); |
| 5596 | } |
| 5597 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5598 | StmtResult Sema::ActOnOpenMPTargetParallelForDirective( |
| 5599 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 5600 | SourceLocation EndLoc, |
| 5601 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 5602 | if (!AStmt) |
| 5603 | return StmtError(); |
| 5604 | |
| 5605 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5606 | // 1.2.2 OpenMP Language Terminology |
| 5607 | // Structured block - An executable statement with a single entry at the |
| 5608 | // top and a single exit at the bottom. |
| 5609 | // The point of exit cannot be a branch out of the structured block. |
| 5610 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5611 | CS->getCapturedDecl()->setNothrow(); |
| 5612 | |
| 5613 | OMPLoopDirective::HelperExprs B; |
| 5614 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 5615 | // define the nested loops number. |
| 5616 | unsigned NestedLoopCount = |
| 5617 | CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), |
| 5618 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 5619 | VarsWithImplicitDSA, B); |
| 5620 | if (NestedLoopCount == 0) |
| 5621 | return StmtError(); |
| 5622 | |
| 5623 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 5624 | "omp target parallel for loop exprs were not built"); |
| 5625 | |
| 5626 | if (!CurContext->isDependentContext()) { |
| 5627 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 5628 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5629 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5630 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 5631 | B.NumIterations, *this, CurScope, |
| 5632 | DSAStack)) |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5633 | return StmtError(); |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | getCurFunction()->setHasBranchProtectedScope(); |
| 5638 | return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, |
| 5639 | NestedLoopCount, Clauses, AStmt, |
| 5640 | B, DSAStack->isCancelRegion()); |
| 5641 | } |
| 5642 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5643 | /// \brief Check for existence of a map clause in the list of clauses. |
| 5644 | static bool HasMapClause(ArrayRef<OMPClause *> Clauses) { |
| 5645 | for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); |
| 5646 | I != E; ++I) { |
| 5647 | if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) { |
| 5648 | return true; |
| 5649 | } |
| 5650 | } |
| 5651 | |
| 5652 | return false; |
| 5653 | } |
| 5654 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5655 | StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, |
| 5656 | Stmt *AStmt, |
| 5657 | SourceLocation StartLoc, |
| 5658 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 5659 | if (!AStmt) |
| 5660 | return StmtError(); |
| 5661 | |
| 5662 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 5663 | |
Arpith Chacko Jacob | 46a04bb | 2016-01-21 19:57:55 +0000 | [diff] [blame] | 5664 | // OpenMP [2.10.1, Restrictions, p. 97] |
| 5665 | // At least one map clause must appear on the directive. |
| 5666 | if (!HasMapClause(Clauses)) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5667 | Diag(StartLoc, diag::err_omp_no_map_for_directive) |
| 5668 | << getOpenMPDirectiveName(OMPD_target_data); |
Arpith Chacko Jacob | 46a04bb | 2016-01-21 19:57:55 +0000 | [diff] [blame] | 5669 | return StmtError(); |
| 5670 | } |
| 5671 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5672 | getCurFunction()->setHasBranchProtectedScope(); |
| 5673 | |
| 5674 | return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 5675 | AStmt); |
| 5676 | } |
| 5677 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5678 | StmtResult |
| 5679 | Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, |
| 5680 | SourceLocation StartLoc, |
| 5681 | SourceLocation EndLoc) { |
| 5682 | // OpenMP [2.10.2, Restrictions, p. 99] |
| 5683 | // At least one map clause must appear on the directive. |
| 5684 | if (!HasMapClause(Clauses)) { |
| 5685 | Diag(StartLoc, diag::err_omp_no_map_for_directive) |
| 5686 | << getOpenMPDirectiveName(OMPD_target_enter_data); |
| 5687 | return StmtError(); |
| 5688 | } |
| 5689 | |
| 5690 | return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, |
| 5691 | Clauses); |
| 5692 | } |
| 5693 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 5694 | StmtResult |
| 5695 | Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, |
| 5696 | SourceLocation StartLoc, |
| 5697 | SourceLocation EndLoc) { |
| 5698 | // OpenMP [2.10.3, Restrictions, p. 102] |
| 5699 | // At least one map clause must appear on the directive. |
| 5700 | if (!HasMapClause(Clauses)) { |
| 5701 | Diag(StartLoc, diag::err_omp_no_map_for_directive) |
| 5702 | << getOpenMPDirectiveName(OMPD_target_exit_data); |
| 5703 | return StmtError(); |
| 5704 | } |
| 5705 | |
| 5706 | return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses); |
| 5707 | } |
| 5708 | |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5709 | StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, |
| 5710 | SourceLocation StartLoc, |
| 5711 | SourceLocation EndLoc) { |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5712 | bool seenMotionClause = false; |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 5713 | for (auto *C : Clauses) { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 5714 | if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from) |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 5715 | seenMotionClause = true; |
| 5716 | } |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5717 | if (!seenMotionClause) { |
| 5718 | Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); |
| 5719 | return StmtError(); |
| 5720 | } |
| 5721 | return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses); |
| 5722 | } |
| 5723 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5724 | StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, |
| 5725 | Stmt *AStmt, SourceLocation StartLoc, |
| 5726 | SourceLocation EndLoc) { |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 5727 | if (!AStmt) |
| 5728 | return StmtError(); |
| 5729 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5730 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5731 | // 1.2.2 OpenMP Language Terminology |
| 5732 | // Structured block - An executable statement with a single entry at the |
| 5733 | // top and a single exit at the bottom. |
| 5734 | // The point of exit cannot be a branch out of the structured block. |
| 5735 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5736 | CS->getCapturedDecl()->setNothrow(); |
| 5737 | |
| 5738 | getCurFunction()->setHasBranchProtectedScope(); |
| 5739 | |
| 5740 | return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 5741 | } |
| 5742 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 5743 | StmtResult |
| 5744 | Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, |
| 5745 | SourceLocation EndLoc, |
| 5746 | OpenMPDirectiveKind CancelRegion) { |
| 5747 | if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for && |
| 5748 | CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) { |
| 5749 | Diag(StartLoc, diag::err_omp_wrong_cancel_region) |
| 5750 | << getOpenMPDirectiveName(CancelRegion); |
| 5751 | return StmtError(); |
| 5752 | } |
| 5753 | if (DSAStack->isParentNowaitRegion()) { |
| 5754 | Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; |
| 5755 | return StmtError(); |
| 5756 | } |
| 5757 | if (DSAStack->isParentOrderedRegion()) { |
| 5758 | Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; |
| 5759 | return StmtError(); |
| 5760 | } |
| 5761 | return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, |
| 5762 | CancelRegion); |
| 5763 | } |
| 5764 | |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 5765 | StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, |
| 5766 | SourceLocation StartLoc, |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 5767 | SourceLocation EndLoc, |
| 5768 | OpenMPDirectiveKind CancelRegion) { |
| 5769 | if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for && |
| 5770 | CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) { |
| 5771 | Diag(StartLoc, diag::err_omp_wrong_cancel_region) |
| 5772 | << getOpenMPDirectiveName(CancelRegion); |
| 5773 | return StmtError(); |
| 5774 | } |
| 5775 | if (DSAStack->isParentNowaitRegion()) { |
| 5776 | Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; |
| 5777 | return StmtError(); |
| 5778 | } |
| 5779 | if (DSAStack->isParentOrderedRegion()) { |
| 5780 | Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; |
| 5781 | return StmtError(); |
| 5782 | } |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 5783 | DSAStack->setParentCancelRegion(/*Cancel=*/true); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 5784 | return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 5785 | CancelRegion); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 5786 | } |
| 5787 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 5788 | static bool checkGrainsizeNumTasksClauses(Sema &S, |
| 5789 | ArrayRef<OMPClause *> Clauses) { |
| 5790 | OMPClause *PrevClause = nullptr; |
| 5791 | bool ErrorFound = false; |
| 5792 | for (auto *C : Clauses) { |
| 5793 | if (C->getClauseKind() == OMPC_grainsize || |
| 5794 | C->getClauseKind() == OMPC_num_tasks) { |
| 5795 | if (!PrevClause) |
| 5796 | PrevClause = C; |
| 5797 | else if (PrevClause->getClauseKind() != C->getClauseKind()) { |
| 5798 | S.Diag(C->getLocStart(), |
| 5799 | diag::err_omp_grainsize_num_tasks_mutually_exclusive) |
| 5800 | << getOpenMPClauseName(C->getClauseKind()) |
| 5801 | << getOpenMPClauseName(PrevClause->getClauseKind()); |
| 5802 | S.Diag(PrevClause->getLocStart(), |
| 5803 | diag::note_omp_previous_grainsize_num_tasks) |
| 5804 | << getOpenMPClauseName(PrevClause->getClauseKind()); |
| 5805 | ErrorFound = true; |
| 5806 | } |
| 5807 | } |
| 5808 | } |
| 5809 | return ErrorFound; |
| 5810 | } |
| 5811 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5812 | StmtResult Sema::ActOnOpenMPTaskLoopDirective( |
| 5813 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 5814 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 5815 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5816 | if (!AStmt) |
| 5817 | return StmtError(); |
| 5818 | |
| 5819 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 5820 | OMPLoopDirective::HelperExprs B; |
| 5821 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 5822 | // define the nested loops number. |
| 5823 | unsigned NestedLoopCount = |
| 5824 | CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5825 | /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5826 | VarsWithImplicitDSA, B); |
| 5827 | if (NestedLoopCount == 0) |
| 5828 | return StmtError(); |
| 5829 | |
| 5830 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 5831 | "omp for loop exprs were not built"); |
| 5832 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 5833 | // OpenMP, [2.9.2 taskloop Construct, Restrictions] |
| 5834 | // The grainsize clause and num_tasks clause are mutually exclusive and may |
| 5835 | // not appear on the same taskloop directive. |
| 5836 | if (checkGrainsizeNumTasksClauses(*this, Clauses)) |
| 5837 | return StmtError(); |
| 5838 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5839 | getCurFunction()->setHasBranchProtectedScope(); |
| 5840 | return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, |
| 5841 | NestedLoopCount, Clauses, AStmt, B); |
| 5842 | } |
| 5843 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5844 | StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( |
| 5845 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 5846 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 5847 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5848 | if (!AStmt) |
| 5849 | return StmtError(); |
| 5850 | |
| 5851 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 5852 | OMPLoopDirective::HelperExprs B; |
| 5853 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 5854 | // define the nested loops number. |
| 5855 | unsigned NestedLoopCount = |
| 5856 | CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), |
| 5857 | /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, |
| 5858 | VarsWithImplicitDSA, B); |
| 5859 | if (NestedLoopCount == 0) |
| 5860 | return StmtError(); |
| 5861 | |
| 5862 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 5863 | "omp for loop exprs were not built"); |
| 5864 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 5865 | if (!CurContext->isDependentContext()) { |
| 5866 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 5867 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 5868 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 5869 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 5870 | B.NumIterations, *this, CurScope, |
| 5871 | DSAStack)) |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 5872 | return StmtError(); |
| 5873 | } |
| 5874 | } |
| 5875 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 5876 | // OpenMP, [2.9.2 taskloop Construct, Restrictions] |
| 5877 | // The grainsize clause and num_tasks clause are mutually exclusive and may |
| 5878 | // not appear on the same taskloop directive. |
| 5879 | if (checkGrainsizeNumTasksClauses(*this, Clauses)) |
| 5880 | return StmtError(); |
| 5881 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5882 | getCurFunction()->setHasBranchProtectedScope(); |
| 5883 | return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, |
| 5884 | NestedLoopCount, Clauses, AStmt, B); |
| 5885 | } |
| 5886 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 5887 | StmtResult Sema::ActOnOpenMPDistributeDirective( |
| 5888 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 5889 | SourceLocation EndLoc, |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 5890 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 5891 | if (!AStmt) |
| 5892 | return StmtError(); |
| 5893 | |
| 5894 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 5895 | OMPLoopDirective::HelperExprs B; |
| 5896 | // In presence of clause 'collapse' with number of loops, it will |
| 5897 | // define the nested loops number. |
| 5898 | unsigned NestedLoopCount = |
| 5899 | CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), |
| 5900 | nullptr /*ordered not a clause on distribute*/, AStmt, |
| 5901 | *this, *DSAStack, VarsWithImplicitDSA, B); |
| 5902 | if (NestedLoopCount == 0) |
| 5903 | return StmtError(); |
| 5904 | |
| 5905 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 5906 | "omp for loop exprs were not built"); |
| 5907 | |
| 5908 | getCurFunction()->setHasBranchProtectedScope(); |
| 5909 | return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, |
| 5910 | NestedLoopCount, Clauses, AStmt, B); |
| 5911 | } |
| 5912 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 5913 | StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( |
| 5914 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 5915 | SourceLocation EndLoc, |
| 5916 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 5917 | if (!AStmt) |
| 5918 | return StmtError(); |
| 5919 | |
| 5920 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5921 | // 1.2.2 OpenMP Language Terminology |
| 5922 | // Structured block - An executable statement with a single entry at the |
| 5923 | // top and a single exit at the bottom. |
| 5924 | // The point of exit cannot be a branch out of the structured block. |
| 5925 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5926 | CS->getCapturedDecl()->setNothrow(); |
| 5927 | |
| 5928 | OMPLoopDirective::HelperExprs B; |
| 5929 | // In presence of clause 'collapse' with number of loops, it will |
| 5930 | // define the nested loops number. |
| 5931 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 5932 | OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), |
| 5933 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 5934 | VarsWithImplicitDSA, B); |
| 5935 | if (NestedLoopCount == 0) |
| 5936 | return StmtError(); |
| 5937 | |
| 5938 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 5939 | "omp for loop exprs were not built"); |
| 5940 | |
| 5941 | getCurFunction()->setHasBranchProtectedScope(); |
| 5942 | return OMPDistributeParallelForDirective::Create( |
| 5943 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 5944 | } |
| 5945 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 5946 | StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( |
| 5947 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 5948 | SourceLocation EndLoc, |
| 5949 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 5950 | if (!AStmt) |
| 5951 | return StmtError(); |
| 5952 | |
| 5953 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5954 | // 1.2.2 OpenMP Language Terminology |
| 5955 | // Structured block - An executable statement with a single entry at the |
| 5956 | // top and a single exit at the bottom. |
| 5957 | // The point of exit cannot be a branch out of the structured block. |
| 5958 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5959 | CS->getCapturedDecl()->setNothrow(); |
| 5960 | |
| 5961 | OMPLoopDirective::HelperExprs B; |
| 5962 | // In presence of clause 'collapse' with number of loops, it will |
| 5963 | // define the nested loops number. |
| 5964 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 5965 | OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), |
| 5966 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 5967 | VarsWithImplicitDSA, B); |
| 5968 | if (NestedLoopCount == 0) |
| 5969 | return StmtError(); |
| 5970 | |
| 5971 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 5972 | "omp for loop exprs were not built"); |
| 5973 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 5974 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 5975 | return StmtError(); |
| 5976 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 5977 | getCurFunction()->setHasBranchProtectedScope(); |
| 5978 | return OMPDistributeParallelForSimdDirective::Create( |
| 5979 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 5980 | } |
| 5981 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 5982 | StmtResult Sema::ActOnOpenMPDistributeSimdDirective( |
| 5983 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 5984 | SourceLocation EndLoc, |
| 5985 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 5986 | if (!AStmt) |
| 5987 | return StmtError(); |
| 5988 | |
| 5989 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 5990 | // 1.2.2 OpenMP Language Terminology |
| 5991 | // Structured block - An executable statement with a single entry at the |
| 5992 | // top and a single exit at the bottom. |
| 5993 | // The point of exit cannot be a branch out of the structured block. |
| 5994 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 5995 | CS->getCapturedDecl()->setNothrow(); |
| 5996 | |
| 5997 | OMPLoopDirective::HelperExprs B; |
| 5998 | // In presence of clause 'collapse' with number of loops, it will |
| 5999 | // define the nested loops number. |
| 6000 | unsigned NestedLoopCount = |
| 6001 | CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), |
| 6002 | nullptr /*ordered not a clause on distribute*/, AStmt, |
| 6003 | *this, *DSAStack, VarsWithImplicitDSA, B); |
| 6004 | if (NestedLoopCount == 0) |
| 6005 | return StmtError(); |
| 6006 | |
| 6007 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6008 | "omp for loop exprs were not built"); |
| 6009 | |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 6010 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 6011 | return StmtError(); |
| 6012 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 6013 | getCurFunction()->setHasBranchProtectedScope(); |
| 6014 | return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, |
| 6015 | NestedLoopCount, Clauses, AStmt, B); |
| 6016 | } |
| 6017 | |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 6018 | StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( |
| 6019 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6020 | SourceLocation EndLoc, |
| 6021 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6022 | if (!AStmt) |
| 6023 | return StmtError(); |
| 6024 | |
| 6025 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6026 | // 1.2.2 OpenMP Language Terminology |
| 6027 | // Structured block - An executable statement with a single entry at the |
| 6028 | // top and a single exit at the bottom. |
| 6029 | // The point of exit cannot be a branch out of the structured block. |
| 6030 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6031 | CS->getCapturedDecl()->setNothrow(); |
| 6032 | |
| 6033 | OMPLoopDirective::HelperExprs B; |
| 6034 | // In presence of clause 'collapse' or 'ordered' with number of loops, it will |
| 6035 | // define the nested loops number. |
| 6036 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 6037 | OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), |
| 6038 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 6039 | VarsWithImplicitDSA, B); |
| 6040 | if (NestedLoopCount == 0) |
| 6041 | return StmtError(); |
| 6042 | |
| 6043 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6044 | "omp target parallel for simd loop exprs were not built"); |
| 6045 | |
| 6046 | if (!CurContext->isDependentContext()) { |
| 6047 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6048 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6049 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 6050 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6051 | B.NumIterations, *this, CurScope, |
| 6052 | DSAStack)) |
| 6053 | return StmtError(); |
| 6054 | } |
| 6055 | } |
Kelvin Li | c560949 | 2016-07-15 04:39:07 +0000 | [diff] [blame] | 6056 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 6057 | return StmtError(); |
| 6058 | |
| 6059 | getCurFunction()->setHasBranchProtectedScope(); |
| 6060 | return OMPTargetParallelForSimdDirective::Create( |
| 6061 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6062 | } |
| 6063 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 6064 | StmtResult Sema::ActOnOpenMPTargetSimdDirective( |
| 6065 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6066 | SourceLocation EndLoc, |
| 6067 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6068 | if (!AStmt) |
| 6069 | return StmtError(); |
| 6070 | |
| 6071 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6072 | // 1.2.2 OpenMP Language Terminology |
| 6073 | // Structured block - An executable statement with a single entry at the |
| 6074 | // top and a single exit at the bottom. |
| 6075 | // The point of exit cannot be a branch out of the structured block. |
| 6076 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6077 | CS->getCapturedDecl()->setNothrow(); |
| 6078 | |
| 6079 | OMPLoopDirective::HelperExprs B; |
| 6080 | // In presence of clause 'collapse' with number of loops, it will define the |
| 6081 | // nested loops number. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6082 | unsigned NestedLoopCount = |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 6083 | CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), |
| 6084 | getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, |
| 6085 | VarsWithImplicitDSA, B); |
| 6086 | if (NestedLoopCount == 0) |
| 6087 | return StmtError(); |
| 6088 | |
| 6089 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6090 | "omp target simd loop exprs were not built"); |
| 6091 | |
| 6092 | if (!CurContext->isDependentContext()) { |
| 6093 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6094 | for (auto C : Clauses) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6095 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 6096 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6097 | B.NumIterations, *this, CurScope, |
| 6098 | DSAStack)) |
| 6099 | return StmtError(); |
| 6100 | } |
| 6101 | } |
| 6102 | |
| 6103 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 6104 | return StmtError(); |
| 6105 | |
| 6106 | getCurFunction()->setHasBranchProtectedScope(); |
| 6107 | return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, |
| 6108 | NestedLoopCount, Clauses, AStmt, B); |
| 6109 | } |
| 6110 | |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 6111 | StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( |
| 6112 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6113 | SourceLocation EndLoc, |
| 6114 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6115 | if (!AStmt) |
| 6116 | return StmtError(); |
| 6117 | |
| 6118 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6119 | // 1.2.2 OpenMP Language Terminology |
| 6120 | // Structured block - An executable statement with a single entry at the |
| 6121 | // top and a single exit at the bottom. |
| 6122 | // The point of exit cannot be a branch out of the structured block. |
| 6123 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6124 | CS->getCapturedDecl()->setNothrow(); |
| 6125 | |
| 6126 | OMPLoopDirective::HelperExprs B; |
| 6127 | // In presence of clause 'collapse' with number of loops, it will |
| 6128 | // define the nested loops number. |
| 6129 | unsigned NestedLoopCount = |
| 6130 | CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), |
| 6131 | nullptr /*ordered not a clause on distribute*/, AStmt, |
| 6132 | *this, *DSAStack, VarsWithImplicitDSA, B); |
| 6133 | if (NestedLoopCount == 0) |
| 6134 | return StmtError(); |
| 6135 | |
| 6136 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6137 | "omp teams distribute loop exprs were not built"); |
| 6138 | |
| 6139 | getCurFunction()->setHasBranchProtectedScope(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6140 | return OMPTeamsDistributeDirective::Create( |
| 6141 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 6144 | StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( |
| 6145 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6146 | SourceLocation EndLoc, |
| 6147 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6148 | if (!AStmt) |
| 6149 | return StmtError(); |
| 6150 | |
| 6151 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6152 | // 1.2.2 OpenMP Language Terminology |
| 6153 | // Structured block - An executable statement with a single entry at the |
| 6154 | // top and a single exit at the bottom. |
| 6155 | // The point of exit cannot be a branch out of the structured block. |
| 6156 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6157 | CS->getCapturedDecl()->setNothrow(); |
| 6158 | |
| 6159 | OMPLoopDirective::HelperExprs B; |
| 6160 | // In presence of clause 'collapse' with number of loops, it will |
| 6161 | // define the nested loops number. |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 6162 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 6163 | OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), |
| 6164 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6165 | VarsWithImplicitDSA, B); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 6166 | |
| 6167 | if (NestedLoopCount == 0) |
| 6168 | return StmtError(); |
| 6169 | |
| 6170 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6171 | "omp teams distribute simd loop exprs were not built"); |
| 6172 | |
| 6173 | if (!CurContext->isDependentContext()) { |
| 6174 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6175 | for (auto C : Clauses) { |
| 6176 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6177 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6178 | B.NumIterations, *this, CurScope, |
| 6179 | DSAStack)) |
| 6180 | return StmtError(); |
| 6181 | } |
| 6182 | } |
| 6183 | |
| 6184 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 6185 | return StmtError(); |
| 6186 | |
| 6187 | getCurFunction()->setHasBranchProtectedScope(); |
| 6188 | return OMPTeamsDistributeSimdDirective::Create( |
| 6189 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6190 | } |
| 6191 | |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 6192 | StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( |
| 6193 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6194 | SourceLocation EndLoc, |
| 6195 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6196 | if (!AStmt) |
| 6197 | return StmtError(); |
| 6198 | |
| 6199 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6200 | // 1.2.2 OpenMP Language Terminology |
| 6201 | // Structured block - An executable statement with a single entry at the |
| 6202 | // top and a single exit at the bottom. |
| 6203 | // The point of exit cannot be a branch out of the structured block. |
| 6204 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6205 | CS->getCapturedDecl()->setNothrow(); |
| 6206 | |
| 6207 | OMPLoopDirective::HelperExprs B; |
| 6208 | // In presence of clause 'collapse' with number of loops, it will |
| 6209 | // define the nested loops number. |
| 6210 | auto NestedLoopCount = CheckOpenMPLoop( |
| 6211 | OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), |
| 6212 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6213 | VarsWithImplicitDSA, B); |
| 6214 | |
| 6215 | if (NestedLoopCount == 0) |
| 6216 | return StmtError(); |
| 6217 | |
| 6218 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6219 | "omp for loop exprs were not built"); |
| 6220 | |
| 6221 | if (!CurContext->isDependentContext()) { |
| 6222 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6223 | for (auto C : Clauses) { |
| 6224 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6225 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6226 | B.NumIterations, *this, CurScope, |
| 6227 | DSAStack)) |
| 6228 | return StmtError(); |
| 6229 | } |
| 6230 | } |
| 6231 | |
| 6232 | if (checkSimdlenSafelenSpecified(*this, Clauses)) |
| 6233 | return StmtError(); |
| 6234 | |
| 6235 | getCurFunction()->setHasBranchProtectedScope(); |
| 6236 | return OMPTeamsDistributeParallelForSimdDirective::Create( |
| 6237 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6238 | } |
| 6239 | |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 6240 | StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( |
| 6241 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6242 | SourceLocation EndLoc, |
| 6243 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6244 | if (!AStmt) |
| 6245 | return StmtError(); |
| 6246 | |
| 6247 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6248 | // 1.2.2 OpenMP Language Terminology |
| 6249 | // Structured block - An executable statement with a single entry at the |
| 6250 | // top and a single exit at the bottom. |
| 6251 | // The point of exit cannot be a branch out of the structured block. |
| 6252 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6253 | CS->getCapturedDecl()->setNothrow(); |
| 6254 | |
| 6255 | OMPLoopDirective::HelperExprs B; |
| 6256 | // In presence of clause 'collapse' with number of loops, it will |
| 6257 | // define the nested loops number. |
| 6258 | unsigned NestedLoopCount = CheckOpenMPLoop( |
| 6259 | OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), |
| 6260 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6261 | VarsWithImplicitDSA, B); |
| 6262 | |
| 6263 | if (NestedLoopCount == 0) |
| 6264 | return StmtError(); |
| 6265 | |
| 6266 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6267 | "omp for loop exprs were not built"); |
| 6268 | |
| 6269 | if (!CurContext->isDependentContext()) { |
| 6270 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6271 | for (auto C : Clauses) { |
| 6272 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6273 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6274 | B.NumIterations, *this, CurScope, |
| 6275 | DSAStack)) |
| 6276 | return StmtError(); |
| 6277 | } |
| 6278 | } |
| 6279 | |
| 6280 | getCurFunction()->setHasBranchProtectedScope(); |
| 6281 | return OMPTeamsDistributeParallelForDirective::Create( |
| 6282 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6283 | } |
| 6284 | |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 6285 | StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, |
| 6286 | Stmt *AStmt, |
| 6287 | SourceLocation StartLoc, |
| 6288 | SourceLocation EndLoc) { |
| 6289 | if (!AStmt) |
| 6290 | return StmtError(); |
| 6291 | |
| 6292 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6293 | // 1.2.2 OpenMP Language Terminology |
| 6294 | // Structured block - An executable statement with a single entry at the |
| 6295 | // top and a single exit at the bottom. |
| 6296 | // The point of exit cannot be a branch out of the structured block. |
| 6297 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6298 | CS->getCapturedDecl()->setNothrow(); |
| 6299 | |
| 6300 | getCurFunction()->setHasBranchProtectedScope(); |
| 6301 | |
| 6302 | return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 6303 | AStmt); |
| 6304 | } |
| 6305 | |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 6306 | StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( |
| 6307 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6308 | SourceLocation EndLoc, |
| 6309 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6310 | if (!AStmt) |
| 6311 | return StmtError(); |
| 6312 | |
| 6313 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6314 | // 1.2.2 OpenMP Language Terminology |
| 6315 | // Structured block - An executable statement with a single entry at the |
| 6316 | // top and a single exit at the bottom. |
| 6317 | // The point of exit cannot be a branch out of the structured block. |
| 6318 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6319 | CS->getCapturedDecl()->setNothrow(); |
| 6320 | |
| 6321 | OMPLoopDirective::HelperExprs B; |
| 6322 | // In presence of clause 'collapse' with number of loops, it will |
| 6323 | // define the nested loops number. |
| 6324 | auto NestedLoopCount = CheckOpenMPLoop( |
| 6325 | OMPD_target_teams_distribute, |
| 6326 | getCollapseNumberExpr(Clauses), |
| 6327 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6328 | VarsWithImplicitDSA, B); |
| 6329 | if (NestedLoopCount == 0) |
| 6330 | return StmtError(); |
| 6331 | |
| 6332 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6333 | "omp target teams distribute loop exprs were not built"); |
| 6334 | |
| 6335 | getCurFunction()->setHasBranchProtectedScope(); |
| 6336 | return OMPTargetTeamsDistributeDirective::Create( |
| 6337 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6338 | } |
| 6339 | |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 6340 | StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( |
| 6341 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6342 | SourceLocation EndLoc, |
| 6343 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6344 | if (!AStmt) |
| 6345 | return StmtError(); |
| 6346 | |
| 6347 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6348 | // 1.2.2 OpenMP Language Terminology |
| 6349 | // Structured block - An executable statement with a single entry at the |
| 6350 | // top and a single exit at the bottom. |
| 6351 | // The point of exit cannot be a branch out of the structured block. |
| 6352 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6353 | CS->getCapturedDecl()->setNothrow(); |
| 6354 | |
| 6355 | OMPLoopDirective::HelperExprs B; |
| 6356 | // In presence of clause 'collapse' with number of loops, it will |
| 6357 | // define the nested loops number. |
| 6358 | auto NestedLoopCount = CheckOpenMPLoop( |
| 6359 | OMPD_target_teams_distribute_parallel_for, |
| 6360 | getCollapseNumberExpr(Clauses), |
| 6361 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6362 | VarsWithImplicitDSA, B); |
| 6363 | if (NestedLoopCount == 0) |
| 6364 | return StmtError(); |
| 6365 | |
| 6366 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6367 | "omp target teams distribute parallel for 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) { |
| 6372 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6373 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6374 | B.NumIterations, *this, CurScope, |
| 6375 | DSAStack)) |
| 6376 | return StmtError(); |
| 6377 | } |
| 6378 | } |
| 6379 | |
| 6380 | getCurFunction()->setHasBranchProtectedScope(); |
| 6381 | return OMPTargetTeamsDistributeParallelForDirective::Create( |
| 6382 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6383 | } |
| 6384 | |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 6385 | StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( |
| 6386 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 6387 | SourceLocation EndLoc, |
| 6388 | llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { |
| 6389 | if (!AStmt) |
| 6390 | return StmtError(); |
| 6391 | |
| 6392 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 6393 | // 1.2.2 OpenMP Language Terminology |
| 6394 | // Structured block - An executable statement with a single entry at the |
| 6395 | // top and a single exit at the bottom. |
| 6396 | // The point of exit cannot be a branch out of the structured block. |
| 6397 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 6398 | CS->getCapturedDecl()->setNothrow(); |
| 6399 | |
| 6400 | OMPLoopDirective::HelperExprs B; |
| 6401 | // In presence of clause 'collapse' with number of loops, it will |
| 6402 | // define the nested loops number. |
| 6403 | auto NestedLoopCount = CheckOpenMPLoop( |
| 6404 | OMPD_target_teams_distribute_parallel_for_simd, |
| 6405 | getCollapseNumberExpr(Clauses), |
| 6406 | nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, |
| 6407 | VarsWithImplicitDSA, B); |
| 6408 | if (NestedLoopCount == 0) |
| 6409 | return StmtError(); |
| 6410 | |
| 6411 | assert((CurContext->isDependentContext() || B.builtAll()) && |
| 6412 | "omp target teams distribute parallel for simd loop exprs were not " |
| 6413 | "built"); |
| 6414 | |
| 6415 | if (!CurContext->isDependentContext()) { |
| 6416 | // Finalize the clauses that need pre-built expressions for CodeGen. |
| 6417 | for (auto C : Clauses) { |
| 6418 | if (auto *LC = dyn_cast<OMPLinearClause>(C)) |
| 6419 | if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), |
| 6420 | B.NumIterations, *this, CurScope, |
| 6421 | DSAStack)) |
| 6422 | return StmtError(); |
| 6423 | } |
| 6424 | } |
| 6425 | |
| 6426 | getCurFunction()->setHasBranchProtectedScope(); |
| 6427 | return OMPTargetTeamsDistributeParallelForSimdDirective::Create( |
| 6428 | Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); |
| 6429 | } |
| 6430 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6431 | OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6432 | SourceLocation StartLoc, |
| 6433 | SourceLocation LParenLoc, |
| 6434 | SourceLocation EndLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 6435 | OMPClause *Res = nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6436 | switch (Kind) { |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 6437 | case OMPC_final: |
| 6438 | Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6439 | break; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6440 | case OMPC_num_threads: |
| 6441 | Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6442 | break; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6443 | case OMPC_safelen: |
| 6444 | Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6445 | break; |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 6446 | case OMPC_simdlen: |
| 6447 | Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6448 | break; |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 6449 | case OMPC_collapse: |
| 6450 | Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6451 | break; |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 6452 | case OMPC_ordered: |
| 6453 | Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); |
| 6454 | break; |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 6455 | case OMPC_device: |
| 6456 | Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6457 | break; |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 6458 | case OMPC_num_teams: |
| 6459 | Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6460 | break; |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6461 | case OMPC_thread_limit: |
| 6462 | Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6463 | break; |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 6464 | case OMPC_priority: |
| 6465 | Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6466 | break; |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 6467 | case OMPC_grainsize: |
| 6468 | Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6469 | break; |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 6470 | case OMPC_num_tasks: |
| 6471 | Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6472 | break; |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 6473 | case OMPC_hint: |
| 6474 | Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 6475 | break; |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 6476 | case OMPC_if: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6477 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 6478 | case OMPC_proc_bind: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6479 | case OMPC_schedule: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6480 | case OMPC_private: |
| 6481 | case OMPC_firstprivate: |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 6482 | case OMPC_lastprivate: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6483 | case OMPC_shared: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 6484 | case OMPC_reduction: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 6485 | case OMPC_linear: |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 6486 | case OMPC_aligned: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6487 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6488 | case OMPC_copyprivate: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 6489 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 6490 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 6491 | case OMPC_mergeable: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6492 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 6493 | case OMPC_flush: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 6494 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 6495 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 6496 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 6497 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 6498 | case OMPC_seq_cst: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 6499 | case OMPC_depend: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 6500 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 6501 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 6502 | case OMPC_map: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 6503 | case OMPC_nogroup: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 6504 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 6505 | case OMPC_defaultmap: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6506 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 6507 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 6508 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 6509 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 6510 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 6511 | case OMPC_is_device_ptr: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6512 | llvm_unreachable("Clause is not allowed."); |
| 6513 | } |
| 6514 | return Res; |
| 6515 | } |
| 6516 | |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 6517 | OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, |
| 6518 | Expr *Condition, SourceLocation StartLoc, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6519 | SourceLocation LParenLoc, |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 6520 | SourceLocation NameModifierLoc, |
| 6521 | SourceLocation ColonLoc, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6522 | SourceLocation EndLoc) { |
| 6523 | Expr *ValExpr = Condition; |
| 6524 | if (!Condition->isValueDependent() && !Condition->isTypeDependent() && |
| 6525 | !Condition->isInstantiationDependent() && |
| 6526 | !Condition->containsUnexpandedParameterPack()) { |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 6527 | ExprResult Val = CheckBooleanCondition(StartLoc, Condition); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6528 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 6529 | return nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6530 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 6531 | ValExpr = MakeFullExpr(Val.get()).get(); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6532 | } |
| 6533 | |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 6534 | return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc, |
| 6535 | NameModifierLoc, ColonLoc, EndLoc); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6536 | } |
| 6537 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 6538 | OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, |
| 6539 | SourceLocation StartLoc, |
| 6540 | SourceLocation LParenLoc, |
| 6541 | SourceLocation EndLoc) { |
| 6542 | Expr *ValExpr = Condition; |
| 6543 | if (!Condition->isValueDependent() && !Condition->isTypeDependent() && |
| 6544 | !Condition->isInstantiationDependent() && |
| 6545 | !Condition->containsUnexpandedParameterPack()) { |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 6546 | ExprResult Val = CheckBooleanCondition(StartLoc, Condition); |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 6547 | if (Val.isInvalid()) |
| 6548 | return nullptr; |
| 6549 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 6550 | ValExpr = MakeFullExpr(Val.get()).get(); |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
| 6553 | return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 6554 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 6555 | ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, |
| 6556 | Expr *Op) { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6557 | if (!Op) |
| 6558 | return ExprError(); |
| 6559 | |
| 6560 | class IntConvertDiagnoser : public ICEConvertDiagnoser { |
| 6561 | public: |
| 6562 | IntConvertDiagnoser() |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6563 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 6564 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 6565 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6566 | return S.Diag(Loc, diag::err_omp_not_integral) << T; |
| 6567 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6568 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
| 6569 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6570 | return S.Diag(Loc, diag::err_omp_incomplete_type) << T; |
| 6571 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6572 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
| 6573 | QualType T, |
| 6574 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6575 | return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; |
| 6576 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6577 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
| 6578 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6579 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6580 | << ConvTy->isEnumeralType() << ConvTy; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6581 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6582 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 6583 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6584 | return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; |
| 6585 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6586 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
| 6587 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6588 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6589 | << ConvTy->isEnumeralType() << ConvTy; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6590 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6591 | SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, |
| 6592 | QualType) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6593 | llvm_unreachable("conversion functions are permitted"); |
| 6594 | } |
| 6595 | } ConvertDiagnoser; |
| 6596 | return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); |
| 6597 | } |
| 6598 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6599 | static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 6600 | OpenMPClauseKind CKind, |
| 6601 | bool StrictlyPositive) { |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6602 | if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && |
| 6603 | !ValExpr->isInstantiationDependent()) { |
| 6604 | SourceLocation Loc = ValExpr->getExprLoc(); |
| 6605 | ExprResult Value = |
| 6606 | SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); |
| 6607 | if (Value.isInvalid()) |
| 6608 | return false; |
| 6609 | |
| 6610 | ValExpr = Value.get(); |
| 6611 | // The expression must evaluate to a non-negative integer value. |
| 6612 | llvm::APSInt Result; |
| 6613 | if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 6614 | Result.isSigned() && |
| 6615 | !((!StrictlyPositive && Result.isNonNegative()) || |
| 6616 | (StrictlyPositive && Result.isStrictlyPositive()))) { |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6617 | SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 6618 | << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) |
| 6619 | << ValExpr->getSourceRange(); |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6620 | return false; |
| 6621 | } |
| 6622 | } |
| 6623 | return true; |
| 6624 | } |
| 6625 | |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6626 | OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, |
| 6627 | SourceLocation StartLoc, |
| 6628 | SourceLocation LParenLoc, |
| 6629 | SourceLocation EndLoc) { |
| 6630 | Expr *ValExpr = NumThreads; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6631 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6632 | // OpenMP [2.5, Restrictions] |
| 6633 | // The num_threads expression must evaluate to a positive integer value. |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 6634 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, |
| 6635 | /*StrictlyPositive=*/true)) |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6636 | return nullptr; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6637 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6638 | return new (Context) |
| 6639 | OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6640 | } |
| 6641 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6642 | ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 6643 | OpenMPClauseKind CKind, |
| 6644 | bool StrictlyPositive) { |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6645 | if (!E) |
| 6646 | return ExprError(); |
| 6647 | if (E->isValueDependent() || E->isTypeDependent() || |
| 6648 | E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6649 | return E; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6650 | llvm::APSInt Result; |
| 6651 | ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); |
| 6652 | if (ICE.isInvalid()) |
| 6653 | return ExprError(); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 6654 | if ((StrictlyPositive && !Result.isStrictlyPositive()) || |
| 6655 | (!StrictlyPositive && !Result.isNonNegative())) { |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6656 | Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 6657 | << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) |
| 6658 | << E->getSourceRange(); |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6659 | return ExprError(); |
| 6660 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 6661 | if (CKind == OMPC_aligned && !Result.isPowerOf2()) { |
| 6662 | Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) |
| 6663 | << E->getSourceRange(); |
| 6664 | return ExprError(); |
| 6665 | } |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 6666 | if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) |
| 6667 | DSAStack->setAssociatedLoops(Result.getExtValue()); |
Alexey Bataev | 7b6bc88 | 2015-11-26 07:50:39 +0000 | [diff] [blame] | 6668 | else if (CKind == OMPC_ordered) |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 6669 | DSAStack->setAssociatedLoops(Result.getExtValue()); |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6670 | return ICE; |
| 6671 | } |
| 6672 | |
| 6673 | OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, |
| 6674 | SourceLocation LParenLoc, |
| 6675 | SourceLocation EndLoc) { |
| 6676 | // OpenMP [2.8.1, simd construct, Description] |
| 6677 | // The parameter of the safelen clause must be a constant |
| 6678 | // positive integer expression. |
| 6679 | ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); |
| 6680 | if (Safelen.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 6681 | return nullptr; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6682 | return new (Context) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6683 | OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 6686 | OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, |
| 6687 | SourceLocation LParenLoc, |
| 6688 | SourceLocation EndLoc) { |
| 6689 | // OpenMP [2.8.1, simd construct, Description] |
| 6690 | // The parameter of the simdlen clause must be a constant |
| 6691 | // positive integer expression. |
| 6692 | ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); |
| 6693 | if (Simdlen.isInvalid()) |
| 6694 | return nullptr; |
| 6695 | return new (Context) |
| 6696 | OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); |
| 6697 | } |
| 6698 | |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 6699 | OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, |
| 6700 | SourceLocation StartLoc, |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 6701 | SourceLocation LParenLoc, |
| 6702 | SourceLocation EndLoc) { |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 6703 | // OpenMP [2.7.1, loop construct, Description] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 6704 | // OpenMP [2.8.1, simd construct, Description] |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 6705 | // OpenMP [2.9.6, distribute construct, Description] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 6706 | // The parameter of the collapse clause must be a constant |
| 6707 | // positive integer expression. |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 6708 | ExprResult NumForLoopsResult = |
| 6709 | VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); |
| 6710 | if (NumForLoopsResult.isInvalid()) |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 6711 | return nullptr; |
| 6712 | return new (Context) |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 6713 | OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 6714 | } |
| 6715 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 6716 | OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, |
| 6717 | SourceLocation EndLoc, |
| 6718 | SourceLocation LParenLoc, |
| 6719 | Expr *NumForLoops) { |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 6720 | // OpenMP [2.7.1, loop construct, Description] |
| 6721 | // OpenMP [2.8.1, simd construct, Description] |
| 6722 | // OpenMP [2.9.6, distribute construct, Description] |
| 6723 | // The parameter of the ordered clause must be a constant |
| 6724 | // positive integer expression if any. |
| 6725 | if (NumForLoops && LParenLoc.isValid()) { |
| 6726 | ExprResult NumForLoopsResult = |
| 6727 | VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); |
| 6728 | if (NumForLoopsResult.isInvalid()) |
| 6729 | return nullptr; |
| 6730 | NumForLoops = NumForLoopsResult.get(); |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 6731 | } else |
| 6732 | NumForLoops = nullptr; |
| 6733 | DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops); |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 6734 | return new (Context) |
| 6735 | OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc); |
| 6736 | } |
| 6737 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6738 | OMPClause *Sema::ActOnOpenMPSimpleClause( |
| 6739 | OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, |
| 6740 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 6741 | OMPClause *Res = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6742 | switch (Kind) { |
| 6743 | case OMPC_default: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 6744 | Res = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6745 | ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), |
| 6746 | ArgumentLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6747 | break; |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 6748 | case OMPC_proc_bind: |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6749 | Res = ActOnOpenMPProcBindClause( |
| 6750 | static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, |
| 6751 | LParenLoc, EndLoc); |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 6752 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6753 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 6754 | case OMPC_final: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 6755 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 6756 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 6757 | case OMPC_simdlen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 6758 | case OMPC_collapse: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6759 | case OMPC_schedule: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6760 | case OMPC_private: |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 6761 | case OMPC_firstprivate: |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 6762 | case OMPC_lastprivate: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 6763 | case OMPC_shared: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 6764 | case OMPC_reduction: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 6765 | case OMPC_linear: |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 6766 | case OMPC_aligned: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 6767 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6768 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 6769 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 6770 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 6771 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 6772 | case OMPC_mergeable: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6773 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 6774 | case OMPC_flush: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 6775 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 6776 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 6777 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 6778 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 6779 | case OMPC_seq_cst: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 6780 | case OMPC_depend: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 6781 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 6782 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 6783 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 6784 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 6785 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6786 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 6787 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 6788 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 6789 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 6790 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 6791 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 6792 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 6793 | case OMPC_defaultmap: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6794 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 6795 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 6796 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 6797 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 6798 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 6799 | case OMPC_is_device_ptr: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6800 | llvm_unreachable("Clause is not allowed."); |
| 6801 | } |
| 6802 | return Res; |
| 6803 | } |
| 6804 | |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6805 | static std::string |
| 6806 | getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, |
| 6807 | ArrayRef<unsigned> Exclude = llvm::None) { |
| 6808 | std::string Values; |
| 6809 | unsigned Bound = Last >= 2 ? Last - 2 : 0; |
| 6810 | unsigned Skipped = Exclude.size(); |
| 6811 | auto S = Exclude.begin(), E = Exclude.end(); |
| 6812 | for (unsigned i = First; i < Last; ++i) { |
| 6813 | if (std::find(S, E, i) != E) { |
| 6814 | --Skipped; |
| 6815 | continue; |
| 6816 | } |
| 6817 | Values += "'"; |
| 6818 | Values += getOpenMPSimpleClauseTypeName(K, i); |
| 6819 | Values += "'"; |
| 6820 | if (i == Bound - Skipped) |
| 6821 | Values += " or "; |
| 6822 | else if (i != Bound + 1 - Skipped) |
| 6823 | Values += ", "; |
| 6824 | } |
| 6825 | return Values; |
| 6826 | } |
| 6827 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6828 | OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, |
| 6829 | SourceLocation KindKwLoc, |
| 6830 | SourceLocation StartLoc, |
| 6831 | SourceLocation LParenLoc, |
| 6832 | SourceLocation EndLoc) { |
| 6833 | if (Kind == OMPC_DEFAULT_unknown) { |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 6834 | static_assert(OMPC_DEFAULT_unknown > 0, |
| 6835 | "OMPC_DEFAULT_unknown not greater than 0"); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6836 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6837 | << getListOfPossibleValues(OMPC_default, /*First=*/0, |
| 6838 | /*Last=*/OMPC_DEFAULT_unknown) |
| 6839 | << getOpenMPClauseName(OMPC_default); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 6840 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6841 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 6842 | switch (Kind) { |
| 6843 | case OMPC_DEFAULT_none: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6844 | DSAStack->setDefaultDSANone(KindKwLoc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 6845 | break; |
| 6846 | case OMPC_DEFAULT_shared: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6847 | DSAStack->setDefaultDSAShared(KindKwLoc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 6848 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6849 | case OMPC_DEFAULT_unknown: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 6850 | llvm_unreachable("Clause kind is not allowed."); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 6851 | break; |
| 6852 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6853 | return new (Context) |
| 6854 | OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 6855 | } |
| 6856 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 6857 | OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, |
| 6858 | SourceLocation KindKwLoc, |
| 6859 | SourceLocation StartLoc, |
| 6860 | SourceLocation LParenLoc, |
| 6861 | SourceLocation EndLoc) { |
| 6862 | if (Kind == OMPC_PROC_BIND_unknown) { |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 6863 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6864 | << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, |
| 6865 | /*Last=*/OMPC_PROC_BIND_unknown) |
| 6866 | << getOpenMPClauseName(OMPC_proc_bind); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 6867 | return nullptr; |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 6868 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 6869 | return new (Context) |
| 6870 | OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 6871 | } |
| 6872 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6873 | OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6874 | OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6875 | SourceLocation StartLoc, SourceLocation LParenLoc, |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6876 | ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6877 | SourceLocation EndLoc) { |
| 6878 | OMPClause *Res = nullptr; |
| 6879 | switch (Kind) { |
| 6880 | case OMPC_schedule: |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6881 | enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; |
| 6882 | assert(Argument.size() == NumberOfElements && |
| 6883 | ArgumentLoc.size() == NumberOfElements); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6884 | Res = ActOnOpenMPScheduleClause( |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6885 | static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), |
| 6886 | static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), |
| 6887 | static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, |
| 6888 | StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], |
| 6889 | ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6890 | break; |
| 6891 | case OMPC_if: |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6892 | assert(Argument.size() == 1 && ArgumentLoc.size() == 1); |
| 6893 | Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), |
| 6894 | Expr, StartLoc, LParenLoc, ArgumentLoc.back(), |
| 6895 | DelimLoc, EndLoc); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 6896 | break; |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 6897 | case OMPC_dist_schedule: |
| 6898 | Res = ActOnOpenMPDistScheduleClause( |
| 6899 | static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, |
| 6900 | StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); |
| 6901 | break; |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 6902 | case OMPC_defaultmap: |
| 6903 | enum { Modifier, DefaultmapKind }; |
| 6904 | Res = ActOnOpenMPDefaultmapClause( |
| 6905 | static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), |
| 6906 | static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 6907 | StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], |
| 6908 | EndLoc); |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 6909 | break; |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 6910 | case OMPC_final: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6911 | case OMPC_num_threads: |
| 6912 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 6913 | case OMPC_simdlen: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6914 | case OMPC_collapse: |
| 6915 | case OMPC_default: |
| 6916 | case OMPC_proc_bind: |
| 6917 | case OMPC_private: |
| 6918 | case OMPC_firstprivate: |
| 6919 | case OMPC_lastprivate: |
| 6920 | case OMPC_shared: |
| 6921 | case OMPC_reduction: |
| 6922 | case OMPC_linear: |
| 6923 | case OMPC_aligned: |
| 6924 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 6925 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 6926 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 6927 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 6928 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 6929 | case OMPC_mergeable: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6930 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 6931 | case OMPC_flush: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 6932 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 6933 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 6934 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 6935 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 6936 | case OMPC_seq_cst: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 6937 | case OMPC_depend: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 6938 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 6939 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 6940 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 6941 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 6942 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 6943 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 6944 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 6945 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 6946 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 6947 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 6948 | case OMPC_hint: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6949 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 6950 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 6951 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 6952 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 6953 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 6954 | case OMPC_is_device_ptr: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6955 | llvm_unreachable("Clause is not allowed."); |
| 6956 | } |
| 6957 | return Res; |
| 6958 | } |
| 6959 | |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6960 | static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, |
| 6961 | OpenMPScheduleClauseModifier M2, |
| 6962 | SourceLocation M1Loc, SourceLocation M2Loc) { |
| 6963 | if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { |
| 6964 | SmallVector<unsigned, 2> Excluded; |
| 6965 | if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) |
| 6966 | Excluded.push_back(M2); |
| 6967 | if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) |
| 6968 | Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); |
| 6969 | if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) |
| 6970 | Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); |
| 6971 | S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) |
| 6972 | << getListOfPossibleValues(OMPC_schedule, |
| 6973 | /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, |
| 6974 | /*Last=*/OMPC_SCHEDULE_MODIFIER_last, |
| 6975 | Excluded) |
| 6976 | << getOpenMPClauseName(OMPC_schedule); |
| 6977 | return true; |
| 6978 | } |
| 6979 | return false; |
| 6980 | } |
| 6981 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6982 | OMPClause *Sema::ActOnOpenMPScheduleClause( |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6983 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 6984 | OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 6985 | SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, |
| 6986 | SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { |
| 6987 | if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || |
| 6988 | checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) |
| 6989 | return nullptr; |
| 6990 | // OpenMP, 2.7.1, Loop Construct, Restrictions |
| 6991 | // Either the monotonic modifier or the nonmonotonic modifier can be specified |
| 6992 | // but not both. |
| 6993 | if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || |
| 6994 | (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && |
| 6995 | M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || |
| 6996 | (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && |
| 6997 | M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { |
| 6998 | Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) |
| 6999 | << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) |
| 7000 | << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); |
| 7001 | return nullptr; |
| 7002 | } |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7003 | if (Kind == OMPC_SCHEDULE_unknown) { |
| 7004 | std::string Values; |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7005 | if (M1Loc.isInvalid() && M2Loc.isInvalid()) { |
| 7006 | unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; |
| 7007 | Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, |
| 7008 | /*Last=*/OMPC_SCHEDULE_MODIFIER_last, |
| 7009 | Exclude); |
| 7010 | } else { |
| 7011 | Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, |
| 7012 | /*Last=*/OMPC_SCHEDULE_unknown); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7013 | } |
| 7014 | Diag(KindLoc, diag::err_omp_unexpected_clause_value) |
| 7015 | << Values << getOpenMPClauseName(OMPC_schedule); |
| 7016 | return nullptr; |
| 7017 | } |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7018 | // OpenMP, 2.7.1, Loop Construct, Restrictions |
| 7019 | // The nonmonotonic modifier can only be specified with schedule(dynamic) or |
| 7020 | // schedule(guided). |
| 7021 | if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || |
| 7022 | M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && |
| 7023 | Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { |
| 7024 | Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, |
| 7025 | diag::err_omp_schedule_nonmonotonic_static); |
| 7026 | return nullptr; |
| 7027 | } |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7028 | Expr *ValExpr = ChunkSize; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 7029 | Stmt *HelperValStmt = nullptr; |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7030 | if (ChunkSize) { |
| 7031 | if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && |
| 7032 | !ChunkSize->isInstantiationDependent() && |
| 7033 | !ChunkSize->containsUnexpandedParameterPack()) { |
| 7034 | SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); |
| 7035 | ExprResult Val = |
| 7036 | PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); |
| 7037 | if (Val.isInvalid()) |
| 7038 | return nullptr; |
| 7039 | |
| 7040 | ValExpr = Val.get(); |
| 7041 | |
| 7042 | // OpenMP [2.7.1, Restrictions] |
| 7043 | // chunk_size must be a loop invariant integer expression with a positive |
| 7044 | // value. |
| 7045 | llvm::APSInt Result; |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 7046 | if (ValExpr->isIntegerConstantExpr(Result, Context)) { |
| 7047 | if (Result.isSigned() && !Result.isStrictlyPositive()) { |
| 7048 | Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7049 | << "schedule" << 1 << ChunkSize->getSourceRange(); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 7050 | return nullptr; |
| 7051 | } |
Alexey Bataev | b46cdea | 2016-06-15 11:20:48 +0000 | [diff] [blame] | 7052 | } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && |
| 7053 | !CurContext->isDependentContext()) { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 7054 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
| 7055 | ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); |
| 7056 | HelperValStmt = buildPreInits(Context, Captures); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7057 | } |
| 7058 | } |
| 7059 | } |
| 7060 | |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 7061 | return new (Context) |
| 7062 | OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 7063 | ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7066 | OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, |
| 7067 | SourceLocation StartLoc, |
| 7068 | SourceLocation EndLoc) { |
| 7069 | OMPClause *Res = nullptr; |
| 7070 | switch (Kind) { |
| 7071 | case OMPC_ordered: |
| 7072 | Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); |
| 7073 | break; |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7074 | case OMPC_nowait: |
| 7075 | Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); |
| 7076 | break; |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 7077 | case OMPC_untied: |
| 7078 | Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); |
| 7079 | break; |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 7080 | case OMPC_mergeable: |
| 7081 | Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); |
| 7082 | break; |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7083 | case OMPC_read: |
| 7084 | Res = ActOnOpenMPReadClause(StartLoc, EndLoc); |
| 7085 | break; |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 7086 | case OMPC_write: |
| 7087 | Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); |
| 7088 | break; |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 7089 | case OMPC_update: |
| 7090 | Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); |
| 7091 | break; |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 7092 | case OMPC_capture: |
| 7093 | Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); |
| 7094 | break; |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 7095 | case OMPC_seq_cst: |
| 7096 | Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); |
| 7097 | break; |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 7098 | case OMPC_threads: |
| 7099 | Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); |
| 7100 | break; |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 7101 | case OMPC_simd: |
| 7102 | Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); |
| 7103 | break; |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 7104 | case OMPC_nogroup: |
| 7105 | Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); |
| 7106 | break; |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7107 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 7108 | case OMPC_final: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7109 | case OMPC_num_threads: |
| 7110 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 7111 | case OMPC_simdlen: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7112 | case OMPC_collapse: |
| 7113 | case OMPC_schedule: |
| 7114 | case OMPC_private: |
| 7115 | case OMPC_firstprivate: |
| 7116 | case OMPC_lastprivate: |
| 7117 | case OMPC_shared: |
| 7118 | case OMPC_reduction: |
| 7119 | case OMPC_linear: |
| 7120 | case OMPC_aligned: |
| 7121 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 7122 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7123 | case OMPC_default: |
| 7124 | case OMPC_proc_bind: |
| 7125 | case OMPC_threadprivate: |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 7126 | case OMPC_flush: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7127 | case OMPC_depend: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 7128 | case OMPC_device: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 7129 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 7130 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7131 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7132 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 7133 | case OMPC_grainsize: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 7134 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 7135 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 7136 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 7137 | case OMPC_defaultmap: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7138 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 7139 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 7140 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 7141 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 7142 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 7143 | case OMPC_is_device_ptr: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7144 | llvm_unreachable("Clause is not allowed."); |
| 7145 | } |
| 7146 | return Res; |
| 7147 | } |
| 7148 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7149 | OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, |
| 7150 | SourceLocation EndLoc) { |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 7151 | DSAStack->setNowaitRegion(); |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7152 | return new (Context) OMPNowaitClause(StartLoc, EndLoc); |
| 7153 | } |
| 7154 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 7155 | OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, |
| 7156 | SourceLocation EndLoc) { |
| 7157 | return new (Context) OMPUntiedClause(StartLoc, EndLoc); |
| 7158 | } |
| 7159 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 7160 | OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, |
| 7161 | SourceLocation EndLoc) { |
| 7162 | return new (Context) OMPMergeableClause(StartLoc, EndLoc); |
| 7163 | } |
| 7164 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7165 | OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, |
| 7166 | SourceLocation EndLoc) { |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7167 | return new (Context) OMPReadClause(StartLoc, EndLoc); |
| 7168 | } |
| 7169 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 7170 | OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, |
| 7171 | SourceLocation EndLoc) { |
| 7172 | return new (Context) OMPWriteClause(StartLoc, EndLoc); |
| 7173 | } |
| 7174 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 7175 | OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, |
| 7176 | SourceLocation EndLoc) { |
| 7177 | return new (Context) OMPUpdateClause(StartLoc, EndLoc); |
| 7178 | } |
| 7179 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 7180 | OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, |
| 7181 | SourceLocation EndLoc) { |
| 7182 | return new (Context) OMPCaptureClause(StartLoc, EndLoc); |
| 7183 | } |
| 7184 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 7185 | OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, |
| 7186 | SourceLocation EndLoc) { |
| 7187 | return new (Context) OMPSeqCstClause(StartLoc, EndLoc); |
| 7188 | } |
| 7189 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 7190 | OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, |
| 7191 | SourceLocation EndLoc) { |
| 7192 | return new (Context) OMPThreadsClause(StartLoc, EndLoc); |
| 7193 | } |
| 7194 | |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 7195 | OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, |
| 7196 | SourceLocation EndLoc) { |
| 7197 | return new (Context) OMPSIMDClause(StartLoc, EndLoc); |
| 7198 | } |
| 7199 | |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 7200 | OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, |
| 7201 | SourceLocation EndLoc) { |
| 7202 | return new (Context) OMPNogroupClause(StartLoc, EndLoc); |
| 7203 | } |
| 7204 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 7205 | OMPClause *Sema::ActOnOpenMPVarListClause( |
| 7206 | OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, |
| 7207 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, |
| 7208 | SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7209 | const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, |
Samuel Antao | 23abd72 | 2016-01-19 20:40:49 +0000 | [diff] [blame] | 7210 | OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, |
| 7211 | OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, |
| 7212 | SourceLocation DepLinMapLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 7213 | OMPClause *Res = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7214 | switch (Kind) { |
| 7215 | case OMPC_private: |
| 7216 | Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7217 | break; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7218 | case OMPC_firstprivate: |
| 7219 | Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7220 | break; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7221 | case OMPC_lastprivate: |
| 7222 | Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7223 | break; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7224 | case OMPC_shared: |
| 7225 | Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7226 | break; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 7227 | case OMPC_reduction: |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 7228 | Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, |
| 7229 | EndLoc, ReductionIdScopeSpec, ReductionId); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 7230 | break; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 7231 | case OMPC_linear: |
| 7232 | Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 7233 | LinKind, DepLinMapLoc, ColonLoc, EndLoc); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 7234 | break; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 7235 | case OMPC_aligned: |
| 7236 | Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, |
| 7237 | ColonLoc, EndLoc); |
| 7238 | break; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 7239 | case OMPC_copyin: |
| 7240 | Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7241 | break; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 7242 | case OMPC_copyprivate: |
| 7243 | Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7244 | break; |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 7245 | case OMPC_flush: |
| 7246 | Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7247 | break; |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7248 | case OMPC_depend: |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 7249 | Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 7250 | StartLoc, LParenLoc, EndLoc); |
| 7251 | break; |
| 7252 | case OMPC_map: |
Samuel Antao | 23abd72 | 2016-01-19 20:40:49 +0000 | [diff] [blame] | 7253 | Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit, |
| 7254 | DepLinMapLoc, ColonLoc, VarList, StartLoc, |
| 7255 | LParenLoc, EndLoc); |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 7256 | break; |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 7257 | case OMPC_to: |
| 7258 | Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7259 | break; |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 7260 | case OMPC_from: |
| 7261 | Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7262 | break; |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 7263 | case OMPC_use_device_ptr: |
| 7264 | Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7265 | break; |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 7266 | case OMPC_is_device_ptr: |
| 7267 | Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 7268 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 7269 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 7270 | case OMPC_final: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 7271 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 7272 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 7273 | case OMPC_simdlen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 7274 | case OMPC_collapse: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7275 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 7276 | case OMPC_proc_bind: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 7277 | case OMPC_schedule: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 7278 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 7279 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 7280 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 7281 | case OMPC_mergeable: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7282 | case OMPC_threadprivate: |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 7283 | case OMPC_read: |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 7284 | case OMPC_write: |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 7285 | case OMPC_update: |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 7286 | case OMPC_capture: |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 7287 | case OMPC_seq_cst: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 7288 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 7289 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 7290 | case OMPC_simd: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 7291 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 7292 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 7293 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 7294 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 7295 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 7296 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 7297 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 7298 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 7299 | case OMPC_defaultmap: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7300 | case OMPC_unknown: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 7301 | case OMPC_uniform: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7302 | llvm_unreachable("Clause is not allowed."); |
| 7303 | } |
| 7304 | return Res; |
| 7305 | } |
| 7306 | |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 7307 | ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 7308 | ExprObjectKind OK, SourceLocation Loc) { |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 7309 | ExprResult Res = BuildDeclRefExpr( |
| 7310 | Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); |
| 7311 | if (!Res.isUsable()) |
| 7312 | return ExprError(); |
| 7313 | if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { |
| 7314 | Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); |
| 7315 | if (!Res.isUsable()) |
| 7316 | return ExprError(); |
| 7317 | } |
| 7318 | if (VK != VK_LValue && Res.get()->isGLValue()) { |
| 7319 | Res = DefaultLvalueConversion(Res.get()); |
| 7320 | if (!Res.isUsable()) |
| 7321 | return ExprError(); |
| 7322 | } |
| 7323 | return Res; |
| 7324 | } |
| 7325 | |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7326 | static std::pair<ValueDecl *, bool> |
| 7327 | getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, |
| 7328 | SourceRange &ERange, bool AllowArraySection = false) { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7329 | if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || |
| 7330 | RefExpr->containsUnexpandedParameterPack()) |
| 7331 | return std::make_pair(nullptr, true); |
| 7332 | |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7333 | // OpenMP [3.1, C/C++] |
| 7334 | // A list item is a variable name. |
| 7335 | // OpenMP [2.9.3.3, Restrictions, p.1] |
| 7336 | // A variable that is part of another variable (as an array or |
| 7337 | // structure element) cannot appear in a private clause. |
| 7338 | RefExpr = RefExpr->IgnoreParens(); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7339 | enum { |
| 7340 | NoArrayExpr = -1, |
| 7341 | ArraySubscript = 0, |
| 7342 | OMPArraySection = 1 |
| 7343 | } IsArrayExpr = NoArrayExpr; |
| 7344 | if (AllowArraySection) { |
| 7345 | if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { |
| 7346 | auto *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 7347 | while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 7348 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 7349 | RefExpr = Base; |
| 7350 | IsArrayExpr = ArraySubscript; |
| 7351 | } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { |
| 7352 | auto *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 7353 | while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
| 7354 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
| 7355 | while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 7356 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 7357 | RefExpr = Base; |
| 7358 | IsArrayExpr = OMPArraySection; |
| 7359 | } |
| 7360 | } |
| 7361 | ELoc = RefExpr->getExprLoc(); |
| 7362 | ERange = RefExpr->getSourceRange(); |
| 7363 | RefExpr = RefExpr->IgnoreParenImpCasts(); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7364 | auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
| 7365 | auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); |
| 7366 | if ((!DE || !isa<VarDecl>(DE->getDecl())) && |
| 7367 | (S.getCurrentThisType().isNull() || !ME || |
| 7368 | !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || |
| 7369 | !isa<FieldDecl>(ME->getMemberDecl()))) { |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7370 | if (IsArrayExpr != NoArrayExpr) |
| 7371 | S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr |
| 7372 | << ERange; |
| 7373 | else { |
| 7374 | S.Diag(ELoc, |
| 7375 | AllowArraySection |
| 7376 | ? diag::err_omp_expected_var_name_member_expr_or_array_item |
| 7377 | : diag::err_omp_expected_var_name_member_expr) |
| 7378 | << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; |
| 7379 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7380 | return std::make_pair(nullptr, false); |
| 7381 | } |
| 7382 | return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false); |
| 7383 | } |
| 7384 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7385 | OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, |
| 7386 | SourceLocation StartLoc, |
| 7387 | SourceLocation LParenLoc, |
| 7388 | SourceLocation EndLoc) { |
| 7389 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 7390 | SmallVector<Expr *, 8> PrivateCopies; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7391 | for (auto &RefExpr : VarList) { |
| 7392 | assert(RefExpr && "NULL expr in OpenMP private clause."); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7393 | SourceLocation ELoc; |
| 7394 | SourceRange ERange; |
| 7395 | Expr *SimpleRefExpr = RefExpr; |
| 7396 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7397 | if (Res.second) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7398 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7399 | Vars.push_back(RefExpr); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 7400 | PrivateCopies.push_back(nullptr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7401 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7402 | ValueDecl *D = Res.first; |
| 7403 | if (!D) |
| 7404 | continue; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7405 | |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 7406 | QualType Type = D->getType(); |
| 7407 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7408 | |
| 7409 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 7410 | // A variable that appears in a private clause must not have an incomplete |
| 7411 | // type or a reference type. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 7412 | if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7413 | continue; |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 7414 | Type = Type.getNonReferenceType(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7415 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7416 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 7417 | // in a Construct] |
| 7418 | // Variables with the predetermined data-sharing attributes may not be |
| 7419 | // listed in data-sharing attributes clauses, except for the cases |
| 7420 | // listed below. For these exceptions only, listing a predetermined |
| 7421 | // variable in a data-sharing attribute clause is allowed and overrides |
| 7422 | // the variable's predetermined data-sharing attributes. |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 7423 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7424 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7425 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 7426 | << getOpenMPClauseName(OMPC_private); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 7427 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7428 | continue; |
| 7429 | } |
| 7430 | |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 7431 | auto CurrDir = DSAStack->getCurrentDirective(); |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7432 | // Variably modified types are not supported for tasks. |
Alexey Bataev | 5129d3a | 2015-05-21 09:47:46 +0000 | [diff] [blame] | 7433 | if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 7434 | isOpenMPTaskingDirective(CurrDir)) { |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7435 | Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) |
| 7436 | << getOpenMPClauseName(OMPC_private) << Type |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 7437 | << getOpenMPDirectiveName(CurrDir); |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7438 | bool IsDecl = |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 7439 | !VD || |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7440 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 7441 | Diag(D->getLocation(), |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7442 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 7443 | << D; |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7444 | continue; |
| 7445 | } |
| 7446 | |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 7447 | // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] |
| 7448 | // A list item cannot appear in both a map clause and a data-sharing |
| 7449 | // attribute clause on the same construct |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 7450 | if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel || |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 7451 | CurrDir == OMPD_target_teams || |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 7452 | CurrDir == OMPD_target_teams_distribute || |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 7453 | CurrDir == OMPD_target_teams_distribute_parallel_for || |
Kelvin Li | c4bfc6f | 2017-01-10 04:26:44 +0000 | [diff] [blame] | 7454 | CurrDir == OMPD_target_teams_distribute_parallel_for_simd || |
Kelvin Li | 4101032 | 2017-01-10 05:15:35 +0000 | [diff] [blame^] | 7455 | CurrDir == OMPD_target_parallel_for_simd || |
| 7456 | CurrDir == OMPD_target_parallel_for) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7457 | OpenMPClauseKind ConflictKind; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 7458 | if (DSAStack->checkMappableExprComponentListsForDecl( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 7459 | VD, /*CurrentRegionOnly=*/true, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7460 | [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| 7461 | OpenMPClauseKind WhereFoundClauseKind) -> bool { |
| 7462 | ConflictKind = WhereFoundClauseKind; |
| 7463 | return true; |
| 7464 | })) { |
| 7465 | Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 7466 | << getOpenMPClauseName(OMPC_private) |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7467 | << getOpenMPClauseName(ConflictKind) |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 7468 | << getOpenMPDirectiveName(CurrDir); |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 7469 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
| 7470 | continue; |
| 7471 | } |
| 7472 | } |
| 7473 | |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7474 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] |
| 7475 | // A variable of class type (or array thereof) that appears in a private |
| 7476 | // clause requires an accessible, unambiguous default constructor for the |
| 7477 | // class type. |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 7478 | // Generate helper private variable and initialize it with the default |
| 7479 | // value. The address of the original variable is replaced by the address of |
| 7480 | // the new private variable in CodeGen. This new variable is not added to |
| 7481 | // IdResolver, so the code in the OpenMP region uses original variable for |
| 7482 | // proper diagnostics. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7483 | Type = Type.getUnqualifiedType(); |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 7484 | auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 7485 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 7486 | ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 7487 | if (VDPrivate->isInvalidDecl()) |
| 7488 | continue; |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7489 | auto VDPrivateRefExpr = buildDeclRefExpr( |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 7490 | *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 7491 | |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 7492 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 7493 | if (!VD && !CurContext->isDependentContext()) |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 7494 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 7495 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 7496 | Vars.push_back((VD || CurContext->isDependentContext()) |
| 7497 | ? RefExpr->IgnoreParens() |
| 7498 | : Ref); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 7499 | PrivateCopies.push_back(VDPrivateRefExpr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7500 | } |
| 7501 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7502 | if (Vars.empty()) |
| 7503 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7504 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 7505 | return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, |
| 7506 | PrivateCopies); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 7507 | } |
| 7508 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7509 | namespace { |
| 7510 | class DiagsUninitializedSeveretyRAII { |
| 7511 | private: |
| 7512 | DiagnosticsEngine &Diags; |
| 7513 | SourceLocation SavedLoc; |
| 7514 | bool IsIgnored; |
| 7515 | |
| 7516 | public: |
| 7517 | DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, |
| 7518 | bool IsIgnored) |
| 7519 | : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { |
| 7520 | if (!IsIgnored) { |
| 7521 | Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, |
| 7522 | /*Map*/ diag::Severity::Ignored, Loc); |
| 7523 | } |
| 7524 | } |
| 7525 | ~DiagsUninitializedSeveretyRAII() { |
| 7526 | if (!IsIgnored) |
| 7527 | Diags.popMappings(SavedLoc); |
| 7528 | } |
| 7529 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7530 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7531 | |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7532 | OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, |
| 7533 | SourceLocation StartLoc, |
| 7534 | SourceLocation LParenLoc, |
| 7535 | SourceLocation EndLoc) { |
| 7536 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7537 | SmallVector<Expr *, 8> PrivateCopies; |
| 7538 | SmallVector<Expr *, 8> Inits; |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 7539 | SmallVector<Decl *, 4> ExprCaptures; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 7540 | bool IsImplicitClause = |
| 7541 | StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); |
| 7542 | auto ImplicitClauseLoc = DSAStack->getConstructLoc(); |
| 7543 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7544 | for (auto &RefExpr : VarList) { |
| 7545 | assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7546 | SourceLocation ELoc; |
| 7547 | SourceRange ERange; |
| 7548 | Expr *SimpleRefExpr = RefExpr; |
| 7549 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7550 | if (Res.second) { |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7551 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7552 | Vars.push_back(RefExpr); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7553 | PrivateCopies.push_back(nullptr); |
| 7554 | Inits.push_back(nullptr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7555 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7556 | ValueDecl *D = Res.first; |
| 7557 | if (!D) |
| 7558 | continue; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7559 | |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7560 | ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7561 | QualType Type = D->getType(); |
| 7562 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7563 | |
| 7564 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 7565 | // A variable that appears in a private clause must not have an incomplete |
| 7566 | // type or a reference type. |
| 7567 | if (RequireCompleteType(ELoc, Type, |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7568 | diag::err_omp_firstprivate_incomplete_type)) |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7569 | continue; |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 7570 | Type = Type.getNonReferenceType(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7571 | |
| 7572 | // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] |
| 7573 | // 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] | 7574 | // clause requires an accessible, unambiguous copy constructor for the |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7575 | // class type. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7576 | auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7577 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 7578 | // If an implicit firstprivate variable found it was checked already. |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7579 | DSAStackTy::DSAVarData TopDVar; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 7580 | if (!IsImplicitClause) { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7581 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7582 | TopDVar = DVar; |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7583 | bool IsConstant = ElemType.isConstant(Context); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7584 | // OpenMP [2.4.13, Data-sharing Attribute Clauses] |
| 7585 | // A list item that specifies a given variable may not appear in more |
| 7586 | // than one clause on the same directive, except that a variable may be |
| 7587 | // specified in both firstprivate and lastprivate clauses. |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7588 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 7589 | DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7590 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7591 | << getOpenMPClauseName(DVar.CKind) |
| 7592 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7593 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7594 | continue; |
| 7595 | } |
| 7596 | |
| 7597 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 7598 | // in a Construct] |
| 7599 | // Variables with the predetermined data-sharing attributes may not be |
| 7600 | // listed in data-sharing attributes clauses, except for the cases |
| 7601 | // listed below. For these exceptions only, listing a predetermined |
| 7602 | // variable in a data-sharing attribute clause is allowed and overrides |
| 7603 | // the variable's predetermined data-sharing attributes. |
| 7604 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 7605 | // in a Construct, C/C++, p.2] |
| 7606 | // Variables with const-qualified type having no mutable member may be |
| 7607 | // listed in a firstprivate clause, even if they are static data members. |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7608 | if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7609 | DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { |
| 7610 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7611 | << getOpenMPClauseName(DVar.CKind) |
| 7612 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7613 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7614 | continue; |
| 7615 | } |
| 7616 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 7617 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7618 | // OpenMP [2.9.3.4, Restrictions, p.2] |
| 7619 | // A list item that is private within a parallel region must not appear |
| 7620 | // in a firstprivate clause on a worksharing construct if any of the |
| 7621 | // worksharing regions arising from the worksharing construct ever bind |
| 7622 | // to any of the parallel regions arising from the parallel construct. |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 7623 | if (isOpenMPWorksharingDirective(CurrDir) && |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 7624 | !isOpenMPParallelDirective(CurrDir) && |
| 7625 | !isOpenMPTeamsDirective(CurrDir)) { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7626 | DVar = DSAStack->getImplicitDSA(D, true); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 7627 | if (DVar.CKind != OMPC_shared && |
| 7628 | (isOpenMPParallelDirective(DVar.DKind) || |
| 7629 | DVar.DKind == OMPD_unknown)) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 7630 | Diag(ELoc, diag::err_omp_required_access) |
| 7631 | << getOpenMPClauseName(OMPC_firstprivate) |
| 7632 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7633 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 7634 | continue; |
| 7635 | } |
| 7636 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7637 | // OpenMP [2.9.3.4, Restrictions, p.3] |
| 7638 | // A list item that appears in a reduction clause of a parallel construct |
| 7639 | // must not appear in a firstprivate clause on a worksharing or task |
| 7640 | // construct if any of the worksharing or task regions arising from the |
| 7641 | // worksharing or task construct ever bind to any of the parallel regions |
| 7642 | // arising from the parallel construct. |
| 7643 | // OpenMP [2.9.3.4, Restrictions, p.4] |
| 7644 | // A list item that appears in a reduction clause in worksharing |
| 7645 | // construct must not appear in a firstprivate clause in a task construct |
| 7646 | // encountered during execution of any of the worksharing regions arising |
| 7647 | // from the worksharing construct. |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 7648 | if (isOpenMPTaskingDirective(CurrDir)) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 7649 | DVar = DSAStack->hasInnermostDSA( |
| 7650 | D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, |
| 7651 | [](OpenMPDirectiveKind K) -> bool { |
| 7652 | return isOpenMPParallelDirective(K) || |
| 7653 | isOpenMPWorksharingDirective(K); |
| 7654 | }, |
| 7655 | false); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 7656 | if (DVar.CKind == OMPC_reduction && |
| 7657 | (isOpenMPParallelDirective(DVar.DKind) || |
| 7658 | isOpenMPWorksharingDirective(DVar.DKind))) { |
| 7659 | Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) |
| 7660 | << getOpenMPDirectiveName(DVar.DKind); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7661 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 7662 | continue; |
| 7663 | } |
| 7664 | } |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 7665 | |
| 7666 | // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] |
| 7667 | // A list item that is private within a teams region must not appear in a |
| 7668 | // firstprivate clause on a distribute construct if any of the distribute |
| 7669 | // regions arising from the distribute construct ever bind to any of the |
| 7670 | // teams regions arising from the teams construct. |
| 7671 | // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] |
| 7672 | // A list item that appears in a reduction clause of a teams construct |
| 7673 | // must not appear in a firstprivate clause on a distribute construct if |
| 7674 | // any of the distribute regions arising from the distribute construct |
| 7675 | // ever bind to any of the teams regions arising from the teams construct. |
| 7676 | // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] |
| 7677 | // A list item may appear in a firstprivate or lastprivate clause but not |
| 7678 | // both. |
| 7679 | if (CurrDir == OMPD_distribute) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 7680 | DVar = DSAStack->hasInnermostDSA( |
| 7681 | D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; }, |
| 7682 | [](OpenMPDirectiveKind K) -> bool { |
| 7683 | return isOpenMPTeamsDirective(K); |
| 7684 | }, |
| 7685 | false); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 7686 | if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) { |
| 7687 | Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7688 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 7689 | continue; |
| 7690 | } |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 7691 | DVar = DSAStack->hasInnermostDSA( |
| 7692 | D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, |
| 7693 | [](OpenMPDirectiveKind K) -> bool { |
| 7694 | return isOpenMPTeamsDirective(K); |
| 7695 | }, |
| 7696 | false); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 7697 | if (DVar.CKind == OMPC_reduction && |
| 7698 | isOpenMPTeamsDirective(DVar.DKind)) { |
| 7699 | Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7700 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 7701 | continue; |
| 7702 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7703 | DVar = DSAStack->getTopDSA(D, false); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 7704 | if (DVar.CKind == OMPC_lastprivate) { |
| 7705 | Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7706 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 7707 | continue; |
| 7708 | } |
| 7709 | } |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 7710 | // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] |
| 7711 | // A list item cannot appear in both a map clause and a data-sharing |
| 7712 | // attribute clause on the same construct |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 7713 | if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel || |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 7714 | CurrDir == OMPD_target_teams || |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 7715 | CurrDir == OMPD_target_teams_distribute || |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 7716 | CurrDir == OMPD_target_teams_distribute_parallel_for || |
Kelvin Li | c4bfc6f | 2017-01-10 04:26:44 +0000 | [diff] [blame] | 7717 | CurrDir == OMPD_target_teams_distribute_parallel_for_simd || |
Kelvin Li | 4101032 | 2017-01-10 05:15:35 +0000 | [diff] [blame^] | 7718 | CurrDir == OMPD_target_parallel_for_simd || |
| 7719 | CurrDir == OMPD_target_parallel_for) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7720 | OpenMPClauseKind ConflictKind; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 7721 | if (DSAStack->checkMappableExprComponentListsForDecl( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 7722 | VD, /*CurrentRegionOnly=*/true, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7723 | [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| 7724 | OpenMPClauseKind WhereFoundClauseKind) -> bool { |
| 7725 | ConflictKind = WhereFoundClauseKind; |
| 7726 | return true; |
| 7727 | })) { |
| 7728 | Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 7729 | << getOpenMPClauseName(OMPC_firstprivate) |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7730 | << getOpenMPClauseName(ConflictKind) |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 7731 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 7732 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
| 7733 | continue; |
| 7734 | } |
| 7735 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7736 | } |
| 7737 | |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7738 | // Variably modified types are not supported for tasks. |
Alexey Bataev | 5129d3a | 2015-05-21 09:47:46 +0000 | [diff] [blame] | 7739 | if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && |
Alexey Bataev | 35aaee6 | 2016-04-13 13:36:48 +0000 | [diff] [blame] | 7740 | isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7741 | Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) |
| 7742 | << getOpenMPClauseName(OMPC_firstprivate) << Type |
| 7743 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 7744 | bool IsDecl = |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7745 | !VD || |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7746 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7747 | Diag(D->getLocation(), |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7748 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7749 | << D; |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 7750 | continue; |
| 7751 | } |
| 7752 | |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7753 | Type = Type.getUnqualifiedType(); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7754 | auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 7755 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7756 | // Generate helper private variable and initialize it with the value of the |
| 7757 | // original variable. The address of the original variable is replaced by |
| 7758 | // the address of the new private variable in the CodeGen. This new variable |
| 7759 | // is not added to IdResolver, so the code in the OpenMP region uses |
| 7760 | // original variable for proper diagnostics and variable capturing. |
| 7761 | Expr *VDInitRefExpr = nullptr; |
| 7762 | // For arrays generate initializer for single element and replace it by the |
| 7763 | // original array element in CodeGen. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7764 | if (Type->isArrayType()) { |
| 7765 | auto VDInit = |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7766 | buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7767 | VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7768 | auto Init = DefaultLvalueConversion(VDInitRefExpr).get(); |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7769 | ElemType = ElemType.getUnqualifiedType(); |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7770 | auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 7771 | ".firstprivate.temp"); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 7772 | InitializedEntity Entity = |
| 7773 | InitializedEntity::InitializeVariable(VDInitTemp); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7774 | InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); |
| 7775 | |
| 7776 | InitializationSequence InitSeq(*this, Entity, Kind, Init); |
| 7777 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); |
| 7778 | if (Result.isInvalid()) |
| 7779 | VDPrivate->setInvalidDecl(); |
| 7780 | else |
| 7781 | VDPrivate->setInit(Result.getAs<Expr>()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 7782 | // Remove temp variable declaration. |
| 7783 | Context.Deallocate(VDInitTemp); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7784 | } else { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7785 | auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, |
| 7786 | ".firstprivate.temp"); |
| 7787 | VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), |
| 7788 | RefExpr->getExprLoc()); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 7789 | AddInitializerToDecl(VDPrivate, |
| 7790 | DefaultLvalueConversion(VDInitRefExpr).get(), |
| 7791 | /*DirectInit=*/false, /*TypeMayContainAuto=*/false); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7792 | } |
| 7793 | if (VDPrivate->isInvalidDecl()) { |
| 7794 | if (IsImplicitClause) { |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7795 | Diag(RefExpr->getExprLoc(), |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7796 | diag::note_omp_task_predetermined_firstprivate_here); |
| 7797 | } |
| 7798 | continue; |
| 7799 | } |
| 7800 | CurContext->addDecl(VDPrivate); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 7801 | auto VDPrivateRefExpr = buildDeclRefExpr( |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7802 | *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), |
| 7803 | RefExpr->getExprLoc()); |
| 7804 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 7805 | if (!VD && !CurContext->isDependentContext()) { |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7806 | if (TopDVar.CKind == OMPC_lastprivate) |
| 7807 | Ref = TopDVar.PrivateCopy; |
| 7808 | else { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 7809 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7810 | if (!IsOpenMPCapturedDecl(D)) |
| 7811 | ExprCaptures.push_back(Ref->getDecl()); |
| 7812 | } |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 7813 | } |
Alexey Bataev | d985eda | 2016-02-10 11:29:16 +0000 | [diff] [blame] | 7814 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 7815 | Vars.push_back((VD || CurContext->isDependentContext()) |
| 7816 | ? RefExpr->IgnoreParens() |
| 7817 | : Ref); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 7818 | PrivateCopies.push_back(VDPrivateRefExpr); |
| 7819 | Inits.push_back(VDInitRefExpr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7820 | } |
| 7821 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7822 | if (Vars.empty()) |
| 7823 | return nullptr; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7824 | |
| 7825 | return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 7826 | Vars, PrivateCopies, Inits, |
| 7827 | buildPreInits(Context, ExprCaptures)); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 7828 | } |
| 7829 | |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7830 | OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, |
| 7831 | SourceLocation StartLoc, |
| 7832 | SourceLocation LParenLoc, |
| 7833 | SourceLocation EndLoc) { |
| 7834 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 7835 | SmallVector<Expr *, 8> SrcExprs; |
| 7836 | SmallVector<Expr *, 8> DstExprs; |
| 7837 | SmallVector<Expr *, 8> AssignmentOps; |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7838 | SmallVector<Decl *, 4> ExprCaptures; |
| 7839 | SmallVector<Expr *, 4> ExprPostUpdates; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7840 | for (auto &RefExpr : VarList) { |
| 7841 | assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7842 | SourceLocation ELoc; |
| 7843 | SourceRange ERange; |
| 7844 | Expr *SimpleRefExpr = RefExpr; |
| 7845 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7846 | if (Res.second) { |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7847 | // It will be analyzed later. |
| 7848 | Vars.push_back(RefExpr); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 7849 | SrcExprs.push_back(nullptr); |
| 7850 | DstExprs.push_back(nullptr); |
| 7851 | AssignmentOps.push_back(nullptr); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7852 | } |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7853 | ValueDecl *D = Res.first; |
| 7854 | if (!D) |
| 7855 | continue; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7856 | |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7857 | QualType Type = D->getType(); |
| 7858 | auto *VD = dyn_cast<VarDecl>(D); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7859 | |
| 7860 | // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] |
| 7861 | // A variable that appears in a lastprivate clause must not have an |
| 7862 | // incomplete type or a reference type. |
| 7863 | if (RequireCompleteType(ELoc, Type, |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7864 | diag::err_omp_lastprivate_incomplete_type)) |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7865 | continue; |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 7866 | Type = Type.getNonReferenceType(); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7867 | |
| 7868 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 7869 | // in a Construct] |
| 7870 | // Variables with the predetermined data-sharing attributes may not be |
| 7871 | // listed in data-sharing attributes clauses, except for the cases |
| 7872 | // listed below. |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7873 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7874 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && |
| 7875 | DVar.CKind != OMPC_firstprivate && |
| 7876 | (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { |
| 7877 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 7878 | << getOpenMPClauseName(DVar.CKind) |
| 7879 | << getOpenMPClauseName(OMPC_lastprivate); |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7880 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7881 | continue; |
| 7882 | } |
| 7883 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 7884 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
| 7885 | // OpenMP [2.14.3.5, Restrictions, p.2] |
| 7886 | // A list item that is private within a parallel region, or that appears in |
| 7887 | // the reduction clause of a parallel construct, must not appear in a |
| 7888 | // lastprivate clause on a worksharing construct if any of the corresponding |
| 7889 | // worksharing regions ever binds to any of the corresponding parallel |
| 7890 | // regions. |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 7891 | DSAStackTy::DSAVarData TopDVar = DVar; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 7892 | if (isOpenMPWorksharingDirective(CurrDir) && |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 7893 | !isOpenMPParallelDirective(CurrDir) && |
| 7894 | !isOpenMPTeamsDirective(CurrDir)) { |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7895 | DVar = DSAStack->getImplicitDSA(D, true); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 7896 | if (DVar.CKind != OMPC_shared) { |
| 7897 | Diag(ELoc, diag::err_omp_required_access) |
| 7898 | << getOpenMPClauseName(OMPC_lastprivate) |
| 7899 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7900 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 7901 | continue; |
| 7902 | } |
| 7903 | } |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7904 | |
| 7905 | // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] |
| 7906 | // A list item may appear in a firstprivate or lastprivate clause but not |
| 7907 | // both. |
| 7908 | if (CurrDir == OMPD_distribute) { |
| 7909 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
| 7910 | if (DVar.CKind == OMPC_firstprivate) { |
| 7911 | Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute); |
| 7912 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
| 7913 | continue; |
| 7914 | } |
| 7915 | } |
| 7916 | |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7917 | // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 7918 | // A variable of class type (or array thereof) that appears in a |
| 7919 | // lastprivate clause requires an accessible, unambiguous default |
| 7920 | // constructor for the class type, unless the list item is also specified |
| 7921 | // in a firstprivate clause. |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7922 | // A variable of class type (or array thereof) that appears in a |
| 7923 | // lastprivate clause requires an accessible, unambiguous copy assignment |
| 7924 | // operator for the class type. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 7925 | Type = Context.getBaseElementType(Type).getNonReferenceType(); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7926 | auto *SrcVD = buildVarDecl(*this, ERange.getBegin(), |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 7927 | Type.getUnqualifiedType(), ".lastprivate.src", |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7928 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 7929 | auto *PseudoSrcExpr = |
| 7930 | buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 7931 | auto *DstVD = |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7932 | buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7933 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 7934 | auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 7935 | // For arrays generate assignment operation for single element and replace |
| 7936 | // it by the original array element in CodeGen. |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7937 | auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 7938 | PseudoDstExpr, PseudoSrcExpr); |
| 7939 | if (AssignmentOp.isInvalid()) |
| 7940 | continue; |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7941 | AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 7942 | /*DiscardedValue=*/true); |
| 7943 | if (AssignmentOp.isInvalid()) |
| 7944 | continue; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7945 | |
Alexey Bataev | 74caaf2 | 2016-02-20 04:09:36 +0000 | [diff] [blame] | 7946 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 7947 | if (!VD && !CurContext->isDependentContext()) { |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7948 | if (TopDVar.CKind == OMPC_firstprivate) |
| 7949 | Ref = TopDVar.PrivateCopy; |
| 7950 | else { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 7951 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7952 | if (!IsOpenMPCapturedDecl(D)) |
| 7953 | ExprCaptures.push_back(Ref->getDecl()); |
| 7954 | } |
| 7955 | if (TopDVar.CKind == OMPC_firstprivate || |
| 7956 | (!IsOpenMPCapturedDecl(D) && |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 7957 | Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7958 | ExprResult RefRes = DefaultLvalueConversion(Ref); |
| 7959 | if (!RefRes.isUsable()) |
| 7960 | continue; |
| 7961 | ExprResult PostUpdateRes = |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7962 | BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, |
| 7963 | RefRes.get()); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7964 | if (!PostUpdateRes.isUsable()) |
| 7965 | continue; |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 7966 | ExprPostUpdates.push_back( |
| 7967 | IgnoredValueConversions(PostUpdateRes.get()).get()); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7968 | } |
| 7969 | } |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 7970 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 7971 | Vars.push_back((VD || CurContext->isDependentContext()) |
| 7972 | ? RefExpr->IgnoreParens() |
| 7973 | : Ref); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 7974 | SrcExprs.push_back(PseudoSrcExpr); |
| 7975 | DstExprs.push_back(PseudoDstExpr); |
| 7976 | AssignmentOps.push_back(AssignmentOp.get()); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7977 | } |
| 7978 | |
| 7979 | if (Vars.empty()) |
| 7980 | return nullptr; |
| 7981 | |
| 7982 | return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 7983 | Vars, SrcExprs, DstExprs, AssignmentOps, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 7984 | buildPreInits(Context, ExprCaptures), |
| 7985 | buildPostUpdate(*this, ExprPostUpdates)); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 7986 | } |
| 7987 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 7988 | OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, |
| 7989 | SourceLocation StartLoc, |
| 7990 | SourceLocation LParenLoc, |
| 7991 | SourceLocation EndLoc) { |
| 7992 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 7993 | for (auto &RefExpr : VarList) { |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 7994 | assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 7995 | SourceLocation ELoc; |
| 7996 | SourceRange ERange; |
| 7997 | Expr *SimpleRefExpr = RefExpr; |
| 7998 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 7999 | if (Res.second) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8000 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8001 | Vars.push_back(RefExpr); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8002 | } |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8003 | ValueDecl *D = Res.first; |
| 8004 | if (!D) |
| 8005 | continue; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8006 | |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8007 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8008 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 8009 | // in a Construct] |
| 8010 | // Variables with the predetermined data-sharing attributes may not be |
| 8011 | // listed in data-sharing attributes clauses, except for the cases |
| 8012 | // listed below. For these exceptions only, listing a predetermined |
| 8013 | // variable in a data-sharing attribute clause is allowed and overrides |
| 8014 | // the variable's predetermined data-sharing attributes. |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8015 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8016 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && |
| 8017 | DVar.RefExpr) { |
| 8018 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 8019 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8020 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8021 | continue; |
| 8022 | } |
| 8023 | |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8024 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8025 | if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8026 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); |
Alexey Bataev | b7a34b6 | 2016-02-25 03:59:29 +0000 | [diff] [blame] | 8027 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8028 | Vars.push_back((VD || !Ref || CurContext->isDependentContext()) |
| 8029 | ? RefExpr->IgnoreParens() |
| 8030 | : Ref); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8031 | } |
| 8032 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8033 | if (Vars.empty()) |
| 8034 | return nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 8035 | |
| 8036 | return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 8037 | } |
| 8038 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8039 | namespace { |
| 8040 | class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { |
| 8041 | DSAStackTy *Stack; |
| 8042 | |
| 8043 | public: |
| 8044 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 8045 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 8046 | DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8047 | if (DVar.CKind == OMPC_shared && !DVar.RefExpr) |
| 8048 | return false; |
| 8049 | if (DVar.CKind != OMPC_unknown) |
| 8050 | return true; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 8051 | DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( |
| 8052 | VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, |
| 8053 | false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8054 | if (DVarPrivate.CKind != OMPC_unknown) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8055 | return true; |
| 8056 | return false; |
| 8057 | } |
| 8058 | return false; |
| 8059 | } |
| 8060 | bool VisitStmt(Stmt *S) { |
| 8061 | for (auto Child : S->children()) { |
| 8062 | if (Child && Visit(Child)) |
| 8063 | return true; |
| 8064 | } |
| 8065 | return false; |
| 8066 | } |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 8067 | explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8068 | }; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 8069 | } // namespace |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8070 | |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8071 | namespace { |
| 8072 | // Transform MemberExpression for specified FieldDecl of current class to |
| 8073 | // DeclRefExpr to specified OMPCapturedExprDecl. |
| 8074 | class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { |
| 8075 | typedef TreeTransform<TransformExprToCaptures> BaseTransform; |
| 8076 | ValueDecl *Field; |
| 8077 | DeclRefExpr *CapturedExpr; |
| 8078 | |
| 8079 | public: |
| 8080 | TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) |
| 8081 | : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} |
| 8082 | |
| 8083 | ExprResult TransformMemberExpr(MemberExpr *E) { |
| 8084 | if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && |
| 8085 | E->getMemberDecl() == Field) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8086 | CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8087 | return CapturedExpr; |
| 8088 | } |
| 8089 | return BaseTransform::TransformMemberExpr(E); |
| 8090 | } |
| 8091 | DeclRefExpr *getCapturedExpr() { return CapturedExpr; } |
| 8092 | }; |
| 8093 | } // namespace |
| 8094 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8095 | template <typename T> |
| 8096 | static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups, |
| 8097 | const llvm::function_ref<T(ValueDecl *)> &Gen) { |
| 8098 | for (auto &Set : Lookups) { |
| 8099 | for (auto *D : Set) { |
| 8100 | if (auto Res = Gen(cast<ValueDecl>(D))) |
| 8101 | return Res; |
| 8102 | } |
| 8103 | } |
| 8104 | return T(); |
| 8105 | } |
| 8106 | |
| 8107 | static ExprResult |
| 8108 | buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, |
| 8109 | Scope *S, CXXScopeSpec &ReductionIdScopeSpec, |
| 8110 | const DeclarationNameInfo &ReductionId, QualType Ty, |
| 8111 | CXXCastPath &BasePath, Expr *UnresolvedReduction) { |
| 8112 | if (ReductionIdScopeSpec.isInvalid()) |
| 8113 | return ExprError(); |
| 8114 | SmallVector<UnresolvedSet<8>, 4> Lookups; |
| 8115 | if (S) { |
| 8116 | LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); |
| 8117 | Lookup.suppressDiagnostics(); |
| 8118 | while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { |
| 8119 | auto *D = Lookup.getRepresentativeDecl(); |
| 8120 | do { |
| 8121 | S = S->getParent(); |
| 8122 | } while (S && !S->isDeclScope(D)); |
| 8123 | if (S) |
| 8124 | S = S->getParent(); |
| 8125 | Lookups.push_back(UnresolvedSet<8>()); |
| 8126 | Lookups.back().append(Lookup.begin(), Lookup.end()); |
| 8127 | Lookup.clear(); |
| 8128 | } |
| 8129 | } else if (auto *ULE = |
| 8130 | cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { |
| 8131 | Lookups.push_back(UnresolvedSet<8>()); |
| 8132 | Decl *PrevD = nullptr; |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 8133 | for (auto *D : ULE->decls()) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8134 | if (D == PrevD) |
| 8135 | Lookups.push_back(UnresolvedSet<8>()); |
| 8136 | else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) |
| 8137 | Lookups.back().addDecl(DRD); |
| 8138 | PrevD = D; |
| 8139 | } |
| 8140 | } |
| 8141 | if (Ty->isDependentType() || Ty->isInstantiationDependentType() || |
| 8142 | Ty->containsUnexpandedParameterPack() || |
| 8143 | filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool { |
| 8144 | return !D->isInvalidDecl() && |
| 8145 | (D->getType()->isDependentType() || |
| 8146 | D->getType()->isInstantiationDependentType() || |
| 8147 | D->getType()->containsUnexpandedParameterPack()); |
| 8148 | })) { |
| 8149 | UnresolvedSet<8> ResSet; |
| 8150 | for (auto &Set : Lookups) { |
| 8151 | ResSet.append(Set.begin(), Set.end()); |
| 8152 | // The last item marks the end of all declarations at the specified scope. |
| 8153 | ResSet.addDecl(Set[Set.size() - 1]); |
| 8154 | } |
| 8155 | return UnresolvedLookupExpr::Create( |
| 8156 | SemaRef.Context, /*NamingClass=*/nullptr, |
| 8157 | ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, |
| 8158 | /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); |
| 8159 | } |
| 8160 | if (auto *VD = filterLookupForUDR<ValueDecl *>( |
| 8161 | Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { |
| 8162 | if (!D->isInvalidDecl() && |
| 8163 | SemaRef.Context.hasSameType(D->getType(), Ty)) |
| 8164 | return D; |
| 8165 | return nullptr; |
| 8166 | })) |
| 8167 | return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); |
| 8168 | if (auto *VD = filterLookupForUDR<ValueDecl *>( |
| 8169 | Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { |
| 8170 | if (!D->isInvalidDecl() && |
| 8171 | SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && |
| 8172 | !Ty.isMoreQualifiedThan(D->getType())) |
| 8173 | return D; |
| 8174 | return nullptr; |
| 8175 | })) { |
| 8176 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8177 | /*DetectVirtual=*/false); |
| 8178 | if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { |
| 8179 | if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( |
| 8180 | VD->getType().getUnqualifiedType()))) { |
| 8181 | if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), |
| 8182 | /*DiagID=*/0) != |
| 8183 | Sema::AR_inaccessible) { |
| 8184 | SemaRef.BuildBasePathArray(Paths, BasePath); |
| 8185 | return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); |
| 8186 | } |
| 8187 | } |
| 8188 | } |
| 8189 | } |
| 8190 | if (ReductionIdScopeSpec.isSet()) { |
| 8191 | SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; |
| 8192 | return ExprError(); |
| 8193 | } |
| 8194 | return ExprEmpty(); |
| 8195 | } |
| 8196 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8197 | OMPClause *Sema::ActOnOpenMPReductionClause( |
| 8198 | ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, |
| 8199 | SourceLocation ColonLoc, SourceLocation EndLoc, |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8200 | CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, |
| 8201 | ArrayRef<Expr *> UnresolvedReductions) { |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8202 | auto DN = ReductionId.getName(); |
| 8203 | auto OOK = DN.getCXXOverloadedOperator(); |
| 8204 | BinaryOperatorKind BOK = BO_Comma; |
| 8205 | |
| 8206 | // OpenMP [2.14.3.6, reduction clause] |
| 8207 | // C |
| 8208 | // reduction-identifier is either an identifier or one of the following |
| 8209 | // operators: +, -, *, &, |, ^, && and || |
| 8210 | // C++ |
| 8211 | // reduction-identifier is either an id-expression or one of the following |
| 8212 | // operators: +, -, *, &, |, ^, && and || |
| 8213 | // FIXME: Only 'min' and 'max' identifiers are supported for now. |
| 8214 | switch (OOK) { |
| 8215 | case OO_Plus: |
| 8216 | case OO_Minus: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8217 | BOK = BO_Add; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8218 | break; |
| 8219 | case OO_Star: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8220 | BOK = BO_Mul; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8221 | break; |
| 8222 | case OO_Amp: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8223 | BOK = BO_And; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8224 | break; |
| 8225 | case OO_Pipe: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8226 | BOK = BO_Or; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8227 | break; |
| 8228 | case OO_Caret: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8229 | BOK = BO_Xor; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8230 | break; |
| 8231 | case OO_AmpAmp: |
| 8232 | BOK = BO_LAnd; |
| 8233 | break; |
| 8234 | case OO_PipePipe: |
| 8235 | BOK = BO_LOr; |
| 8236 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8237 | case OO_New: |
| 8238 | case OO_Delete: |
| 8239 | case OO_Array_New: |
| 8240 | case OO_Array_Delete: |
| 8241 | case OO_Slash: |
| 8242 | case OO_Percent: |
| 8243 | case OO_Tilde: |
| 8244 | case OO_Exclaim: |
| 8245 | case OO_Equal: |
| 8246 | case OO_Less: |
| 8247 | case OO_Greater: |
| 8248 | case OO_LessEqual: |
| 8249 | case OO_GreaterEqual: |
| 8250 | case OO_PlusEqual: |
| 8251 | case OO_MinusEqual: |
| 8252 | case OO_StarEqual: |
| 8253 | case OO_SlashEqual: |
| 8254 | case OO_PercentEqual: |
| 8255 | case OO_CaretEqual: |
| 8256 | case OO_AmpEqual: |
| 8257 | case OO_PipeEqual: |
| 8258 | case OO_LessLess: |
| 8259 | case OO_GreaterGreater: |
| 8260 | case OO_LessLessEqual: |
| 8261 | case OO_GreaterGreaterEqual: |
| 8262 | case OO_EqualEqual: |
| 8263 | case OO_ExclaimEqual: |
| 8264 | case OO_PlusPlus: |
| 8265 | case OO_MinusMinus: |
| 8266 | case OO_Comma: |
| 8267 | case OO_ArrowStar: |
| 8268 | case OO_Arrow: |
| 8269 | case OO_Call: |
| 8270 | case OO_Subscript: |
| 8271 | case OO_Conditional: |
Richard Smith | 9be594e | 2015-10-22 05:12:22 +0000 | [diff] [blame] | 8272 | case OO_Coawait: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8273 | case NUM_OVERLOADED_OPERATORS: |
| 8274 | llvm_unreachable("Unexpected reduction identifier"); |
| 8275 | case OO_None: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8276 | if (auto II = DN.getAsIdentifierInfo()) { |
| 8277 | if (II->isStr("max")) |
| 8278 | BOK = BO_GT; |
| 8279 | else if (II->isStr("min")) |
| 8280 | BOK = BO_LT; |
| 8281 | } |
| 8282 | break; |
| 8283 | } |
| 8284 | SourceRange ReductionIdRange; |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8285 | if (ReductionIdScopeSpec.isValid()) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8286 | ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8287 | ReductionIdRange.setEnd(ReductionId.getEndLoc()); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8288 | |
| 8289 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8290 | SmallVector<Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8291 | SmallVector<Expr *, 8> LHSs; |
| 8292 | SmallVector<Expr *, 8> RHSs; |
| 8293 | SmallVector<Expr *, 8> ReductionOps; |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8294 | SmallVector<Decl *, 4> ExprCaptures; |
| 8295 | SmallVector<Expr *, 4> ExprPostUpdates; |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8296 | auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); |
| 8297 | bool FirstIter = true; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8298 | for (auto RefExpr : VarList) { |
| 8299 | assert(RefExpr && "nullptr expr in OpenMP reduction clause."); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8300 | // OpenMP [2.1, C/C++] |
| 8301 | // A list item is a variable or array section, subject to the restrictions |
| 8302 | // specified in Section 2.4 on page 42 and in each of the sections |
| 8303 | // describing clauses and directives for which a list appears. |
| 8304 | // OpenMP [2.14.3.3, Restrictions, p.1] |
| 8305 | // A variable that is part of another variable (as an array or |
| 8306 | // structure element) cannot appear in a private clause. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8307 | if (!FirstIter && IR != ER) |
| 8308 | ++IR; |
| 8309 | FirstIter = false; |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8310 | SourceLocation ELoc; |
| 8311 | SourceRange ERange; |
| 8312 | Expr *SimpleRefExpr = RefExpr; |
| 8313 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, |
| 8314 | /*AllowArraySection=*/true); |
| 8315 | if (Res.second) { |
| 8316 | // It will be analyzed later. |
| 8317 | Vars.push_back(RefExpr); |
| 8318 | Privates.push_back(nullptr); |
| 8319 | LHSs.push_back(nullptr); |
| 8320 | RHSs.push_back(nullptr); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8321 | // Try to find 'declare reduction' corresponding construct before using |
| 8322 | // builtin/overloaded operators. |
| 8323 | QualType Type = Context.DependentTy; |
| 8324 | CXXCastPath BasePath; |
| 8325 | ExprResult DeclareReductionRef = buildDeclareReductionRef( |
| 8326 | *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec, |
| 8327 | ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); |
| 8328 | if (CurContext->isDependentContext() && |
| 8329 | (DeclareReductionRef.isUnset() || |
| 8330 | isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) |
| 8331 | ReductionOps.push_back(DeclareReductionRef.get()); |
| 8332 | else |
| 8333 | ReductionOps.push_back(nullptr); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8334 | } |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8335 | ValueDecl *D = Res.first; |
| 8336 | if (!D) |
| 8337 | continue; |
| 8338 | |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8339 | QualType Type; |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8340 | auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); |
| 8341 | auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); |
| 8342 | if (ASE) |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 8343 | Type = ASE->getType().getNonReferenceType(); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8344 | else if (OASE) { |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8345 | auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); |
| 8346 | if (auto *ATy = BaseType->getAsArrayTypeUnsafe()) |
| 8347 | Type = ATy->getElementType(); |
| 8348 | else |
| 8349 | Type = BaseType->getPointeeType(); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 8350 | Type = Type.getNonReferenceType(); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8351 | } else |
| 8352 | Type = Context.getBaseElementType(D->getType().getNonReferenceType()); |
| 8353 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8354 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8355 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 8356 | // A variable that appears in a private clause must not have an incomplete |
| 8357 | // type or a reference type. |
| 8358 | if (RequireCompleteType(ELoc, Type, |
| 8359 | diag::err_omp_reduction_incomplete_type)) |
| 8360 | continue; |
| 8361 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8362 | // A list item that appears in a reduction clause must not be |
| 8363 | // const-qualified. |
| 8364 | if (Type.getNonReferenceType().isConstant(Context)) { |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8365 | Diag(ELoc, diag::err_omp_const_reduction_list_item) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8366 | << getOpenMPClauseName(OMPC_reduction) << Type << ERange; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8367 | if (!ASE && !OASE) { |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8368 | bool IsDecl = !VD || |
| 8369 | VD->isThisDeclarationADefinition(Context) == |
| 8370 | VarDecl::DeclarationOnly; |
| 8371 | Diag(D->getLocation(), |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8372 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8373 | << D; |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8374 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8375 | continue; |
| 8376 | } |
| 8377 | // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] |
| 8378 | // If a list-item is a reference type then it must bind to the same object |
| 8379 | // for all threads of the team. |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8380 | if (!ASE && !OASE && VD) { |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8381 | VarDecl *VDDef = VD->getDefinition(); |
David Sheinkman | 9258999 | 2016-10-04 14:41:36 +0000 | [diff] [blame] | 8382 | if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8383 | DSARefChecker Check(DSAStack); |
| 8384 | if (Check.Visit(VDDef->getInit())) { |
| 8385 | Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; |
| 8386 | Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; |
| 8387 | continue; |
| 8388 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8389 | } |
| 8390 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8391 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8392 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 8393 | // in a Construct] |
| 8394 | // Variables with the predetermined data-sharing attributes may not be |
| 8395 | // listed in data-sharing attributes clauses, except for the cases |
| 8396 | // listed below. For these exceptions only, listing a predetermined |
| 8397 | // variable in a data-sharing attribute clause is allowed and overrides |
| 8398 | // the variable's predetermined data-sharing attributes. |
| 8399 | // OpenMP [2.14.3.6, Restrictions, p.3] |
| 8400 | // Any number of reduction clauses can be specified on the directive, |
| 8401 | // but a list item can appear only once in the reduction clauses for that |
| 8402 | // directive. |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 8403 | DSAStackTy::DSAVarData DVar; |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8404 | DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8405 | if (DVar.CKind == OMPC_reduction) { |
| 8406 | Diag(ELoc, diag::err_omp_once_referenced) |
| 8407 | << getOpenMPClauseName(OMPC_reduction); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8408 | if (DVar.RefExpr) |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8409 | Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8410 | } else if (DVar.CKind != OMPC_unknown) { |
| 8411 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 8412 | << getOpenMPClauseName(DVar.CKind) |
| 8413 | << getOpenMPClauseName(OMPC_reduction); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8414 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8415 | continue; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8416 | } |
| 8417 | |
| 8418 | // OpenMP [2.14.3.6, Restrictions, p.1] |
| 8419 | // A list item that appears in a reduction clause of a worksharing |
| 8420 | // construct must be shared in the parallel regions to which any of the |
| 8421 | // worksharing regions arising from the worksharing construct bind. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8422 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
| 8423 | if (isOpenMPWorksharingDirective(CurrDir) && |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 8424 | !isOpenMPParallelDirective(CurrDir) && |
| 8425 | !isOpenMPTeamsDirective(CurrDir)) { |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8426 | DVar = DSAStack->getImplicitDSA(D, true); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8427 | if (DVar.CKind != OMPC_shared) { |
| 8428 | Diag(ELoc, diag::err_omp_required_access) |
| 8429 | << getOpenMPClauseName(OMPC_reduction) |
| 8430 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8431 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8432 | continue; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 8433 | } |
| 8434 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8435 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8436 | // Try to find 'declare reduction' corresponding construct before using |
| 8437 | // builtin/overloaded operators. |
| 8438 | CXXCastPath BasePath; |
| 8439 | ExprResult DeclareReductionRef = buildDeclareReductionRef( |
| 8440 | *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec, |
| 8441 | ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); |
| 8442 | if (DeclareReductionRef.isInvalid()) |
| 8443 | continue; |
| 8444 | if (CurContext->isDependentContext() && |
| 8445 | (DeclareReductionRef.isUnset() || |
| 8446 | isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { |
| 8447 | Vars.push_back(RefExpr); |
| 8448 | Privates.push_back(nullptr); |
| 8449 | LHSs.push_back(nullptr); |
| 8450 | RHSs.push_back(nullptr); |
| 8451 | ReductionOps.push_back(DeclareReductionRef.get()); |
| 8452 | continue; |
| 8453 | } |
| 8454 | if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { |
| 8455 | // Not allowed reduction identifier is found. |
| 8456 | Diag(ReductionId.getLocStart(), |
| 8457 | diag::err_omp_unknown_reduction_identifier) |
| 8458 | << Type << ReductionIdRange; |
| 8459 | continue; |
| 8460 | } |
| 8461 | |
| 8462 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
| 8463 | // The type of a list item that appears in a reduction clause must be valid |
| 8464 | // for the reduction-identifier. For a max or min reduction in C, the type |
| 8465 | // of the list item must be an allowed arithmetic data type: char, int, |
| 8466 | // float, double, or _Bool, possibly modified with long, short, signed, or |
| 8467 | // unsigned. For a max or min reduction in C++, the type of the list item |
| 8468 | // must be an allowed arithmetic data type: char, wchar_t, int, float, |
| 8469 | // double, or bool, possibly modified with long, short, signed, or unsigned. |
| 8470 | if (DeclareReductionRef.isUnset()) { |
| 8471 | if ((BOK == BO_GT || BOK == BO_LT) && |
| 8472 | !(Type->isScalarType() || |
| 8473 | (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { |
| 8474 | Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) |
| 8475 | << getLangOpts().CPlusPlus; |
| 8476 | if (!ASE && !OASE) { |
| 8477 | bool IsDecl = !VD || |
| 8478 | VD->isThisDeclarationADefinition(Context) == |
| 8479 | VarDecl::DeclarationOnly; |
| 8480 | Diag(D->getLocation(), |
| 8481 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 8482 | << D; |
| 8483 | } |
| 8484 | continue; |
| 8485 | } |
| 8486 | if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && |
| 8487 | !getLangOpts().CPlusPlus && Type->isFloatingType()) { |
| 8488 | Diag(ELoc, diag::err_omp_clause_floating_type_arg); |
| 8489 | if (!ASE && !OASE) { |
| 8490 | bool IsDecl = !VD || |
| 8491 | VD->isThisDeclarationADefinition(Context) == |
| 8492 | VarDecl::DeclarationOnly; |
| 8493 | Diag(D->getLocation(), |
| 8494 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 8495 | << D; |
| 8496 | } |
| 8497 | continue; |
| 8498 | } |
| 8499 | } |
| 8500 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8501 | Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8502 | auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs", |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8503 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 8504 | auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 8505 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8506 | auto PrivateTy = Type; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 8507 | if (OASE || |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8508 | (!ASE && |
| 8509 | D->getType().getNonReferenceType()->isVariablyModifiedType())) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 8510 | // For arrays/array sections only: |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8511 | // Create pseudo array type for private copy. The size for this array will |
| 8512 | // be generated during codegen. |
| 8513 | // For array subscripts or single variables Private Ty is the same as Type |
| 8514 | // (type of the variable or single array element). |
| 8515 | PrivateTy = Context.getVariableArrayType( |
| 8516 | Type, new (Context) OpaqueValueExpr(SourceLocation(), |
| 8517 | Context.getSizeType(), VK_RValue), |
| 8518 | ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8519 | } else if (!ASE && !OASE && |
| 8520 | Context.getAsArrayType(D->getType().getNonReferenceType())) |
| 8521 | PrivateTy = D->getType().getNonReferenceType(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8522 | // Private copy. |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8523 | auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(), |
| 8524 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8525 | // Add initializer for private variable. |
| 8526 | Expr *Init = nullptr; |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8527 | auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc); |
| 8528 | auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc); |
| 8529 | if (DeclareReductionRef.isUsable()) { |
| 8530 | auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); |
| 8531 | auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); |
| 8532 | if (DRD->getInitializer()) { |
| 8533 | Init = DRDRef; |
| 8534 | RHSVD->setInit(DRDRef); |
| 8535 | RHSVD->setInitStyle(VarDecl::CallInit); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8536 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8537 | } else { |
| 8538 | switch (BOK) { |
| 8539 | case BO_Add: |
| 8540 | case BO_Xor: |
| 8541 | case BO_Or: |
| 8542 | case BO_LOr: |
| 8543 | // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. |
| 8544 | if (Type->isScalarType() || Type->isAnyComplexType()) |
| 8545 | Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get(); |
| 8546 | break; |
| 8547 | case BO_Mul: |
| 8548 | case BO_LAnd: |
| 8549 | if (Type->isScalarType() || Type->isAnyComplexType()) { |
| 8550 | // '*' and '&&' reduction ops - initializer is '1'. |
| 8551 | Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get(); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8552 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8553 | break; |
| 8554 | case BO_And: { |
| 8555 | // '&' reduction op - initializer is '~0'. |
| 8556 | QualType OrigType = Type; |
| 8557 | if (auto *ComplexTy = OrigType->getAs<ComplexType>()) |
| 8558 | Type = ComplexTy->getElementType(); |
| 8559 | if (Type->isRealFloatingType()) { |
| 8560 | llvm::APFloat InitValue = |
| 8561 | llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), |
| 8562 | /*isIEEE=*/true); |
| 8563 | Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, |
| 8564 | Type, ELoc); |
| 8565 | } else if (Type->isScalarType()) { |
| 8566 | auto Size = Context.getTypeSize(Type); |
| 8567 | QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); |
| 8568 | llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); |
| 8569 | Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); |
| 8570 | } |
| 8571 | if (Init && OrigType->isAnyComplexType()) { |
| 8572 | // Init = 0xFFFF + 0xFFFFi; |
| 8573 | auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); |
| 8574 | Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); |
| 8575 | } |
| 8576 | Type = OrigType; |
| 8577 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8578 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8579 | case BO_LT: |
| 8580 | case BO_GT: { |
| 8581 | // 'min' reduction op - initializer is 'Largest representable number in |
| 8582 | // the reduction list item type'. |
| 8583 | // 'max' reduction op - initializer is 'Least representable number in |
| 8584 | // the reduction list item type'. |
| 8585 | if (Type->isIntegerType() || Type->isPointerType()) { |
| 8586 | bool IsSigned = Type->hasSignedIntegerRepresentation(); |
| 8587 | auto Size = Context.getTypeSize(Type); |
| 8588 | QualType IntTy = |
| 8589 | Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); |
| 8590 | llvm::APInt InitValue = |
| 8591 | (BOK != BO_LT) |
| 8592 | ? IsSigned ? llvm::APInt::getSignedMinValue(Size) |
| 8593 | : llvm::APInt::getMinValue(Size) |
| 8594 | : IsSigned ? llvm::APInt::getSignedMaxValue(Size) |
| 8595 | : llvm::APInt::getMaxValue(Size); |
| 8596 | Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); |
| 8597 | if (Type->isPointerType()) { |
| 8598 | // Cast to pointer type. |
| 8599 | auto CastExpr = BuildCStyleCastExpr( |
| 8600 | SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc), |
| 8601 | SourceLocation(), Init); |
| 8602 | if (CastExpr.isInvalid()) |
| 8603 | continue; |
| 8604 | Init = CastExpr.get(); |
| 8605 | } |
| 8606 | } else if (Type->isRealFloatingType()) { |
| 8607 | llvm::APFloat InitValue = llvm::APFloat::getLargest( |
| 8608 | Context.getFloatTypeSemantics(Type), BOK != BO_LT); |
| 8609 | Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, |
| 8610 | Type, ELoc); |
| 8611 | } |
| 8612 | break; |
| 8613 | } |
| 8614 | case BO_PtrMemD: |
| 8615 | case BO_PtrMemI: |
| 8616 | case BO_MulAssign: |
| 8617 | case BO_Div: |
| 8618 | case BO_Rem: |
| 8619 | case BO_Sub: |
| 8620 | case BO_Shl: |
| 8621 | case BO_Shr: |
| 8622 | case BO_LE: |
| 8623 | case BO_GE: |
| 8624 | case BO_EQ: |
| 8625 | case BO_NE: |
| 8626 | case BO_AndAssign: |
| 8627 | case BO_XorAssign: |
| 8628 | case BO_OrAssign: |
| 8629 | case BO_Assign: |
| 8630 | case BO_AddAssign: |
| 8631 | case BO_SubAssign: |
| 8632 | case BO_DivAssign: |
| 8633 | case BO_RemAssign: |
| 8634 | case BO_ShlAssign: |
| 8635 | case BO_ShrAssign: |
| 8636 | case BO_Comma: |
| 8637 | llvm_unreachable("Unexpected reduction operation"); |
| 8638 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8639 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8640 | if (Init && DeclareReductionRef.isUnset()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8641 | AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false, |
| 8642 | /*TypeMayContainAuto=*/false); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8643 | } else if (!Init) |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8644 | ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8645 | if (RHSVD->isInvalidDecl()) |
| 8646 | continue; |
| 8647 | if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8648 | Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type |
| 8649 | << ReductionIdRange; |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8650 | bool IsDecl = |
| 8651 | !VD || |
| 8652 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 8653 | Diag(D->getLocation(), |
| 8654 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 8655 | << D; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8656 | continue; |
| 8657 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8658 | // Store initializer for single element in private copy. Will be used during |
| 8659 | // codegen. |
| 8660 | PrivateVD->setInit(RHSVD->getInit()); |
| 8661 | PrivateVD->setInitStyle(RHSVD->getInitStyle()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8662 | auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8663 | ExprResult ReductionOp; |
| 8664 | if (DeclareReductionRef.isUsable()) { |
| 8665 | QualType RedTy = DeclareReductionRef.get()->getType(); |
| 8666 | QualType PtrRedTy = Context.getPointerType(RedTy); |
| 8667 | ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); |
| 8668 | ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); |
| 8669 | if (!BasePath.empty()) { |
| 8670 | LHS = DefaultLvalueConversion(LHS.get()); |
| 8671 | RHS = DefaultLvalueConversion(RHS.get()); |
| 8672 | LHS = ImplicitCastExpr::Create(Context, PtrRedTy, |
| 8673 | CK_UncheckedDerivedToBase, LHS.get(), |
| 8674 | &BasePath, LHS.get()->getValueKind()); |
| 8675 | RHS = ImplicitCastExpr::Create(Context, PtrRedTy, |
| 8676 | CK_UncheckedDerivedToBase, RHS.get(), |
| 8677 | &BasePath, RHS.get()->getValueKind()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8678 | } |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 8679 | FunctionProtoType::ExtProtoInfo EPI; |
| 8680 | QualType Params[] = {PtrRedTy, PtrRedTy}; |
| 8681 | QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); |
| 8682 | auto *OVE = new (Context) OpaqueValueExpr( |
| 8683 | ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, |
| 8684 | DefaultLvalueConversion(DeclareReductionRef.get()).get()); |
| 8685 | Expr *Args[] = {LHS.get(), RHS.get()}; |
| 8686 | ReductionOp = new (Context) |
| 8687 | CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); |
| 8688 | } else { |
| 8689 | ReductionOp = BuildBinOp(DSAStack->getCurScope(), |
| 8690 | ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE); |
| 8691 | if (ReductionOp.isUsable()) { |
| 8692 | if (BOK != BO_LT && BOK != BO_GT) { |
| 8693 | ReductionOp = |
| 8694 | BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), |
| 8695 | BO_Assign, LHSDRE, ReductionOp.get()); |
| 8696 | } else { |
| 8697 | auto *ConditionalOp = new (Context) ConditionalOperator( |
| 8698 | ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(), |
| 8699 | RHSDRE, Type, VK_LValue, OK_Ordinary); |
| 8700 | ReductionOp = |
| 8701 | BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), |
| 8702 | BO_Assign, LHSDRE, ConditionalOp); |
| 8703 | } |
| 8704 | ReductionOp = ActOnFinishFullExpr(ReductionOp.get()); |
| 8705 | } |
| 8706 | if (ReductionOp.isInvalid()) |
| 8707 | continue; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8708 | } |
| 8709 | |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8710 | DeclRefExpr *Ref = nullptr; |
| 8711 | Expr *VarsExpr = RefExpr->IgnoreParens(); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8712 | if (!VD && !CurContext->isDependentContext()) { |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8713 | if (ASE || OASE) { |
| 8714 | TransformExprToCaptures RebuildToCapture(*this, D); |
| 8715 | VarsExpr = |
| 8716 | RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); |
| 8717 | Ref = RebuildToCapture.getCapturedExpr(); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8718 | } else { |
| 8719 | VarsExpr = Ref = |
| 8720 | buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 8721 | } |
| 8722 | if (!IsOpenMPCapturedDecl(D)) { |
| 8723 | ExprCaptures.push_back(Ref->getDecl()); |
| 8724 | if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { |
| 8725 | ExprResult RefRes = DefaultLvalueConversion(Ref); |
| 8726 | if (!RefRes.isUsable()) |
| 8727 | continue; |
| 8728 | ExprResult PostUpdateRes = |
| 8729 | BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, |
| 8730 | SimpleRefExpr, RefRes.get()); |
| 8731 | if (!PostUpdateRes.isUsable()) |
| 8732 | continue; |
| 8733 | ExprPostUpdates.push_back( |
| 8734 | IgnoredValueConversions(PostUpdateRes.get()).get()); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8735 | } |
| 8736 | } |
Alexey Bataev | 60da77e | 2016-02-29 05:54:20 +0000 | [diff] [blame] | 8737 | } |
| 8738 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); |
| 8739 | Vars.push_back(VarsExpr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8740 | Privates.push_back(PrivateDRE); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 8741 | LHSs.push_back(LHSDRE); |
| 8742 | RHSs.push_back(RHSDRE); |
| 8743 | ReductionOps.push_back(ReductionOp.get()); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8744 | } |
| 8745 | |
| 8746 | if (Vars.empty()) |
| 8747 | return nullptr; |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 8748 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8749 | return OMPReductionClause::Create( |
| 8750 | Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 8751 | ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 8752 | LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures), |
| 8753 | buildPostUpdate(*this, ExprPostUpdates)); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 8754 | } |
| 8755 | |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 8756 | bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, |
| 8757 | SourceLocation LinLoc) { |
| 8758 | if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || |
| 8759 | LinKind == OMPC_LINEAR_unknown) { |
| 8760 | Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; |
| 8761 | return true; |
| 8762 | } |
| 8763 | return false; |
| 8764 | } |
| 8765 | |
| 8766 | bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc, |
| 8767 | OpenMPLinearClauseKind LinKind, |
| 8768 | QualType Type) { |
| 8769 | auto *VD = dyn_cast_or_null<VarDecl>(D); |
| 8770 | // A variable must not have an incomplete type or a reference type. |
| 8771 | if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) |
| 8772 | return true; |
| 8773 | if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && |
| 8774 | !Type->isReferenceType()) { |
| 8775 | Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) |
| 8776 | << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); |
| 8777 | return true; |
| 8778 | } |
| 8779 | Type = Type.getNonReferenceType(); |
| 8780 | |
| 8781 | // A list item must not be const-qualified. |
| 8782 | if (Type.isConstant(Context)) { |
| 8783 | Diag(ELoc, diag::err_omp_const_variable) |
| 8784 | << getOpenMPClauseName(OMPC_linear); |
| 8785 | if (D) { |
| 8786 | bool IsDecl = |
| 8787 | !VD || |
| 8788 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 8789 | Diag(D->getLocation(), |
| 8790 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 8791 | << D; |
| 8792 | } |
| 8793 | return true; |
| 8794 | } |
| 8795 | |
| 8796 | // A list item must be of integral or pointer type. |
| 8797 | Type = Type.getUnqualifiedType().getCanonicalType(); |
| 8798 | const auto *Ty = Type.getTypePtrOrNull(); |
| 8799 | if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && |
| 8800 | !Ty->isPointerType())) { |
| 8801 | Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; |
| 8802 | if (D) { |
| 8803 | bool IsDecl = |
| 8804 | !VD || |
| 8805 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 8806 | Diag(D->getLocation(), |
| 8807 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 8808 | << D; |
| 8809 | } |
| 8810 | return true; |
| 8811 | } |
| 8812 | return false; |
| 8813 | } |
| 8814 | |
Alexey Bataev | 182227b | 2015-08-20 10:54:39 +0000 | [diff] [blame] | 8815 | OMPClause *Sema::ActOnOpenMPLinearClause( |
| 8816 | ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, |
| 8817 | SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, |
| 8818 | SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8819 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 8820 | SmallVector<Expr *, 8> Privates; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8821 | SmallVector<Expr *, 8> Inits; |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 8822 | SmallVector<Decl *, 4> ExprCaptures; |
| 8823 | SmallVector<Expr *, 4> ExprPostUpdates; |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 8824 | if (CheckOpenMPLinearModifier(LinKind, LinLoc)) |
Alexey Bataev | 182227b | 2015-08-20 10:54:39 +0000 | [diff] [blame] | 8825 | LinKind = OMPC_LINEAR_val; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8826 | for (auto &RefExpr : VarList) { |
| 8827 | assert(RefExpr && "NULL expr in OpenMP linear clause."); |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8828 | SourceLocation ELoc; |
| 8829 | SourceRange ERange; |
| 8830 | Expr *SimpleRefExpr = RefExpr; |
| 8831 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, |
| 8832 | /*AllowArraySection=*/false); |
| 8833 | if (Res.second) { |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8834 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 8835 | Vars.push_back(RefExpr); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 8836 | Privates.push_back(nullptr); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8837 | Inits.push_back(nullptr); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8838 | } |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8839 | ValueDecl *D = Res.first; |
| 8840 | if (!D) |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8841 | continue; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8842 | |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8843 | QualType Type = D->getType(); |
| 8844 | auto *VD = dyn_cast<VarDecl>(D); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8845 | |
| 8846 | // OpenMP [2.14.3.7, linear clause] |
| 8847 | // A list-item cannot appear in more than one linear clause. |
| 8848 | // A list-item that appears in a linear clause cannot appear in any |
| 8849 | // other data-sharing attribute clause. |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8850 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8851 | if (DVar.RefExpr) { |
| 8852 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 8853 | << getOpenMPClauseName(OMPC_linear); |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8854 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8855 | continue; |
| 8856 | } |
| 8857 | |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 8858 | if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8859 | continue; |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 8860 | Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8861 | |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 8862 | // Build private copy of original var. |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8863 | auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 8864 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 8865 | auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8866 | // Build var to save initial value. |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8867 | VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 8868 | Expr *InitExpr; |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8869 | DeclRefExpr *Ref = nullptr; |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8870 | if (!VD && !CurContext->isDependentContext()) { |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 8871 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); |
| 8872 | if (!IsOpenMPCapturedDecl(D)) { |
| 8873 | ExprCaptures.push_back(Ref->getDecl()); |
| 8874 | if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { |
| 8875 | ExprResult RefRes = DefaultLvalueConversion(Ref); |
| 8876 | if (!RefRes.isUsable()) |
| 8877 | continue; |
| 8878 | ExprResult PostUpdateRes = |
| 8879 | BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, |
| 8880 | SimpleRefExpr, RefRes.get()); |
| 8881 | if (!PostUpdateRes.isUsable()) |
| 8882 | continue; |
| 8883 | ExprPostUpdates.push_back( |
| 8884 | IgnoredValueConversions(PostUpdateRes.get()).get()); |
| 8885 | } |
| 8886 | } |
| 8887 | } |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 8888 | if (LinKind == OMPC_LINEAR_uval) |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8889 | InitExpr = VD ? VD->getInit() : SimpleRefExpr; |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 8890 | else |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8891 | InitExpr = VD ? SimpleRefExpr : Ref; |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 8892 | AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 8893 | /*DirectInit=*/false, /*TypeMayContainAuto=*/false); |
| 8894 | auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); |
| 8895 | |
| 8896 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); |
Alexey Bataev | be8b8b5 | 2016-07-07 11:04:06 +0000 | [diff] [blame] | 8897 | Vars.push_back((VD || CurContext->isDependentContext()) |
| 8898 | ? RefExpr->IgnoreParens() |
| 8899 | : Ref); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 8900 | Privates.push_back(PrivateRef); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8901 | Inits.push_back(InitRef); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8902 | } |
| 8903 | |
| 8904 | if (Vars.empty()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 8905 | return nullptr; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8906 | |
| 8907 | Expr *StepExpr = Step; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8908 | Expr *CalcStepExpr = nullptr; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8909 | if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && |
| 8910 | !Step->isInstantiationDependent() && |
| 8911 | !Step->containsUnexpandedParameterPack()) { |
| 8912 | SourceLocation StepLoc = Step->getLocStart(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 8913 | ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8914 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 8915 | return nullptr; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 8916 | StepExpr = Val.get(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8917 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8918 | // Build var to save the step value. |
| 8919 | VarDecl *SaveVar = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 8920 | buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8921 | ExprResult SaveRef = |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 8922 | buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8923 | ExprResult CalcStep = |
| 8924 | BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 8925 | CalcStep = ActOnFinishFullExpr(CalcStep.get()); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8926 | |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8927 | // Warn about zero linear step (it would be probably better specified as |
| 8928 | // making corresponding variables 'const'). |
| 8929 | llvm::APSInt Result; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8930 | bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); |
| 8931 | if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8932 | Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] |
| 8933 | << (Vars.size() > 1); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8934 | if (!IsConstant && CalcStep.isUsable()) { |
| 8935 | // Calculate the step beforehand instead of doing this on each iteration. |
| 8936 | // (This is not used if the number of iterations may be kfold-ed). |
| 8937 | CalcStepExpr = CalcStep.get(); |
| 8938 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 8939 | } |
| 8940 | |
Alexey Bataev | 182227b | 2015-08-20 10:54:39 +0000 | [diff] [blame] | 8941 | return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, |
| 8942 | ColonLoc, EndLoc, Vars, Privates, Inits, |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 8943 | StepExpr, CalcStepExpr, |
| 8944 | buildPreInits(Context, ExprCaptures), |
| 8945 | buildPostUpdate(*this, ExprPostUpdates)); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8946 | } |
| 8947 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 8948 | static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, |
| 8949 | Expr *NumIterations, Sema &SemaRef, |
| 8950 | Scope *S, DSAStackTy *Stack) { |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8951 | // Walk the vars and build update/final expressions for the CodeGen. |
| 8952 | SmallVector<Expr *, 8> Updates; |
| 8953 | SmallVector<Expr *, 8> Finals; |
| 8954 | Expr *Step = Clause.getStep(); |
| 8955 | Expr *CalcStep = Clause.getCalcStep(); |
| 8956 | // OpenMP [2.14.3.7, linear clause] |
| 8957 | // If linear-step is not specified it is assumed to be 1. |
| 8958 | if (Step == nullptr) |
| 8959 | Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 8960 | else if (CalcStep) { |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8961 | Step = cast<BinaryOperator>(CalcStep)->getLHS(); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 8962 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8963 | bool HasErrors = false; |
| 8964 | auto CurInit = Clause.inits().begin(); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 8965 | auto CurPrivate = Clause.privates().begin(); |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 8966 | auto LinKind = Clause.getModifier(); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8967 | for (auto &RefExpr : Clause.varlists()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 8968 | SourceLocation ELoc; |
| 8969 | SourceRange ERange; |
| 8970 | Expr *SimpleRefExpr = RefExpr; |
| 8971 | auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange, |
| 8972 | /*AllowArraySection=*/false); |
| 8973 | ValueDecl *D = Res.first; |
| 8974 | if (Res.second || !D) { |
| 8975 | Updates.push_back(nullptr); |
| 8976 | Finals.push_back(nullptr); |
| 8977 | HasErrors = true; |
| 8978 | continue; |
| 8979 | } |
| 8980 | if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) { |
| 8981 | D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts()) |
| 8982 | ->getMemberDecl(); |
| 8983 | } |
| 8984 | auto &&Info = Stack->isLoopControlVariable(D); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8985 | Expr *InitExpr = *CurInit; |
| 8986 | |
| 8987 | // Build privatized reference to the current linear var. |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 8988 | auto *DE = cast<DeclRefExpr>(SimpleRefExpr); |
Alexey Bataev | 84cfb1d | 2015-08-21 06:41:23 +0000 | [diff] [blame] | 8989 | Expr *CapturedRef; |
| 8990 | if (LinKind == OMPC_LINEAR_uval) |
| 8991 | CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); |
| 8992 | else |
| 8993 | CapturedRef = |
| 8994 | buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), |
| 8995 | DE->getType().getUnqualifiedType(), DE->getExprLoc(), |
| 8996 | /*RefersToCapture=*/true); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 8997 | |
| 8998 | // Build update: Var = InitExpr + IV * Step |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 8999 | ExprResult Update; |
| 9000 | if (!Info.first) { |
| 9001 | Update = |
| 9002 | BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, |
| 9003 | InitExpr, IV, Step, /* Subtract */ false); |
| 9004 | } else |
| 9005 | Update = *CurPrivate; |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 9006 | Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(), |
| 9007 | /*DiscardedValue=*/true); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9008 | |
| 9009 | // Build final: Var = InitExpr + NumIterations * Step |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 9010 | ExprResult Final; |
| 9011 | if (!Info.first) { |
| 9012 | Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, |
| 9013 | InitExpr, NumIterations, Step, |
| 9014 | /* Subtract */ false); |
| 9015 | } else |
| 9016 | Final = *CurPrivate; |
Alexey Bataev | 6b8046a | 2015-09-03 07:23:48 +0000 | [diff] [blame] | 9017 | Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(), |
| 9018 | /*DiscardedValue=*/true); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 9019 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9020 | if (!Update.isUsable() || !Final.isUsable()) { |
| 9021 | Updates.push_back(nullptr); |
| 9022 | Finals.push_back(nullptr); |
| 9023 | HasErrors = true; |
| 9024 | } else { |
| 9025 | Updates.push_back(Update.get()); |
| 9026 | Finals.push_back(Final.get()); |
| 9027 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 9028 | ++CurInit; |
| 9029 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 9030 | } |
| 9031 | Clause.setUpdates(Updates); |
| 9032 | Clause.setFinals(Finals); |
| 9033 | return HasErrors; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 9034 | } |
| 9035 | |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9036 | OMPClause *Sema::ActOnOpenMPAlignedClause( |
| 9037 | ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, |
| 9038 | SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { |
| 9039 | |
| 9040 | SmallVector<Expr *, 8> Vars; |
| 9041 | for (auto &RefExpr : VarList) { |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9042 | assert(RefExpr && "NULL expr in OpenMP linear clause."); |
| 9043 | SourceLocation ELoc; |
| 9044 | SourceRange ERange; |
| 9045 | Expr *SimpleRefExpr = RefExpr; |
| 9046 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, |
| 9047 | /*AllowArraySection=*/false); |
| 9048 | if (Res.second) { |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9049 | // It will be analyzed later. |
| 9050 | Vars.push_back(RefExpr); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9051 | } |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9052 | ValueDecl *D = Res.first; |
| 9053 | if (!D) |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9054 | continue; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9055 | |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9056 | QualType QType = D->getType(); |
| 9057 | auto *VD = dyn_cast<VarDecl>(D); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9058 | |
| 9059 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 9060 | // The type of list items appearing in the aligned clause must be |
| 9061 | // array, pointer, reference to array, or reference to pointer. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 9062 | QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9063 | const Type *Ty = QType.getTypePtrOrNull(); |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9064 | if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9065 | Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9066 | << QType << getLangOpts().CPlusPlus << ERange; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9067 | bool IsDecl = |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9068 | !VD || |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9069 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9070 | Diag(D->getLocation(), |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9071 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9072 | << D; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9073 | continue; |
| 9074 | } |
| 9075 | |
| 9076 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 9077 | // A list-item cannot appear in more than one aligned clause. |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9078 | if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 9079 | Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9080 | Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) |
| 9081 | << getOpenMPClauseName(OMPC_aligned); |
| 9082 | continue; |
| 9083 | } |
| 9084 | |
Alexey Bataev | 1efd166 | 2016-03-29 10:59:56 +0000 | [diff] [blame] | 9085 | DeclRefExpr *Ref = nullptr; |
| 9086 | if (!VD && IsOpenMPCapturedDecl(D)) |
| 9087 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); |
| 9088 | Vars.push_back(DefaultFunctionArrayConversion( |
| 9089 | (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) |
| 9090 | .get()); |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 9091 | } |
| 9092 | |
| 9093 | // OpenMP [2.8.1, simd construct, Description] |
| 9094 | // The parameter of the aligned clause, alignment, must be a constant |
| 9095 | // positive integer expression. |
| 9096 | // If no optional parameter is specified, implementation-defined default |
| 9097 | // alignments for SIMD instructions on the target platforms are assumed. |
| 9098 | if (Alignment != nullptr) { |
| 9099 | ExprResult AlignResult = |
| 9100 | VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); |
| 9101 | if (AlignResult.isInvalid()) |
| 9102 | return nullptr; |
| 9103 | Alignment = AlignResult.get(); |
| 9104 | } |
| 9105 | if (Vars.empty()) |
| 9106 | return nullptr; |
| 9107 | |
| 9108 | return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, |
| 9109 | EndLoc, Vars, Alignment); |
| 9110 | } |
| 9111 | |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9112 | OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, |
| 9113 | SourceLocation StartLoc, |
| 9114 | SourceLocation LParenLoc, |
| 9115 | SourceLocation EndLoc) { |
| 9116 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9117 | SmallVector<Expr *, 8> SrcExprs; |
| 9118 | SmallVector<Expr *, 8> DstExprs; |
| 9119 | SmallVector<Expr *, 8> AssignmentOps; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9120 | for (auto &RefExpr : VarList) { |
| 9121 | assert(RefExpr && "NULL expr in OpenMP copyin clause."); |
| 9122 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9123 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9124 | Vars.push_back(RefExpr); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9125 | SrcExprs.push_back(nullptr); |
| 9126 | DstExprs.push_back(nullptr); |
| 9127 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9128 | continue; |
| 9129 | } |
| 9130 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9131 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9132 | // OpenMP [2.1, C/C++] |
| 9133 | // A list item is a variable name. |
| 9134 | // OpenMP [2.14.4.1, Restrictions, p.1] |
| 9135 | // A list item that appears in a copyin clause must be threadprivate. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9136 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9137 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 9138 | Diag(ELoc, diag::err_omp_expected_var_name_member_expr) |
| 9139 | << 0 << RefExpr->getSourceRange(); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9140 | continue; |
| 9141 | } |
| 9142 | |
| 9143 | Decl *D = DE->getDecl(); |
| 9144 | VarDecl *VD = cast<VarDecl>(D); |
| 9145 | |
| 9146 | QualType Type = VD->getType(); |
| 9147 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 9148 | // It will be analyzed later. |
| 9149 | Vars.push_back(DE); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9150 | SrcExprs.push_back(nullptr); |
| 9151 | DstExprs.push_back(nullptr); |
| 9152 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9153 | continue; |
| 9154 | } |
| 9155 | |
| 9156 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] |
| 9157 | // A list item that appears in a copyin clause must be threadprivate. |
| 9158 | if (!DSAStack->isThreadPrivate(VD)) { |
| 9159 | Diag(ELoc, diag::err_omp_required_access) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9160 | << getOpenMPClauseName(OMPC_copyin) |
| 9161 | << getOpenMPDirectiveName(OMPD_threadprivate); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9162 | continue; |
| 9163 | } |
| 9164 | |
| 9165 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] |
| 9166 | // A variable of class type (or array thereof) that appears in a |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 9167 | // copyin clause requires an accessible, unambiguous copy assignment |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9168 | // operator for the class type. |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 9169 | auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 9170 | auto *SrcVD = |
| 9171 | buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(), |
| 9172 | ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 9173 | auto *PseudoSrcExpr = buildDeclRefExpr( |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 9174 | *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); |
| 9175 | auto *DstVD = |
Alexey Bataev | 1d7f0fa | 2015-09-10 09:48:30 +0000 | [diff] [blame] | 9176 | buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst", |
| 9177 | VD->hasAttrs() ? &VD->getAttrs() : nullptr); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9178 | auto *PseudoDstExpr = |
Alexey Bataev | f120c0d | 2015-05-19 07:46:42 +0000 | [diff] [blame] | 9179 | buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9180 | // For arrays generate assignment operation for single element and replace |
| 9181 | // it by the original array element in CodeGen. |
| 9182 | auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, |
| 9183 | PseudoDstExpr, PseudoSrcExpr); |
| 9184 | if (AssignmentOp.isInvalid()) |
| 9185 | continue; |
| 9186 | AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), |
| 9187 | /*DiscardedValue=*/true); |
| 9188 | if (AssignmentOp.isInvalid()) |
| 9189 | continue; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9190 | |
| 9191 | DSAStack->addDSA(VD, DE, OMPC_copyin); |
| 9192 | Vars.push_back(DE); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9193 | SrcExprs.push_back(PseudoSrcExpr); |
| 9194 | DstExprs.push_back(PseudoDstExpr); |
| 9195 | AssignmentOps.push_back(AssignmentOp.get()); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9196 | } |
| 9197 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 9198 | if (Vars.empty()) |
| 9199 | return nullptr; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9200 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 9201 | return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, |
| 9202 | SrcExprs, DstExprs, AssignmentOps); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 9203 | } |
| 9204 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9205 | OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, |
| 9206 | SourceLocation StartLoc, |
| 9207 | SourceLocation LParenLoc, |
| 9208 | SourceLocation EndLoc) { |
| 9209 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9210 | SmallVector<Expr *, 8> SrcExprs; |
| 9211 | SmallVector<Expr *, 8> DstExprs; |
| 9212 | SmallVector<Expr *, 8> AssignmentOps; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9213 | for (auto &RefExpr : VarList) { |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9214 | assert(RefExpr && "NULL expr in OpenMP linear clause."); |
| 9215 | SourceLocation ELoc; |
| 9216 | SourceRange ERange; |
| 9217 | Expr *SimpleRefExpr = RefExpr; |
| 9218 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, |
| 9219 | /*AllowArraySection=*/false); |
| 9220 | if (Res.second) { |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9221 | // It will be analyzed later. |
| 9222 | Vars.push_back(RefExpr); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9223 | SrcExprs.push_back(nullptr); |
| 9224 | DstExprs.push_back(nullptr); |
| 9225 | AssignmentOps.push_back(nullptr); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9226 | } |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9227 | ValueDecl *D = Res.first; |
| 9228 | if (!D) |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9229 | continue; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9230 | |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9231 | QualType Type = D->getType(); |
| 9232 | auto *VD = dyn_cast<VarDecl>(D); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9233 | |
| 9234 | // OpenMP [2.14.4.2, Restrictions, p.2] |
| 9235 | // A list item that appears in a copyprivate clause may not appear in a |
| 9236 | // private or firstprivate clause on the single construct. |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9237 | if (!VD || !DSAStack->isThreadPrivate(VD)) { |
| 9238 | auto DVar = DSAStack->getTopDSA(D, false); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9239 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && |
| 9240 | DVar.RefExpr) { |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9241 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 9242 | << getOpenMPClauseName(DVar.CKind) |
| 9243 | << getOpenMPClauseName(OMPC_copyprivate); |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9244 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9245 | continue; |
| 9246 | } |
| 9247 | |
| 9248 | // OpenMP [2.11.4.2, Restrictions, p.1] |
| 9249 | // All list items that appear in a copyprivate clause must be either |
| 9250 | // threadprivate or private in the enclosing context. |
| 9251 | if (DVar.CKind == OMPC_unknown) { |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9252 | DVar = DSAStack->getImplicitDSA(D, false); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9253 | if (DVar.CKind == OMPC_shared) { |
| 9254 | Diag(ELoc, diag::err_omp_required_access) |
| 9255 | << getOpenMPClauseName(OMPC_copyprivate) |
| 9256 | << "threadprivate or private in the enclosing context"; |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9257 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9258 | continue; |
| 9259 | } |
| 9260 | } |
| 9261 | } |
| 9262 | |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9263 | // Variably modified types are not supported. |
Alexey Bataev | 5129d3a | 2015-05-21 09:47:46 +0000 | [diff] [blame] | 9264 | if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9265 | Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 9266 | << getOpenMPClauseName(OMPC_copyprivate) << Type |
| 9267 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9268 | bool IsDecl = |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9269 | !VD || |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9270 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9271 | Diag(D->getLocation(), |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9272 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9273 | << D; |
Alexey Bataev | 7a3e585 | 2015-05-19 08:19:24 +0000 | [diff] [blame] | 9274 | continue; |
| 9275 | } |
Alexey Bataev | ccb59ec | 2015-05-19 08:44:56 +0000 | [diff] [blame] | 9276 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9277 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] |
| 9278 | // A variable of class type (or array thereof) that appears in a |
| 9279 | // copyin clause requires an accessible, unambiguous copy assignment |
| 9280 | // operator for the class type. |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 9281 | Type = Context.getBaseElementType(Type.getNonReferenceType()) |
| 9282 | .getUnqualifiedType(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 9283 | auto *SrcVD = |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9284 | buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src", |
| 9285 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 9286 | auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 9287 | auto *DstVD = |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9288 | buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst", |
| 9289 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 9290 | auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9291 | auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9292 | PseudoDstExpr, PseudoSrcExpr); |
| 9293 | if (AssignmentOp.isInvalid()) |
| 9294 | continue; |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9295 | AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9296 | /*DiscardedValue=*/true); |
| 9297 | if (AssignmentOp.isInvalid()) |
| 9298 | continue; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9299 | |
| 9300 | // No need to mark vars as copyprivate, they are already threadprivate or |
| 9301 | // implicitly private. |
Alexey Bataev | e122da1 | 2016-03-17 10:50:17 +0000 | [diff] [blame] | 9302 | assert(VD || IsOpenMPCapturedDecl(D)); |
| 9303 | Vars.push_back( |
| 9304 | VD ? RefExpr->IgnoreParens() |
| 9305 | : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9306 | SrcExprs.push_back(PseudoSrcExpr); |
| 9307 | DstExprs.push_back(PseudoDstExpr); |
| 9308 | AssignmentOps.push_back(AssignmentOp.get()); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9309 | } |
| 9310 | |
| 9311 | if (Vars.empty()) |
| 9312 | return nullptr; |
| 9313 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 9314 | return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 9315 | Vars, SrcExprs, DstExprs, AssignmentOps); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 9316 | } |
| 9317 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 9318 | OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, |
| 9319 | SourceLocation StartLoc, |
| 9320 | SourceLocation LParenLoc, |
| 9321 | SourceLocation EndLoc) { |
| 9322 | if (VarList.empty()) |
| 9323 | return nullptr; |
| 9324 | |
| 9325 | return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); |
| 9326 | } |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 9327 | |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9328 | OMPClause * |
| 9329 | Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, |
| 9330 | SourceLocation DepLoc, SourceLocation ColonLoc, |
| 9331 | ArrayRef<Expr *> VarList, SourceLocation StartLoc, |
| 9332 | SourceLocation LParenLoc, SourceLocation EndLoc) { |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 9333 | if (DSAStack->getCurrentDirective() == OMPD_ordered && |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9334 | DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 9335 | Diag(DepLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 9336 | << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 9337 | return nullptr; |
| 9338 | } |
| 9339 | if (DSAStack->getCurrentDirective() != OMPD_ordered && |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9340 | (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || |
| 9341 | DepKind == OMPC_DEPEND_sink)) { |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 9342 | unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9343 | Diag(DepLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | 6402bca | 2015-12-28 07:25:51 +0000 | [diff] [blame] | 9344 | << getListOfPossibleValues(OMPC_depend, /*First=*/0, |
| 9345 | /*Last=*/OMPC_DEPEND_unknown, Except) |
| 9346 | << getOpenMPClauseName(OMPC_depend); |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9347 | return nullptr; |
| 9348 | } |
| 9349 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9350 | DSAStackTy::OperatorOffsetTy OpsOffs; |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9351 | llvm::APSInt DepCounter(/*BitWidth=*/32); |
| 9352 | llvm::APSInt TotalDepCount(/*BitWidth=*/32); |
| 9353 | if (DepKind == OMPC_DEPEND_sink) { |
| 9354 | if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) { |
| 9355 | TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); |
| 9356 | TotalDepCount.setIsUnsigned(/*Val=*/true); |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9357 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9358 | } |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9359 | if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) || |
| 9360 | DSAStack->getParentOrderedRegionParam()) { |
| 9361 | for (auto &RefExpr : VarList) { |
| 9362 | assert(RefExpr && "NULL expr in OpenMP shared clause."); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9363 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9364 | // It will be analyzed later. |
| 9365 | Vars.push_back(RefExpr); |
| 9366 | continue; |
| 9367 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9368 | |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9369 | SourceLocation ELoc = RefExpr->getExprLoc(); |
| 9370 | auto *SimpleExpr = RefExpr->IgnoreParenCasts(); |
| 9371 | if (DepKind == OMPC_DEPEND_sink) { |
| 9372 | if (DepCounter >= TotalDepCount) { |
| 9373 | Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); |
| 9374 | continue; |
| 9375 | } |
| 9376 | ++DepCounter; |
| 9377 | // OpenMP [2.13.9, Summary] |
| 9378 | // depend(dependence-type : vec), where dependence-type is: |
| 9379 | // 'sink' and where vec is the iteration vector, which has the form: |
| 9380 | // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] |
| 9381 | // where n is the value specified by the ordered clause in the loop |
| 9382 | // directive, xi denotes the loop iteration variable of the i-th nested |
| 9383 | // loop associated with the loop directive, and di is a constant |
| 9384 | // non-negative integer. |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9385 | if (CurContext->isDependentContext()) { |
| 9386 | // It will be analyzed later. |
| 9387 | Vars.push_back(RefExpr); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9388 | continue; |
| 9389 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9390 | SimpleExpr = SimpleExpr->IgnoreImplicit(); |
| 9391 | OverloadedOperatorKind OOK = OO_None; |
| 9392 | SourceLocation OOLoc; |
| 9393 | Expr *LHS = SimpleExpr; |
| 9394 | Expr *RHS = nullptr; |
| 9395 | if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { |
| 9396 | OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); |
| 9397 | OOLoc = BO->getOperatorLoc(); |
| 9398 | LHS = BO->getLHS()->IgnoreParenImpCasts(); |
| 9399 | RHS = BO->getRHS()->IgnoreParenImpCasts(); |
| 9400 | } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { |
| 9401 | OOK = OCE->getOperator(); |
| 9402 | OOLoc = OCE->getOperatorLoc(); |
| 9403 | LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); |
| 9404 | RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); |
| 9405 | } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { |
| 9406 | OOK = MCE->getMethodDecl() |
| 9407 | ->getNameInfo() |
| 9408 | .getName() |
| 9409 | .getCXXOverloadedOperator(); |
| 9410 | OOLoc = MCE->getCallee()->getExprLoc(); |
| 9411 | LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); |
| 9412 | RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); |
| 9413 | } |
| 9414 | SourceLocation ELoc; |
| 9415 | SourceRange ERange; |
| 9416 | auto Res = getPrivateItem(*this, LHS, ELoc, ERange, |
| 9417 | /*AllowArraySection=*/false); |
| 9418 | if (Res.second) { |
| 9419 | // It will be analyzed later. |
| 9420 | Vars.push_back(RefExpr); |
| 9421 | } |
| 9422 | ValueDecl *D = Res.first; |
| 9423 | if (!D) |
| 9424 | continue; |
| 9425 | |
| 9426 | if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { |
| 9427 | Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); |
| 9428 | continue; |
| 9429 | } |
| 9430 | if (RHS) { |
| 9431 | ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( |
| 9432 | RHS, OMPC_depend, /*StrictlyPositive=*/false); |
| 9433 | if (RHSRes.isInvalid()) |
| 9434 | continue; |
| 9435 | } |
| 9436 | if (!CurContext->isDependentContext() && |
| 9437 | DSAStack->getParentOrderedRegionParam() && |
| 9438 | DepCounter != DSAStack->isParentLoopControlVariable(D).first) { |
| 9439 | Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) |
| 9440 | << DSAStack->getParentLoopControlVariable( |
| 9441 | DepCounter.getZExtValue()); |
| 9442 | continue; |
| 9443 | } |
| 9444 | OpsOffs.push_back({RHS, OOK}); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9445 | } else { |
| 9446 | // OpenMP [2.11.1.1, Restrictions, p.3] |
| 9447 | // A variable that is part of another variable (such as a field of a |
| 9448 | // structure) but is not an array element or an array section cannot |
| 9449 | // appear in a depend clause. |
| 9450 | auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); |
| 9451 | auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); |
| 9452 | auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); |
| 9453 | if (!RefExpr->IgnoreParenImpCasts()->isLValue() || |
| 9454 | (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) || |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 9455 | (ASE && |
| 9456 | !ASE->getBase() |
| 9457 | ->getType() |
| 9458 | .getNonReferenceType() |
| 9459 | ->isPointerType() && |
| 9460 | !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { |
Alexey Bataev | 48c0bfb | 2016-01-20 09:07:54 +0000 | [diff] [blame] | 9461 | Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item) |
| 9462 | << 0 << RefExpr->getSourceRange(); |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9463 | continue; |
| 9464 | } |
| 9465 | } |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 9466 | Vars.push_back(RefExpr->IgnoreParenImpCasts()); |
| 9467 | } |
| 9468 | |
| 9469 | if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && |
| 9470 | TotalDepCount > VarList.size() && |
| 9471 | DSAStack->getParentOrderedRegionParam()) { |
| 9472 | Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) |
| 9473 | << DSAStack->getParentLoopControlVariable(VarList.size() + 1); |
| 9474 | } |
| 9475 | if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && |
| 9476 | Vars.empty()) |
| 9477 | return nullptr; |
| 9478 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9479 | auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 9480 | DepKind, DepLoc, ColonLoc, Vars); |
| 9481 | if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) |
| 9482 | DSAStack->addDoacrossDependClause(C, OpsOffs); |
| 9483 | return C; |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 9484 | } |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 9485 | |
| 9486 | OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, |
| 9487 | SourceLocation LParenLoc, |
| 9488 | SourceLocation EndLoc) { |
| 9489 | Expr *ValExpr = Device; |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 9490 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 9491 | // OpenMP [2.9.1, Restrictions] |
| 9492 | // The device expression must evaluate to a non-negative integer value. |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 9493 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device, |
| 9494 | /*StrictlyPositive=*/false)) |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 9495 | return nullptr; |
| 9496 | |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 9497 | return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 9498 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 9499 | |
| 9500 | static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc, |
| 9501 | DSAStackTy *Stack, CXXRecordDecl *RD) { |
| 9502 | if (!RD || RD->isInvalidDecl()) |
| 9503 | return true; |
| 9504 | |
| 9505 | auto QTy = SemaRef.Context.getRecordType(RD); |
| 9506 | if (RD->isDynamicClass()) { |
| 9507 | SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; |
| 9508 | SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target); |
| 9509 | return false; |
| 9510 | } |
| 9511 | auto *DC = RD; |
| 9512 | bool IsCorrect = true; |
| 9513 | for (auto *I : DC->decls()) { |
| 9514 | if (I) { |
| 9515 | if (auto *MD = dyn_cast<CXXMethodDecl>(I)) { |
| 9516 | if (MD->isStatic()) { |
| 9517 | SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; |
| 9518 | SemaRef.Diag(MD->getLocation(), |
| 9519 | diag::note_omp_static_member_in_target); |
| 9520 | IsCorrect = false; |
| 9521 | } |
| 9522 | } else if (auto *VD = dyn_cast<VarDecl>(I)) { |
| 9523 | if (VD->isStaticDataMember()) { |
| 9524 | SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; |
| 9525 | SemaRef.Diag(VD->getLocation(), |
| 9526 | diag::note_omp_static_member_in_target); |
| 9527 | IsCorrect = false; |
| 9528 | } |
| 9529 | } |
| 9530 | } |
| 9531 | } |
| 9532 | |
| 9533 | for (auto &I : RD->bases()) { |
| 9534 | if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack, |
| 9535 | I.getType()->getAsCXXRecordDecl())) |
| 9536 | IsCorrect = false; |
| 9537 | } |
| 9538 | return IsCorrect; |
| 9539 | } |
| 9540 | |
| 9541 | static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, |
| 9542 | DSAStackTy *Stack, QualType QTy) { |
| 9543 | NamedDecl *ND; |
| 9544 | if (QTy->isIncompleteType(&ND)) { |
| 9545 | SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; |
| 9546 | return false; |
| 9547 | } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) { |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 9548 | if (!RD->isInvalidDecl() && !IsCXXRecordForMappable(SemaRef, SL, Stack, RD)) |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 9549 | return false; |
| 9550 | } |
| 9551 | return true; |
| 9552 | } |
| 9553 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9554 | /// \brief Return true if it can be proven that the provided array expression |
| 9555 | /// (array section or array subscript) does NOT specify the whole size of the |
| 9556 | /// array whose base type is \a BaseQTy. |
| 9557 | static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, |
| 9558 | const Expr *E, |
| 9559 | QualType BaseQTy) { |
| 9560 | auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
| 9561 | |
| 9562 | // If this is an array subscript, it refers to the whole size if the size of |
| 9563 | // the dimension is constant and equals 1. Also, an array section assumes the |
| 9564 | // format of an array subscript if no colon is used. |
| 9565 | if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { |
| 9566 | if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
| 9567 | return ATy->getSize().getSExtValue() != 1; |
| 9568 | // Size can't be evaluated statically. |
| 9569 | return false; |
| 9570 | } |
| 9571 | |
| 9572 | assert(OASE && "Expecting array section if not an array subscript."); |
| 9573 | auto *LowerBound = OASE->getLowerBound(); |
| 9574 | auto *Length = OASE->getLength(); |
| 9575 | |
| 9576 | // 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] | 9577 | // covering the whole dimension. |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9578 | if (LowerBound) { |
| 9579 | llvm::APSInt ConstLowerBound; |
| 9580 | if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext())) |
| 9581 | return false; // Can't get the integer value as a constant. |
| 9582 | if (ConstLowerBound.getSExtValue()) |
| 9583 | return true; |
| 9584 | } |
| 9585 | |
| 9586 | // If we don't have a length we covering the whole dimension. |
| 9587 | if (!Length) |
| 9588 | return false; |
| 9589 | |
| 9590 | // If the base is a pointer, we don't have a way to get the size of the |
| 9591 | // pointee. |
| 9592 | if (BaseQTy->isPointerType()) |
| 9593 | return false; |
| 9594 | |
| 9595 | // We can only check if the length is the same as the size of the dimension |
| 9596 | // if we have a constant array. |
| 9597 | auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); |
| 9598 | if (!CATy) |
| 9599 | return false; |
| 9600 | |
| 9601 | llvm::APSInt ConstLength; |
| 9602 | if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) |
| 9603 | return false; // Can't get the integer value as a constant. |
| 9604 | |
| 9605 | return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); |
| 9606 | } |
| 9607 | |
| 9608 | // Return true if it can be proven that the provided array expression (array |
| 9609 | // section or array subscript) does NOT specify a single element of the array |
| 9610 | // whose base type is \a BaseQTy. |
| 9611 | static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 9612 | const Expr *E, |
| 9613 | QualType BaseQTy) { |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9614 | auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
| 9615 | |
| 9616 | // An array subscript always refer to a single element. Also, an array section |
| 9617 | // assumes the format of an array subscript if no colon is used. |
| 9618 | if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) |
| 9619 | return false; |
| 9620 | |
| 9621 | assert(OASE && "Expecting array section if not an array subscript."); |
| 9622 | auto *Length = OASE->getLength(); |
| 9623 | |
| 9624 | // If we don't have a length we have to check if the array has unitary size |
| 9625 | // for this dimension. Also, we should always expect a length if the base type |
| 9626 | // is pointer. |
| 9627 | if (!Length) { |
| 9628 | if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
| 9629 | return ATy->getSize().getSExtValue() != 1; |
| 9630 | // We cannot assume anything. |
| 9631 | return false; |
| 9632 | } |
| 9633 | |
| 9634 | // Check if the length evaluates to 1. |
| 9635 | llvm::APSInt ConstLength; |
| 9636 | if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) |
| 9637 | return false; // Can't get the integer value as a constant. |
| 9638 | |
| 9639 | return ConstLength.getSExtValue() != 1; |
| 9640 | } |
| 9641 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 9642 | // Return the expression of the base of the mappable expression or null if it |
| 9643 | // cannot be determined and do all the necessary checks to see if the expression |
| 9644 | // 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] | 9645 | // components of the expression. |
| 9646 | static Expr *CheckMapClauseExpressionBase( |
| 9647 | Sema &SemaRef, Expr *E, |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 9648 | OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, |
| 9649 | OpenMPClauseKind CKind) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9650 | SourceLocation ELoc = E->getExprLoc(); |
| 9651 | SourceRange ERange = E->getSourceRange(); |
| 9652 | |
| 9653 | // The base of elements of list in a map clause have to be either: |
| 9654 | // - a reference to variable or field. |
| 9655 | // - a member expression. |
| 9656 | // - an array expression. |
| 9657 | // |
| 9658 | // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the |
| 9659 | // reference to 'r'. |
| 9660 | // |
| 9661 | // If we have: |
| 9662 | // |
| 9663 | // struct SS { |
| 9664 | // Bla S; |
| 9665 | // foo() { |
| 9666 | // #pragma omp target map (S.Arr[:12]); |
| 9667 | // } |
| 9668 | // } |
| 9669 | // |
| 9670 | // We want to retrieve the member expression 'this->S'; |
| 9671 | |
| 9672 | Expr *RelevantExpr = nullptr; |
| 9673 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9674 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] |
| 9675 | // If a list item is an array section, it must specify contiguous storage. |
| 9676 | // |
| 9677 | // For this restriction it is sufficient that we make sure only references |
| 9678 | // to variables or fields and array expressions, and that no array sections |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9679 | // exist except in the rightmost expression (unless they cover the whole |
| 9680 | // dimension of the array). E.g. these would be invalid: |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9681 | // |
| 9682 | // r.ArrS[3:5].Arr[6:7] |
| 9683 | // |
| 9684 | // r.ArrS[3:5].x |
| 9685 | // |
| 9686 | // but these would be valid: |
| 9687 | // r.ArrS[3].Arr[6:7] |
| 9688 | // |
| 9689 | // r.ArrS[3].x |
| 9690 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9691 | bool AllowUnitySizeArraySection = true; |
| 9692 | bool AllowWholeSizeArraySection = true; |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9693 | |
Dmitry Polukhin | 644a925 | 2016-03-11 07:58:34 +0000 | [diff] [blame] | 9694 | while (!RelevantExpr) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9695 | E = E->IgnoreParenImpCasts(); |
| 9696 | |
| 9697 | if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { |
| 9698 | if (!isa<VarDecl>(CurE->getDecl())) |
| 9699 | break; |
| 9700 | |
| 9701 | RelevantExpr = CurE; |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9702 | |
| 9703 | // If we got a reference to a declaration, we should not expect any array |
| 9704 | // section before that. |
| 9705 | AllowUnitySizeArraySection = false; |
| 9706 | AllowWholeSizeArraySection = false; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9707 | |
| 9708 | // Record the component. |
| 9709 | CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent( |
| 9710 | CurE, CurE->getDecl())); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9711 | continue; |
| 9712 | } |
| 9713 | |
| 9714 | if (auto *CurE = dyn_cast<MemberExpr>(E)) { |
| 9715 | auto *BaseE = CurE->getBase()->IgnoreParenImpCasts(); |
| 9716 | |
| 9717 | if (isa<CXXThisExpr>(BaseE)) |
| 9718 | // We found a base expression: this->Val. |
| 9719 | RelevantExpr = CurE; |
| 9720 | else |
| 9721 | E = BaseE; |
| 9722 | |
| 9723 | if (!isa<FieldDecl>(CurE->getMemberDecl())) { |
| 9724 | SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) |
| 9725 | << CurE->getSourceRange(); |
| 9726 | break; |
| 9727 | } |
| 9728 | |
| 9729 | auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); |
| 9730 | |
| 9731 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] |
| 9732 | // A bit-field cannot appear in a map clause. |
| 9733 | // |
| 9734 | if (FD->isBitField()) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 9735 | SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) |
| 9736 | << CurE->getSourceRange() << getOpenMPClauseName(CKind); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9737 | break; |
| 9738 | } |
| 9739 | |
| 9740 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] |
| 9741 | // If the type of a list item is a reference to a type T then the type |
| 9742 | // will be considered to be T for all purposes of this clause. |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9743 | QualType CurType = BaseE->getType().getNonReferenceType(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9744 | |
| 9745 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] |
| 9746 | // A list item cannot be a variable that is a member of a structure with |
| 9747 | // a union type. |
| 9748 | // |
| 9749 | if (auto *RT = CurType->getAs<RecordType>()) |
| 9750 | if (RT->isUnionType()) { |
| 9751 | SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) |
| 9752 | << CurE->getSourceRange(); |
| 9753 | break; |
| 9754 | } |
| 9755 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9756 | // If we got a member expression, we should not expect any array section |
| 9757 | // before that: |
| 9758 | // |
| 9759 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] |
| 9760 | // If a list item is an element of a structure, only the rightmost symbol |
| 9761 | // of the variable reference can be an array section. |
| 9762 | // |
| 9763 | AllowUnitySizeArraySection = false; |
| 9764 | AllowWholeSizeArraySection = false; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9765 | |
| 9766 | // Record the component. |
| 9767 | CurComponents.push_back( |
| 9768 | OMPClauseMappableExprCommon::MappableComponent(CurE, FD)); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9769 | continue; |
| 9770 | } |
| 9771 | |
| 9772 | if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 9773 | E = CurE->getBase()->IgnoreParenImpCasts(); |
| 9774 | |
| 9775 | if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { |
| 9776 | SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) |
| 9777 | << 0 << CurE->getSourceRange(); |
| 9778 | break; |
| 9779 | } |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9780 | |
| 9781 | // If we got an array subscript that express the whole dimension we |
| 9782 | // can have any array expressions before. If it only expressing part of |
| 9783 | // the dimension, we can only have unitary-size array expressions. |
| 9784 | if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, |
| 9785 | E->getType())) |
| 9786 | AllowWholeSizeArraySection = false; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9787 | |
| 9788 | // Record the component - we don't have any declaration associated. |
| 9789 | CurComponents.push_back( |
| 9790 | OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9791 | continue; |
| 9792 | } |
| 9793 | |
| 9794 | if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9795 | E = CurE->getBase()->IgnoreParenImpCasts(); |
| 9796 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9797 | auto CurType = |
| 9798 | OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); |
| 9799 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9800 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] |
| 9801 | // If the type of a list item is a reference to a type T then the type |
| 9802 | // will be considered to be T for all purposes of this clause. |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9803 | if (CurType->isReferenceType()) |
| 9804 | CurType = CurType->getPointeeType(); |
| 9805 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9806 | bool IsPointer = CurType->isAnyPointerType(); |
| 9807 | |
| 9808 | if (!IsPointer && !CurType->isArrayType()) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9809 | SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) |
| 9810 | << 0 << CurE->getSourceRange(); |
| 9811 | break; |
| 9812 | } |
| 9813 | |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9814 | bool NotWhole = |
| 9815 | CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); |
| 9816 | bool NotUnity = |
| 9817 | CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); |
| 9818 | |
Samuel Antao | dab51bb | 2016-07-18 23:22:11 +0000 | [diff] [blame] | 9819 | if (AllowWholeSizeArraySection) { |
| 9820 | // Any array section is currently allowed. Allowing a whole size array |
| 9821 | // section implies allowing a unity array section as well. |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9822 | // |
| 9823 | // If this array section refers to the whole dimension we can still |
| 9824 | // accept other array sections before this one, except if the base is a |
| 9825 | // pointer. Otherwise, only unitary sections are accepted. |
| 9826 | if (NotWhole || IsPointer) |
| 9827 | AllowWholeSizeArraySection = false; |
Samuel Antao | dab51bb | 2016-07-18 23:22:11 +0000 | [diff] [blame] | 9828 | } else if (AllowUnitySizeArraySection && NotUnity) { |
Samuel Antao | a9f35cb | 2016-03-09 15:46:05 +0000 | [diff] [blame] | 9829 | // A unity or whole array section is not allowed and that is not |
| 9830 | // compatible with the properties of the current array section. |
| 9831 | SemaRef.Diag( |
| 9832 | ELoc, diag::err_array_section_does_not_specify_contiguous_storage) |
| 9833 | << CurE->getSourceRange(); |
| 9834 | break; |
| 9835 | } |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9836 | |
| 9837 | // Record the component - we don't have any declaration associated. |
| 9838 | CurComponents.push_back( |
| 9839 | OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9840 | continue; |
| 9841 | } |
| 9842 | |
| 9843 | // If nothing else worked, this is not a valid map clause expression. |
| 9844 | SemaRef.Diag(ELoc, |
| 9845 | diag::err_omp_expected_named_var_member_or_array_expression) |
| 9846 | << ERange; |
| 9847 | break; |
| 9848 | } |
| 9849 | |
| 9850 | return RelevantExpr; |
| 9851 | } |
| 9852 | |
| 9853 | // Return true if expression E associated with value VD has conflicts with other |
| 9854 | // map information. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9855 | static bool CheckMapConflicts( |
| 9856 | Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E, |
| 9857 | bool CurrentRegionOnly, |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 9858 | OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, |
| 9859 | OpenMPClauseKind CKind) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9860 | assert(VD && E); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9861 | SourceLocation ELoc = E->getExprLoc(); |
| 9862 | SourceRange ERange = E->getSourceRange(); |
| 9863 | |
| 9864 | // In order to easily check the conflicts we need to match each component of |
| 9865 | // the expression under test with the components of the expressions that are |
| 9866 | // already in the stack. |
| 9867 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9868 | assert(!CurComponents.empty() && "Map clause expression with no components!"); |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9869 | assert(CurComponents.back().getAssociatedDeclaration() == VD && |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9870 | "Map clause expression with unexpected base!"); |
| 9871 | |
| 9872 | // Variables to help detecting enclosing problems in data environment nests. |
| 9873 | bool IsEnclosedByDataEnvironmentExpr = false; |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9874 | const Expr *EnclosingExpr = nullptr; |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9875 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9876 | bool FoundError = DSAS->checkMappableExprComponentListsForDecl( |
| 9877 | VD, CurrentRegionOnly, |
| 9878 | [&](OMPClauseMappableExprCommon::MappableExprComponentListRef |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 9879 | StackComponents, |
| 9880 | OpenMPClauseKind) -> bool { |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9881 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9882 | assert(!StackComponents.empty() && |
| 9883 | "Map clause expression with no components!"); |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9884 | assert(StackComponents.back().getAssociatedDeclaration() == VD && |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9885 | "Map clause expression with unexpected base!"); |
| 9886 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9887 | // The whole expression in the stack. |
| 9888 | auto *RE = StackComponents.front().getAssociatedExpression(); |
| 9889 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9890 | // Expressions must start from the same base. Here we detect at which |
| 9891 | // point both expressions diverge from each other and see if we can |
| 9892 | // detect if the memory referred to both expressions is contiguous and |
| 9893 | // do not overlap. |
| 9894 | auto CI = CurComponents.rbegin(); |
| 9895 | auto CE = CurComponents.rend(); |
| 9896 | auto SI = StackComponents.rbegin(); |
| 9897 | auto SE = StackComponents.rend(); |
| 9898 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 9899 | |
| 9900 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] |
| 9901 | // At most one list item can be an array item derived from a given |
| 9902 | // variable in map clauses of the same construct. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9903 | if (CurrentRegionOnly && |
| 9904 | (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || |
| 9905 | isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && |
| 9906 | (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || |
| 9907 | isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { |
| 9908 | SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9909 | diag::err_omp_multiple_array_items_in_map_clause) |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9910 | << CI->getAssociatedExpression()->getSourceRange(); |
| 9911 | SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), |
| 9912 | diag::note_used_here) |
| 9913 | << SI->getAssociatedExpression()->getSourceRange(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9914 | return true; |
| 9915 | } |
| 9916 | |
| 9917 | // Do both expressions have the same kind? |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9918 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 9919 | SI->getAssociatedExpression()->getStmtClass()) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9920 | break; |
| 9921 | |
| 9922 | // Are we dealing with different variables/fields? |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9923 | if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9924 | break; |
| 9925 | } |
Kelvin Li | 9f645ae | 2016-07-18 22:49:16 +0000 | [diff] [blame] | 9926 | // Check if the extra components of the expressions in the enclosing |
| 9927 | // data environment are redundant for the current base declaration. |
| 9928 | // If they are, the maps completely overlap, which is legal. |
| 9929 | for (; SI != SE; ++SI) { |
| 9930 | QualType Type; |
| 9931 | if (auto *ASE = |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 9932 | dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { |
Kelvin Li | 9f645ae | 2016-07-18 22:49:16 +0000 | [diff] [blame] | 9933 | Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 9934 | } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>( |
| 9935 | SI->getAssociatedExpression())) { |
Kelvin Li | 9f645ae | 2016-07-18 22:49:16 +0000 | [diff] [blame] | 9936 | auto *E = OASE->getBase()->IgnoreParenImpCasts(); |
| 9937 | Type = |
| 9938 | OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); |
| 9939 | } |
| 9940 | if (Type.isNull() || Type->isAnyPointerType() || |
| 9941 | CheckArrayExpressionDoesNotReferToWholeSize( |
| 9942 | SemaRef, SI->getAssociatedExpression(), Type)) |
| 9943 | break; |
| 9944 | } |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9945 | |
| 9946 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] |
| 9947 | // List items of map clauses in the same construct must not share |
| 9948 | // original storage. |
| 9949 | // |
| 9950 | // If the expressions are exactly the same or one is a subset of the |
| 9951 | // other, it means they are sharing storage. |
| 9952 | if (CI == CE && SI == SE) { |
| 9953 | if (CurrentRegionOnly) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 9954 | if (CKind == OMPC_map) |
| 9955 | SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; |
| 9956 | else { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 9957 | assert(CKind == OMPC_to || CKind == OMPC_from); |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 9958 | SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) |
| 9959 | << ERange; |
| 9960 | } |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9961 | SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) |
| 9962 | << RE->getSourceRange(); |
| 9963 | return true; |
| 9964 | } else { |
| 9965 | // If we find the same expression in the enclosing data environment, |
| 9966 | // that is legal. |
| 9967 | IsEnclosedByDataEnvironmentExpr = true; |
| 9968 | return false; |
| 9969 | } |
| 9970 | } |
| 9971 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9972 | QualType DerivedType = |
| 9973 | std::prev(CI)->getAssociatedDeclaration()->getType(); |
| 9974 | SourceLocation DerivedLoc = |
| 9975 | std::prev(CI)->getAssociatedExpression()->getExprLoc(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9976 | |
| 9977 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] |
| 9978 | // If the type of a list item is a reference to a type T then the type |
| 9979 | // will be considered to be T for all purposes of this clause. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 9980 | DerivedType = DerivedType.getNonReferenceType(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 9981 | |
| 9982 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] |
| 9983 | // A variable for which the type is pointer and an array section |
| 9984 | // derived from that variable must not appear as list items of map |
| 9985 | // clauses of the same construct. |
| 9986 | // |
| 9987 | // Also, cover one of the cases in: |
| 9988 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] |
| 9989 | // If any part of the original storage of a list item has corresponding |
| 9990 | // storage in the device data environment, all of the original storage |
| 9991 | // must have corresponding storage in the device data environment. |
| 9992 | // |
| 9993 | if (DerivedType->isAnyPointerType()) { |
| 9994 | if (CI == CE || SI == SE) { |
| 9995 | SemaRef.Diag( |
| 9996 | DerivedLoc, |
| 9997 | diag::err_omp_pointer_mapped_along_with_derived_section) |
| 9998 | << DerivedLoc; |
| 9999 | } else { |
| 10000 | assert(CI != CE && SI != SE); |
| 10001 | SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced) |
| 10002 | << DerivedLoc; |
| 10003 | } |
| 10004 | SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) |
| 10005 | << RE->getSourceRange(); |
| 10006 | return true; |
| 10007 | } |
| 10008 | |
| 10009 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] |
| 10010 | // List items of map clauses in the same construct must not share |
| 10011 | // original storage. |
| 10012 | // |
| 10013 | // An expression is a subset of the other. |
| 10014 | if (CurrentRegionOnly && (CI == CE || SI == SE)) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10015 | if (CKind == OMPC_map) |
| 10016 | SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; |
| 10017 | else { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 10018 | assert(CKind == OMPC_to || CKind == OMPC_from); |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10019 | SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) |
| 10020 | << ERange; |
| 10021 | } |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10022 | SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) |
| 10023 | << RE->getSourceRange(); |
| 10024 | return true; |
| 10025 | } |
| 10026 | |
| 10027 | // The current expression uses the same base as other expression in the |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10028 | // data environment but does not contain it completely. |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10029 | if (!CurrentRegionOnly && SI != SE) |
| 10030 | EnclosingExpr = RE; |
| 10031 | |
| 10032 | // The current expression is a subset of the expression in the data |
| 10033 | // environment. |
| 10034 | IsEnclosedByDataEnvironmentExpr |= |
| 10035 | (!CurrentRegionOnly && CI != CE && SI == SE); |
| 10036 | |
| 10037 | return false; |
| 10038 | }); |
| 10039 | |
| 10040 | if (CurrentRegionOnly) |
| 10041 | return FoundError; |
| 10042 | |
| 10043 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] |
| 10044 | // If any part of the original storage of a list item has corresponding |
| 10045 | // storage in the device data environment, all of the original storage must |
| 10046 | // have corresponding storage in the device data environment. |
| 10047 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] |
| 10048 | // If a list item is an element of a structure, and a different element of |
| 10049 | // the structure has a corresponding list item in the device data environment |
| 10050 | // prior to a task encountering the construct associated with the map clause, |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10051 | // 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] | 10052 | // data environment prior to the task encountering the construct. |
| 10053 | // |
| 10054 | if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { |
| 10055 | SemaRef.Diag(ELoc, |
| 10056 | diag::err_omp_original_storage_is_shared_and_does_not_contain) |
| 10057 | << ERange; |
| 10058 | SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) |
| 10059 | << EnclosingExpr->getSourceRange(); |
| 10060 | return true; |
| 10061 | } |
| 10062 | |
| 10063 | return FoundError; |
| 10064 | } |
| 10065 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10066 | namespace { |
| 10067 | // Utility struct that gathers all the related lists associated with a mappable |
| 10068 | // expression. |
| 10069 | struct MappableVarListInfo final { |
| 10070 | // The list of expressions. |
| 10071 | ArrayRef<Expr *> VarList; |
| 10072 | // The list of processed expressions. |
| 10073 | SmallVector<Expr *, 16> ProcessedVarList; |
| 10074 | // The mappble components for each expression. |
| 10075 | OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; |
| 10076 | // The base declaration of the variable. |
| 10077 | SmallVector<ValueDecl *, 16> VarBaseDeclarations; |
| 10078 | |
| 10079 | MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { |
| 10080 | // We have a list of components and base declarations for each entry in the |
| 10081 | // variable list. |
| 10082 | VarComponents.reserve(VarList.size()); |
| 10083 | VarBaseDeclarations.reserve(VarList.size()); |
| 10084 | } |
| 10085 | }; |
| 10086 | } |
| 10087 | |
| 10088 | // Check the validity of the provided variable list for the provided clause kind |
| 10089 | // \a CKind. In the check process the valid expressions, and mappable expression |
| 10090 | // components and variables are extracted and used to fill \a Vars, |
| 10091 | // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and |
| 10092 | // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'. |
| 10093 | static void |
| 10094 | checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS, |
| 10095 | OpenMPClauseKind CKind, MappableVarListInfo &MVLI, |
| 10096 | SourceLocation StartLoc, |
| 10097 | OpenMPMapClauseKind MapType = OMPC_MAP_unknown, |
| 10098 | bool IsMapTypeImplicit = false) { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 10099 | // We only expect mappable expressions in 'to', 'from', and 'map' clauses. |
| 10100 | assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10101 | "Unexpected clause kind with mappable expressions!"); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10102 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10103 | // Keep track of the mappable components and base declarations in this clause. |
| 10104 | // Each entry in the list is going to have a list of components associated. We |
| 10105 | // record each set of the components so that we can build the clause later on. |
| 10106 | // In the end we should have the same amount of declarations and component |
| 10107 | // lists. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10108 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10109 | for (auto &RE : MVLI.VarList) { |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 10110 | assert(RE && "Null expr in omp to/from/map clause"); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10111 | SourceLocation ELoc = RE->getExprLoc(); |
| 10112 | |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10113 | auto *VE = RE->IgnoreParenLValueCasts(); |
| 10114 | |
| 10115 | if (VE->isValueDependent() || VE->isTypeDependent() || |
| 10116 | VE->isInstantiationDependent() || |
| 10117 | VE->containsUnexpandedParameterPack()) { |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10118 | // We can only analyze this information once the missing information is |
| 10119 | // resolved. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10120 | MVLI.ProcessedVarList.push_back(RE); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10121 | continue; |
| 10122 | } |
| 10123 | |
| 10124 | auto *SimpleExpr = RE->IgnoreParenCasts(); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10125 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10126 | if (!RE->IgnoreParenImpCasts()->isLValue()) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10127 | SemaRef.Diag(ELoc, |
| 10128 | diag::err_omp_expected_named_var_member_or_array_expression) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10129 | << RE->getSourceRange(); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10130 | continue; |
| 10131 | } |
| 10132 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10133 | OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; |
| 10134 | ValueDecl *CurDeclaration = nullptr; |
| 10135 | |
| 10136 | // Obtain the array or member expression bases if required. Also, fill the |
| 10137 | // components array with all the components identified in the process. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10138 | auto *BE = |
| 10139 | CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10140 | if (!BE) |
| 10141 | continue; |
| 10142 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10143 | assert(!CurComponents.empty() && |
| 10144 | "Invalid mappable expression information."); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10145 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10146 | // For the following checks, we rely on the base declaration which is |
| 10147 | // expected to be associated with the last component. The declaration is |
| 10148 | // expected to be a variable or a field (if 'this' is being mapped). |
| 10149 | CurDeclaration = CurComponents.back().getAssociatedDeclaration(); |
| 10150 | assert(CurDeclaration && "Null decl on map clause."); |
| 10151 | assert( |
| 10152 | CurDeclaration->isCanonicalDecl() && |
| 10153 | "Expecting components to have associated only canonical declarations."); |
| 10154 | |
| 10155 | auto *VD = dyn_cast<VarDecl>(CurDeclaration); |
| 10156 | auto *FD = dyn_cast<FieldDecl>(CurDeclaration); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10157 | |
| 10158 | assert((VD || FD) && "Only variables or fields are expected here!"); |
NAKAMURA Takumi | 6dcb814 | 2016-01-23 01:38:20 +0000 | [diff] [blame] | 10159 | (void)FD; |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10160 | |
| 10161 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10162 | // threadprivate variables cannot appear in a map clause. |
| 10163 | // OpenMP 4.5 [2.10.5, target update Construct] |
| 10164 | // threadprivate variables cannot appear in a from clause. |
| 10165 | if (VD && DSAS->isThreadPrivate(VD)) { |
| 10166 | auto DVar = DSAS->getTopDSA(VD, false); |
| 10167 | SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) |
| 10168 | << getOpenMPClauseName(CKind); |
| 10169 | ReportOriginalDSA(SemaRef, DSAS, VD, DVar); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10170 | continue; |
| 10171 | } |
| 10172 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10173 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] |
| 10174 | // A list item cannot appear in both a map clause and a data-sharing |
| 10175 | // attribute clause on the same construct. |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10176 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10177 | // Check conflicts with other map clause expressions. We check the conflicts |
| 10178 | // with the current construct separately from the enclosing data |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10179 | // environment, because the restrictions are different. We only have to |
| 10180 | // check conflicts across regions for the map clauses. |
| 10181 | if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, |
| 10182 | /*CurrentRegionOnly=*/true, CurComponents, CKind)) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10183 | break; |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10184 | if (CKind == OMPC_map && |
| 10185 | CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, |
| 10186 | /*CurrentRegionOnly=*/false, CurComponents, CKind)) |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10187 | break; |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10188 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10189 | // OpenMP 4.5 [2.10.5, target update Construct] |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10190 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] |
| 10191 | // If the type of a list item is a reference to a type T then the type will |
| 10192 | // be considered to be T for all purposes of this clause. |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10193 | QualType Type = CurDeclaration->getType().getNonReferenceType(); |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10194 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10195 | // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] |
| 10196 | // 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] | 10197 | // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10198 | // A list item must have a mappable type. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10199 | if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, |
| 10200 | DSAS, Type)) |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10201 | continue; |
| 10202 | |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10203 | if (CKind == OMPC_map) { |
| 10204 | // target enter data |
| 10205 | // OpenMP [2.10.2, Restrictions, p. 99] |
| 10206 | // A map-type must be specified in all map clauses and must be either |
| 10207 | // to or alloc. |
| 10208 | OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); |
| 10209 | if (DKind == OMPD_target_enter_data && |
| 10210 | !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { |
| 10211 | SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) |
| 10212 | << (IsMapTypeImplicit ? 1 : 0) |
| 10213 | << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) |
| 10214 | << getOpenMPDirectiveName(DKind); |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 10215 | continue; |
| 10216 | } |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10217 | |
| 10218 | // target exit_data |
| 10219 | // OpenMP [2.10.3, Restrictions, p. 102] |
| 10220 | // A map-type must be specified in all map clauses and must be either |
| 10221 | // from, release, or delete. |
| 10222 | if (DKind == OMPD_target_exit_data && |
| 10223 | !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || |
| 10224 | MapType == OMPC_MAP_delete)) { |
| 10225 | SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) |
| 10226 | << (IsMapTypeImplicit ? 1 : 0) |
| 10227 | << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) |
| 10228 | << getOpenMPDirectiveName(DKind); |
| 10229 | continue; |
| 10230 | } |
| 10231 | |
| 10232 | // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] |
| 10233 | // A list item cannot appear in both a map clause and a data-sharing |
| 10234 | // attribute clause on the same construct |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 10235 | if ((DKind == OMPD_target || DKind == OMPD_target_teams || |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 10236 | DKind == OMPD_target_teams_distribute || |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 10237 | DKind == OMPD_target_teams_distribute_parallel_for || |
| 10238 | DKind == OMPD_target_teams_distribute_parallel_for_simd) && VD) { |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10239 | auto DVar = DSAS->getTopDSA(VD, false); |
| 10240 | if (isOpenMPPrivate(DVar.CKind)) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 10241 | SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10242 | << getOpenMPClauseName(DVar.CKind) |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 10243 | << getOpenMPClauseName(OMPC_map) |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10244 | << getOpenMPDirectiveName(DSAS->getCurrentDirective()); |
| 10245 | ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar); |
| 10246 | continue; |
| 10247 | } |
| 10248 | } |
Carlo Bertolli | b74bfc8 | 2016-03-18 21:43:32 +0000 | [diff] [blame] | 10249 | } |
| 10250 | |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10251 | // Save the current expression. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10252 | MVLI.ProcessedVarList.push_back(RE); |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10253 | |
| 10254 | // Store the components in the stack so that they can be used to check |
| 10255 | // against other clauses later on. |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 10256 | DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, |
| 10257 | /*WhereFoundClauseKind=*/OMPC_map); |
Samuel Antao | 9092700 | 2016-04-26 14:54:23 +0000 | [diff] [blame] | 10258 | |
| 10259 | // Save the components and declaration to create the clause. For purposes of |
| 10260 | // the clause creation, any component list that has has base 'this' uses |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 10261 | // null as base declaration. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10262 | MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); |
| 10263 | MVLI.VarComponents.back().append(CurComponents.begin(), |
| 10264 | CurComponents.end()); |
| 10265 | MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr |
| 10266 | : CurDeclaration); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10267 | } |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10268 | } |
| 10269 | |
| 10270 | OMPClause * |
| 10271 | Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier, |
| 10272 | OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, |
| 10273 | SourceLocation MapLoc, SourceLocation ColonLoc, |
| 10274 | ArrayRef<Expr *> VarList, SourceLocation StartLoc, |
| 10275 | SourceLocation LParenLoc, SourceLocation EndLoc) { |
| 10276 | MappableVarListInfo MVLI(VarList); |
| 10277 | checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc, |
| 10278 | MapType, IsMapTypeImplicit); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10279 | |
Samuel Antao | 5de996e | 2016-01-22 20:21:36 +0000 | [diff] [blame] | 10280 | // We need to produce a map clause even if we don't have variables so that |
| 10281 | // other diagnostics related with non-existing map clauses are accurate. |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10282 | return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 10283 | MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, |
| 10284 | MVLI.VarComponents, MapTypeModifier, MapType, |
| 10285 | IsMapTypeImplicit, MapLoc); |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 10286 | } |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 10287 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 10288 | QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, |
| 10289 | TypeResult ParsedType) { |
| 10290 | assert(ParsedType.isUsable()); |
| 10291 | |
| 10292 | QualType ReductionType = GetTypeFromParser(ParsedType.get()); |
| 10293 | if (ReductionType.isNull()) |
| 10294 | return QualType(); |
| 10295 | |
| 10296 | // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ |
| 10297 | // A type name in a declare reduction directive cannot be a function type, an |
| 10298 | // array type, a reference type, or a type qualified with const, volatile or |
| 10299 | // restrict. |
| 10300 | if (ReductionType.hasQualifiers()) { |
| 10301 | Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; |
| 10302 | return QualType(); |
| 10303 | } |
| 10304 | |
| 10305 | if (ReductionType->isFunctionType()) { |
| 10306 | Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; |
| 10307 | return QualType(); |
| 10308 | } |
| 10309 | if (ReductionType->isReferenceType()) { |
| 10310 | Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; |
| 10311 | return QualType(); |
| 10312 | } |
| 10313 | if (ReductionType->isArrayType()) { |
| 10314 | Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; |
| 10315 | return QualType(); |
| 10316 | } |
| 10317 | return ReductionType; |
| 10318 | } |
| 10319 | |
| 10320 | Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( |
| 10321 | Scope *S, DeclContext *DC, DeclarationName Name, |
| 10322 | ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, |
| 10323 | AccessSpecifier AS, Decl *PrevDeclInScope) { |
| 10324 | SmallVector<Decl *, 8> Decls; |
| 10325 | Decls.reserve(ReductionTypes.size()); |
| 10326 | |
| 10327 | LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, |
| 10328 | ForRedeclaration); |
| 10329 | // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions |
| 10330 | // A reduction-identifier may not be re-declared in the current scope for the |
| 10331 | // same type or for a type that is compatible according to the base language |
| 10332 | // rules. |
| 10333 | llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; |
| 10334 | OMPDeclareReductionDecl *PrevDRD = nullptr; |
| 10335 | bool InCompoundScope = true; |
| 10336 | if (S != nullptr) { |
| 10337 | // Find previous declaration with the same name not referenced in other |
| 10338 | // declarations. |
| 10339 | FunctionScopeInfo *ParentFn = getEnclosingFunction(); |
| 10340 | InCompoundScope = |
| 10341 | (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); |
| 10342 | LookupName(Lookup, S); |
| 10343 | FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, |
| 10344 | /*AllowInlineNamespace=*/false); |
| 10345 | llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; |
| 10346 | auto Filter = Lookup.makeFilter(); |
| 10347 | while (Filter.hasNext()) { |
| 10348 | auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); |
| 10349 | if (InCompoundScope) { |
| 10350 | auto I = UsedAsPrevious.find(PrevDecl); |
| 10351 | if (I == UsedAsPrevious.end()) |
| 10352 | UsedAsPrevious[PrevDecl] = false; |
| 10353 | if (auto *D = PrevDecl->getPrevDeclInScope()) |
| 10354 | UsedAsPrevious[D] = true; |
| 10355 | } |
| 10356 | PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = |
| 10357 | PrevDecl->getLocation(); |
| 10358 | } |
| 10359 | Filter.done(); |
| 10360 | if (InCompoundScope) { |
| 10361 | for (auto &PrevData : UsedAsPrevious) { |
| 10362 | if (!PrevData.second) { |
| 10363 | PrevDRD = PrevData.first; |
| 10364 | break; |
| 10365 | } |
| 10366 | } |
| 10367 | } |
| 10368 | } else if (PrevDeclInScope != nullptr) { |
| 10369 | auto *PrevDRDInScope = PrevDRD = |
| 10370 | cast<OMPDeclareReductionDecl>(PrevDeclInScope); |
| 10371 | do { |
| 10372 | PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = |
| 10373 | PrevDRDInScope->getLocation(); |
| 10374 | PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); |
| 10375 | } while (PrevDRDInScope != nullptr); |
| 10376 | } |
| 10377 | for (auto &TyData : ReductionTypes) { |
| 10378 | auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); |
| 10379 | bool Invalid = false; |
| 10380 | if (I != PreviousRedeclTypes.end()) { |
| 10381 | Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) |
| 10382 | << TyData.first; |
| 10383 | Diag(I->second, diag::note_previous_definition); |
| 10384 | Invalid = true; |
| 10385 | } |
| 10386 | PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; |
| 10387 | auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, |
| 10388 | Name, TyData.first, PrevDRD); |
| 10389 | DC->addDecl(DRD); |
| 10390 | DRD->setAccess(AS); |
| 10391 | Decls.push_back(DRD); |
| 10392 | if (Invalid) |
| 10393 | DRD->setInvalidDecl(); |
| 10394 | else |
| 10395 | PrevDRD = DRD; |
| 10396 | } |
| 10397 | |
| 10398 | return DeclGroupPtrTy::make( |
| 10399 | DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); |
| 10400 | } |
| 10401 | |
| 10402 | void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { |
| 10403 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 10404 | |
| 10405 | // Enter new function scope. |
| 10406 | PushFunctionScope(); |
| 10407 | getCurFunction()->setHasBranchProtectedScope(); |
| 10408 | getCurFunction()->setHasOMPDeclareReductionCombiner(); |
| 10409 | |
| 10410 | if (S != nullptr) |
| 10411 | PushDeclContext(S, DRD); |
| 10412 | else |
| 10413 | CurContext = DRD; |
| 10414 | |
| 10415 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
| 10416 | |
| 10417 | QualType ReductionType = DRD->getType(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 10418 | // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will |
| 10419 | // be replaced by '*omp_parm' during codegen. This required because 'omp_in' |
| 10420 | // uses semantics of argument handles by value, but it should be passed by |
| 10421 | // reference. C lang does not support references, so pass all parameters as |
| 10422 | // pointers. |
| 10423 | // Create 'T omp_in;' variable. |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 10424 | auto *OmpInParm = |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 10425 | buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 10426 | // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will |
| 10427 | // be replaced by '*omp_parm' during codegen. This required because 'omp_out' |
| 10428 | // uses semantics of argument handles by value, but it should be passed by |
| 10429 | // reference. C lang does not support references, so pass all parameters as |
| 10430 | // pointers. |
| 10431 | // Create 'T omp_out;' variable. |
| 10432 | auto *OmpOutParm = |
| 10433 | buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); |
| 10434 | if (S != nullptr) { |
| 10435 | PushOnScopeChains(OmpInParm, S); |
| 10436 | PushOnScopeChains(OmpOutParm, S); |
| 10437 | } else { |
| 10438 | DRD->addDecl(OmpInParm); |
| 10439 | DRD->addDecl(OmpOutParm); |
| 10440 | } |
| 10441 | } |
| 10442 | |
| 10443 | void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { |
| 10444 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 10445 | DiscardCleanupsInEvaluationContext(); |
| 10446 | PopExpressionEvaluationContext(); |
| 10447 | |
| 10448 | PopDeclContext(); |
| 10449 | PopFunctionScopeInfo(); |
| 10450 | |
| 10451 | if (Combiner != nullptr) |
| 10452 | DRD->setCombiner(Combiner); |
| 10453 | else |
| 10454 | DRD->setInvalidDecl(); |
| 10455 | } |
| 10456 | |
| 10457 | void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { |
| 10458 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 10459 | |
| 10460 | // Enter new function scope. |
| 10461 | PushFunctionScope(); |
| 10462 | getCurFunction()->setHasBranchProtectedScope(); |
| 10463 | |
| 10464 | if (S != nullptr) |
| 10465 | PushDeclContext(S, DRD); |
| 10466 | else |
| 10467 | CurContext = DRD; |
| 10468 | |
| 10469 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
| 10470 | |
| 10471 | QualType ReductionType = DRD->getType(); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 10472 | // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will |
| 10473 | // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' |
| 10474 | // uses semantics of argument handles by value, but it should be passed by |
| 10475 | // reference. C lang does not support references, so pass all parameters as |
| 10476 | // pointers. |
| 10477 | // Create 'T omp_priv;' variable. |
| 10478 | auto *OmpPrivParm = |
| 10479 | buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 10480 | // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will |
| 10481 | // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' |
| 10482 | // uses semantics of argument handles by value, but it should be passed by |
| 10483 | // reference. C lang does not support references, so pass all parameters as |
| 10484 | // pointers. |
| 10485 | // Create 'T omp_orig;' variable. |
| 10486 | auto *OmpOrigParm = |
| 10487 | buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 10488 | if (S != nullptr) { |
| 10489 | PushOnScopeChains(OmpPrivParm, S); |
| 10490 | PushOnScopeChains(OmpOrigParm, S); |
| 10491 | } else { |
| 10492 | DRD->addDecl(OmpPrivParm); |
| 10493 | DRD->addDecl(OmpOrigParm); |
| 10494 | } |
| 10495 | } |
| 10496 | |
| 10497 | void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, |
| 10498 | Expr *Initializer) { |
| 10499 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 10500 | DiscardCleanupsInEvaluationContext(); |
| 10501 | PopExpressionEvaluationContext(); |
| 10502 | |
| 10503 | PopDeclContext(); |
| 10504 | PopFunctionScopeInfo(); |
| 10505 | |
| 10506 | if (Initializer != nullptr) |
| 10507 | DRD->setInitializer(Initializer); |
| 10508 | else |
| 10509 | DRD->setInvalidDecl(); |
| 10510 | } |
| 10511 | |
| 10512 | Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( |
| 10513 | Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { |
| 10514 | for (auto *D : DeclReductions.get()) { |
| 10515 | if (IsValid) { |
| 10516 | auto *DRD = cast<OMPDeclareReductionDecl>(D); |
| 10517 | if (S != nullptr) |
| 10518 | PushOnScopeChains(DRD, S, /*AddToContext=*/false); |
| 10519 | } else |
| 10520 | D->setInvalidDecl(); |
| 10521 | } |
| 10522 | return DeclReductions; |
| 10523 | } |
| 10524 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10525 | OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 10526 | SourceLocation StartLoc, |
| 10527 | SourceLocation LParenLoc, |
| 10528 | SourceLocation EndLoc) { |
| 10529 | Expr *ValExpr = NumTeams; |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 10530 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 10531 | // OpenMP [teams Constrcut, Restrictions] |
| 10532 | // The num_teams expression must evaluate to a positive integer value. |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 10533 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, |
| 10534 | /*StrictlyPositive=*/true)) |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 10535 | return nullptr; |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 10536 | |
| 10537 | return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 10538 | } |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 10539 | |
| 10540 | OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, |
| 10541 | SourceLocation StartLoc, |
| 10542 | SourceLocation LParenLoc, |
| 10543 | SourceLocation EndLoc) { |
| 10544 | Expr *ValExpr = ThreadLimit; |
| 10545 | |
| 10546 | // OpenMP [teams Constrcut, Restrictions] |
| 10547 | // The thread_limit expression must evaluate to a positive integer value. |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 10548 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, |
| 10549 | /*StrictlyPositive=*/true)) |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 10550 | return nullptr; |
| 10551 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10552 | return new (Context) |
| 10553 | OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 10554 | } |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 10555 | |
| 10556 | OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, |
| 10557 | SourceLocation StartLoc, |
| 10558 | SourceLocation LParenLoc, |
| 10559 | SourceLocation EndLoc) { |
| 10560 | Expr *ValExpr = Priority; |
| 10561 | |
| 10562 | // OpenMP [2.9.1, task Constrcut] |
| 10563 | // The priority-value is a non-negative numerical scalar expression. |
| 10564 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, |
| 10565 | /*StrictlyPositive=*/false)) |
| 10566 | return nullptr; |
| 10567 | |
| 10568 | return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 10569 | } |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 10570 | |
| 10571 | OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, |
| 10572 | SourceLocation StartLoc, |
| 10573 | SourceLocation LParenLoc, |
| 10574 | SourceLocation EndLoc) { |
| 10575 | Expr *ValExpr = Grainsize; |
| 10576 | |
| 10577 | // OpenMP [2.9.2, taskloop Constrcut] |
| 10578 | // The parameter of the grainsize clause must be a positive integer |
| 10579 | // expression. |
| 10580 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, |
| 10581 | /*StrictlyPositive=*/true)) |
| 10582 | return nullptr; |
| 10583 | |
| 10584 | return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 10585 | } |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 10586 | |
| 10587 | OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, |
| 10588 | SourceLocation StartLoc, |
| 10589 | SourceLocation LParenLoc, |
| 10590 | SourceLocation EndLoc) { |
| 10591 | Expr *ValExpr = NumTasks; |
| 10592 | |
| 10593 | // OpenMP [2.9.2, taskloop Constrcut] |
| 10594 | // The parameter of the num_tasks clause must be a positive integer |
| 10595 | // expression. |
| 10596 | if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, |
| 10597 | /*StrictlyPositive=*/true)) |
| 10598 | return nullptr; |
| 10599 | |
| 10600 | return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 10601 | } |
| 10602 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 10603 | OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, |
| 10604 | SourceLocation LParenLoc, |
| 10605 | SourceLocation EndLoc) { |
| 10606 | // OpenMP [2.13.2, critical construct, Description] |
| 10607 | // ... where hint-expression is an integer constant expression that evaluates |
| 10608 | // to a valid lock hint. |
| 10609 | ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); |
| 10610 | if (HintExpr.isInvalid()) |
| 10611 | return nullptr; |
| 10612 | return new (Context) |
| 10613 | OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); |
| 10614 | } |
| 10615 | |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 10616 | OMPClause *Sema::ActOnOpenMPDistScheduleClause( |
| 10617 | OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, |
| 10618 | SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, |
| 10619 | SourceLocation EndLoc) { |
| 10620 | if (Kind == OMPC_DIST_SCHEDULE_unknown) { |
| 10621 | std::string Values; |
| 10622 | Values += "'"; |
| 10623 | Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); |
| 10624 | Values += "'"; |
| 10625 | Diag(KindLoc, diag::err_omp_unexpected_clause_value) |
| 10626 | << Values << getOpenMPClauseName(OMPC_dist_schedule); |
| 10627 | return nullptr; |
| 10628 | } |
| 10629 | Expr *ValExpr = ChunkSize; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 10630 | Stmt *HelperValStmt = nullptr; |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 10631 | if (ChunkSize) { |
| 10632 | if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && |
| 10633 | !ChunkSize->isInstantiationDependent() && |
| 10634 | !ChunkSize->containsUnexpandedParameterPack()) { |
| 10635 | SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); |
| 10636 | ExprResult Val = |
| 10637 | PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); |
| 10638 | if (Val.isInvalid()) |
| 10639 | return nullptr; |
| 10640 | |
| 10641 | ValExpr = Val.get(); |
| 10642 | |
| 10643 | // OpenMP [2.7.1, Restrictions] |
| 10644 | // chunk_size must be a loop invariant integer expression with a positive |
| 10645 | // value. |
| 10646 | llvm::APSInt Result; |
| 10647 | if (ValExpr->isIntegerConstantExpr(Result, Context)) { |
| 10648 | if (Result.isSigned() && !Result.isStrictlyPositive()) { |
| 10649 | Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) |
| 10650 | << "dist_schedule" << ChunkSize->getSourceRange(); |
| 10651 | return nullptr; |
| 10652 | } |
Alexey Bataev | b46cdea | 2016-06-15 11:20:48 +0000 | [diff] [blame] | 10653 | } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && |
| 10654 | !CurContext->isDependentContext()) { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 10655 | llvm::MapVector<Expr *, DeclRefExpr *> Captures; |
| 10656 | ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); |
| 10657 | HelperValStmt = buildPreInits(Context, Captures); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 10658 | } |
| 10659 | } |
| 10660 | } |
| 10661 | |
| 10662 | return new (Context) |
| 10663 | OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 10664 | Kind, ValExpr, HelperValStmt); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 10665 | } |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 10666 | |
| 10667 | OMPClause *Sema::ActOnOpenMPDefaultmapClause( |
| 10668 | OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, |
| 10669 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, |
| 10670 | SourceLocation KindLoc, SourceLocation EndLoc) { |
| 10671 | // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10672 | if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 10673 | std::string Value; |
| 10674 | SourceLocation Loc; |
| 10675 | Value += "'"; |
| 10676 | if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { |
| 10677 | Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10678 | OMPC_DEFAULTMAP_MODIFIER_tofrom); |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 10679 | Loc = MLoc; |
| 10680 | } else { |
| 10681 | Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10682 | OMPC_DEFAULTMAP_scalar); |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 10683 | Loc = KindLoc; |
| 10684 | } |
| 10685 | Value += "'"; |
| 10686 | Diag(Loc, diag::err_omp_unexpected_clause_value) |
| 10687 | << Value << getOpenMPClauseName(OMPC_defaultmap); |
| 10688 | return nullptr; |
| 10689 | } |
| 10690 | |
| 10691 | return new (Context) |
| 10692 | OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); |
| 10693 | } |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10694 | |
| 10695 | bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { |
| 10696 | DeclContext *CurLexicalContext = getCurLexicalContext(); |
| 10697 | if (!CurLexicalContext->isFileContext() && |
| 10698 | !CurLexicalContext->isExternCContext() && |
| 10699 | !CurLexicalContext->isExternCXXContext()) { |
| 10700 | Diag(Loc, diag::err_omp_region_not_file_context); |
| 10701 | return false; |
| 10702 | } |
| 10703 | if (IsInOpenMPDeclareTargetContext) { |
| 10704 | Diag(Loc, diag::err_omp_enclosed_declare_target); |
| 10705 | return false; |
| 10706 | } |
| 10707 | |
| 10708 | IsInOpenMPDeclareTargetContext = true; |
| 10709 | return true; |
| 10710 | } |
| 10711 | |
| 10712 | void Sema::ActOnFinishOpenMPDeclareTargetDirective() { |
| 10713 | assert(IsInOpenMPDeclareTargetContext && |
| 10714 | "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); |
| 10715 | |
| 10716 | IsInOpenMPDeclareTargetContext = false; |
| 10717 | } |
| 10718 | |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 10719 | void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, |
| 10720 | CXXScopeSpec &ScopeSpec, |
| 10721 | const DeclarationNameInfo &Id, |
| 10722 | OMPDeclareTargetDeclAttr::MapTypeTy MT, |
| 10723 | NamedDeclSetType &SameDirectiveDecls) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10724 | LookupResult Lookup(*this, Id, LookupOrdinaryName); |
| 10725 | LookupParsedName(Lookup, CurScope, &ScopeSpec, true); |
| 10726 | |
| 10727 | if (Lookup.isAmbiguous()) |
| 10728 | return; |
| 10729 | Lookup.suppressDiagnostics(); |
| 10730 | |
| 10731 | if (!Lookup.isSingleResult()) { |
| 10732 | if (TypoCorrection Corrected = |
| 10733 | CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, |
| 10734 | llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), |
| 10735 | CTK_ErrorRecovery)) { |
| 10736 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) |
| 10737 | << Id.getName()); |
| 10738 | checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); |
| 10739 | return; |
| 10740 | } |
| 10741 | |
| 10742 | Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); |
| 10743 | return; |
| 10744 | } |
| 10745 | |
| 10746 | NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); |
| 10747 | if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { |
| 10748 | if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) |
| 10749 | Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); |
| 10750 | |
| 10751 | if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) { |
| 10752 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); |
| 10753 | ND->addAttr(A); |
| 10754 | if (ASTMutationListener *ML = Context.getASTMutationListener()) |
| 10755 | ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); |
| 10756 | checkDeclIsAllowedInOpenMPTarget(nullptr, ND); |
| 10757 | } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) { |
| 10758 | Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) |
| 10759 | << Id.getName(); |
| 10760 | } |
| 10761 | } else |
| 10762 | Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); |
| 10763 | } |
| 10764 | |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10765 | static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, |
| 10766 | Sema &SemaRef, Decl *D) { |
| 10767 | if (!D) |
| 10768 | return; |
| 10769 | Decl *LD = nullptr; |
| 10770 | if (isa<TagDecl>(D)) { |
| 10771 | LD = cast<TagDecl>(D)->getDefinition(); |
| 10772 | } else if (isa<VarDecl>(D)) { |
| 10773 | LD = cast<VarDecl>(D)->getDefinition(); |
| 10774 | |
| 10775 | // If this is an implicit variable that is legal and we do not need to do |
| 10776 | // anything. |
| 10777 | if (cast<VarDecl>(D)->isImplicit()) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10778 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 10779 | SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); |
| 10780 | D->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10781 | if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10782 | ML->DeclarationMarkedOpenMPDeclareTarget(D, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10783 | return; |
| 10784 | } |
| 10785 | |
| 10786 | } else if (isa<FunctionDecl>(D)) { |
| 10787 | const FunctionDecl *FD = nullptr; |
| 10788 | if (cast<FunctionDecl>(D)->hasBody(FD)) |
| 10789 | LD = const_cast<FunctionDecl *>(FD); |
| 10790 | |
| 10791 | // If the definition is associated with the current declaration in the |
| 10792 | // target region (it can be e.g. a lambda) that is legal and we do not need |
| 10793 | // to do anything else. |
| 10794 | if (LD == D) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10795 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 10796 | SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); |
| 10797 | D->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10798 | if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10799 | ML->DeclarationMarkedOpenMPDeclareTarget(D, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10800 | return; |
| 10801 | } |
| 10802 | } |
| 10803 | if (!LD) |
| 10804 | LD = D; |
| 10805 | if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() && |
| 10806 | (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) { |
| 10807 | // Outlined declaration is not declared target. |
| 10808 | if (LD->isOutOfLine()) { |
| 10809 | SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); |
| 10810 | SemaRef.Diag(SL, diag::note_used_here) << SR; |
| 10811 | } else { |
| 10812 | DeclContext *DC = LD->getDeclContext(); |
| 10813 | while (DC) { |
| 10814 | if (isa<FunctionDecl>(DC) && |
| 10815 | cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>()) |
| 10816 | break; |
| 10817 | DC = DC->getParent(); |
| 10818 | } |
| 10819 | if (DC) |
| 10820 | return; |
| 10821 | |
| 10822 | // Is not declared in target context. |
| 10823 | SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); |
| 10824 | SemaRef.Diag(SL, diag::note_used_here) << SR; |
| 10825 | } |
| 10826 | // Mark decl as declared target to prevent further diagnostic. |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10827 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 10828 | SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); |
| 10829 | D->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10830 | if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10831 | ML->DeclarationMarkedOpenMPDeclareTarget(D, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10832 | } |
| 10833 | } |
| 10834 | |
| 10835 | static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, |
| 10836 | Sema &SemaRef, DSAStackTy *Stack, |
| 10837 | ValueDecl *VD) { |
| 10838 | if (VD->hasAttr<OMPDeclareTargetDeclAttr>()) |
| 10839 | return true; |
| 10840 | if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType())) |
| 10841 | return false; |
| 10842 | return true; |
| 10843 | } |
| 10844 | |
| 10845 | void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) { |
| 10846 | if (!D || D->isInvalidDecl()) |
| 10847 | return; |
| 10848 | SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); |
| 10849 | SourceLocation SL = E ? E->getLocStart() : D->getLocation(); |
| 10850 | // 2.10.6: threadprivate variable cannot appear in a declare target directive. |
| 10851 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 10852 | if (DSAStack->isThreadPrivate(VD)) { |
| 10853 | Diag(SL, diag::err_omp_threadprivate_in_target); |
| 10854 | ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); |
| 10855 | return; |
| 10856 | } |
| 10857 | } |
| 10858 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 10859 | // Problem if any with var declared with incomplete type will be reported |
| 10860 | // as normal, so no need to check it here. |
| 10861 | if ((E || !VD->getType()->isIncompleteType()) && |
| 10862 | !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) { |
| 10863 | // Mark decl as declared target to prevent further diagnostic. |
| 10864 | if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10865 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 10866 | Context, OMPDeclareTargetDeclAttr::MT_To); |
| 10867 | VD->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10868 | if (ASTMutationListener *ML = Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10869 | ML->DeclarationMarkedOpenMPDeclareTarget(VD, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10870 | } |
| 10871 | return; |
| 10872 | } |
| 10873 | } |
| 10874 | if (!E) { |
| 10875 | // Checking declaration inside declare target region. |
| 10876 | if (!D->hasAttr<OMPDeclareTargetDeclAttr>() && |
| 10877 | (isa<VarDecl>(D) || isa<FunctionDecl>(D))) { |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10878 | Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( |
| 10879 | Context, OMPDeclareTargetDeclAttr::MT_To); |
| 10880 | D->addAttr(A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10881 | if (ASTMutationListener *ML = Context.getASTMutationListener()) |
Dmitry Polukhin | d69b505 | 2016-05-09 14:59:13 +0000 | [diff] [blame] | 10882 | ML->DeclarationMarkedOpenMPDeclareTarget(D, A); |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 10883 | } |
| 10884 | return; |
| 10885 | } |
| 10886 | checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); |
| 10887 | } |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 10888 | |
| 10889 | OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, |
| 10890 | SourceLocation StartLoc, |
| 10891 | SourceLocation LParenLoc, |
| 10892 | SourceLocation EndLoc) { |
| 10893 | MappableVarListInfo MVLI(VarList); |
| 10894 | checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc); |
| 10895 | if (MVLI.ProcessedVarList.empty()) |
| 10896 | return nullptr; |
| 10897 | |
| 10898 | return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 10899 | MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, |
| 10900 | MVLI.VarComponents); |
| 10901 | } |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 10902 | |
| 10903 | OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, |
| 10904 | SourceLocation StartLoc, |
| 10905 | SourceLocation LParenLoc, |
| 10906 | SourceLocation EndLoc) { |
| 10907 | MappableVarListInfo MVLI(VarList); |
| 10908 | checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc); |
| 10909 | if (MVLI.ProcessedVarList.empty()) |
| 10910 | return nullptr; |
| 10911 | |
| 10912 | return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 10913 | MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, |
| 10914 | MVLI.VarComponents); |
| 10915 | } |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 10916 | |
| 10917 | OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, |
| 10918 | SourceLocation StartLoc, |
| 10919 | SourceLocation LParenLoc, |
| 10920 | SourceLocation EndLoc) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10921 | MappableVarListInfo MVLI(VarList); |
| 10922 | SmallVector<Expr *, 8> PrivateCopies; |
| 10923 | SmallVector<Expr *, 8> Inits; |
| 10924 | |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 10925 | for (auto &RefExpr : VarList) { |
| 10926 | assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); |
| 10927 | SourceLocation ELoc; |
| 10928 | SourceRange ERange; |
| 10929 | Expr *SimpleRefExpr = RefExpr; |
| 10930 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
| 10931 | if (Res.second) { |
| 10932 | // It will be analyzed later. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10933 | MVLI.ProcessedVarList.push_back(RefExpr); |
| 10934 | PrivateCopies.push_back(nullptr); |
| 10935 | Inits.push_back(nullptr); |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 10936 | } |
| 10937 | ValueDecl *D = Res.first; |
| 10938 | if (!D) |
| 10939 | continue; |
| 10940 | |
| 10941 | QualType Type = D->getType(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10942 | Type = Type.getNonReferenceType().getUnqualifiedType(); |
| 10943 | |
| 10944 | auto *VD = dyn_cast<VarDecl>(D); |
| 10945 | |
| 10946 | // Item should be a pointer or reference to pointer. |
| 10947 | if (!Type->isPointerType()) { |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 10948 | Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) |
| 10949 | << 0 << RefExpr->getSourceRange(); |
| 10950 | continue; |
| 10951 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10952 | |
| 10953 | // Build the private variable and the expression that refers to it. |
| 10954 | auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), |
| 10955 | D->hasAttrs() ? &D->getAttrs() : nullptr); |
| 10956 | if (VDPrivate->isInvalidDecl()) |
| 10957 | continue; |
| 10958 | |
| 10959 | CurContext->addDecl(VDPrivate); |
| 10960 | auto VDPrivateRefExpr = buildDeclRefExpr( |
| 10961 | *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); |
| 10962 | |
| 10963 | // Add temporary variable to initialize the private copy of the pointer. |
| 10964 | auto *VDInit = |
| 10965 | buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); |
| 10966 | auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), |
| 10967 | RefExpr->getExprLoc()); |
| 10968 | AddInitializerToDecl(VDPrivate, |
| 10969 | DefaultLvalueConversion(VDInitRefExpr).get(), |
| 10970 | /*DirectInit=*/false, /*TypeMayContainAuto=*/false); |
| 10971 | |
| 10972 | // If required, build a capture to implement the privatization initialized |
| 10973 | // with the current list item value. |
| 10974 | DeclRefExpr *Ref = nullptr; |
| 10975 | if (!VD) |
| 10976 | Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); |
| 10977 | MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); |
| 10978 | PrivateCopies.push_back(VDPrivateRefExpr); |
| 10979 | Inits.push_back(VDInitRefExpr); |
| 10980 | |
| 10981 | // We need to add a data sharing attribute for this variable to make sure it |
| 10982 | // is correctly captured. A variable that shows up in a use_device_ptr has |
| 10983 | // similar properties of a first private variable. |
| 10984 | DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); |
| 10985 | |
| 10986 | // Create a mappable component for the list item. List items in this clause |
| 10987 | // only need a component. |
| 10988 | MVLI.VarBaseDeclarations.push_back(D); |
| 10989 | MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); |
| 10990 | MVLI.VarComponents.back().push_back( |
| 10991 | OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 10992 | } |
| 10993 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10994 | if (MVLI.ProcessedVarList.empty()) |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 10995 | return nullptr; |
| 10996 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10997 | return OMPUseDevicePtrClause::Create( |
| 10998 | Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, |
| 10999 | PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents); |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 11000 | } |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11001 | |
| 11002 | OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, |
| 11003 | SourceLocation StartLoc, |
| 11004 | SourceLocation LParenLoc, |
| 11005 | SourceLocation EndLoc) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11006 | MappableVarListInfo MVLI(VarList); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11007 | for (auto &RefExpr : VarList) { |
Kelvin Li | 8437625 | 2016-12-14 15:39:58 +0000 | [diff] [blame] | 11008 | assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11009 | SourceLocation ELoc; |
| 11010 | SourceRange ERange; |
| 11011 | Expr *SimpleRefExpr = RefExpr; |
| 11012 | auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); |
| 11013 | if (Res.second) { |
| 11014 | // It will be analyzed later. |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11015 | MVLI.ProcessedVarList.push_back(RefExpr); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11016 | } |
| 11017 | ValueDecl *D = Res.first; |
| 11018 | if (!D) |
| 11019 | continue; |
| 11020 | |
| 11021 | QualType Type = D->getType(); |
| 11022 | // item should be a pointer or array or reference to pointer or array |
| 11023 | if (!Type.getNonReferenceType()->isPointerType() && |
| 11024 | !Type.getNonReferenceType()->isArrayType()) { |
| 11025 | Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) |
| 11026 | << 0 << RefExpr->getSourceRange(); |
| 11027 | continue; |
| 11028 | } |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11029 | |
| 11030 | // Check if the declaration in the clause does not show up in any data |
| 11031 | // sharing attribute. |
| 11032 | auto DVar = DSAStack->getTopDSA(D, false); |
| 11033 | if (isOpenMPPrivate(DVar.CKind)) { |
| 11034 | Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) |
| 11035 | << getOpenMPClauseName(DVar.CKind) |
| 11036 | << getOpenMPClauseName(OMPC_is_device_ptr) |
| 11037 | << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); |
| 11038 | ReportOriginalDSA(*this, DSAStack, D, DVar); |
| 11039 | continue; |
| 11040 | } |
| 11041 | |
| 11042 | Expr *ConflictExpr; |
| 11043 | if (DSAStack->checkMappableExprComponentListsForDecl( |
David Majnemer | 9d16822 | 2016-08-05 17:44:54 +0000 | [diff] [blame] | 11044 | D, /*CurrentRegionOnly=*/true, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11045 | [&ConflictExpr]( |
| 11046 | OMPClauseMappableExprCommon::MappableExprComponentListRef R, |
| 11047 | OpenMPClauseKind) -> bool { |
| 11048 | ConflictExpr = R.front().getAssociatedExpression(); |
| 11049 | return true; |
| 11050 | })) { |
| 11051 | Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); |
| 11052 | Diag(ConflictExpr->getExprLoc(), diag::note_used_here) |
| 11053 | << ConflictExpr->getSourceRange(); |
| 11054 | continue; |
| 11055 | } |
| 11056 | |
| 11057 | // Store the components in the stack so that they can be used to check |
| 11058 | // against other clauses later on. |
| 11059 | OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); |
| 11060 | DSAStack->addMappableExpressionComponents( |
| 11061 | D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); |
| 11062 | |
| 11063 | // Record the expression we've just processed. |
| 11064 | MVLI.ProcessedVarList.push_back(SimpleRefExpr); |
| 11065 | |
| 11066 | // Create a mappable component for the list item. List items in this clause |
| 11067 | // only need a component. We use a null declaration to signal fields in |
| 11068 | // 'this'. |
| 11069 | assert((isa<DeclRefExpr>(SimpleRefExpr) || |
| 11070 | isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && |
| 11071 | "Unexpected device pointer expression!"); |
| 11072 | MVLI.VarBaseDeclarations.push_back( |
| 11073 | isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); |
| 11074 | MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); |
| 11075 | MVLI.VarComponents.back().push_back(MC); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11076 | } |
| 11077 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11078 | if (MVLI.ProcessedVarList.empty()) |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11079 | return nullptr; |
| 11080 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 11081 | return OMPIsDevicePtrClause::Create( |
| 11082 | Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, |
| 11083 | MVLI.VarBaseDeclarations, MVLI.VarComponents); |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 11084 | } |