Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1 | //===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements stmt-related attribute processing. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/Sema/SemaInternal.h" |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
| 16 | #include "clang/Sema/DelayedDiagnostic.h" |
| 17 | #include "clang/Sema/Lookup.h" |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ScopeInfo.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace sema; |
| 23 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 24 | static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const ParsedAttr &A, |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 25 | SourceRange Range) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 26 | FallThroughAttr Attr(S.Context, A); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 27 | if (!isa<NullStmt>(St)) { |
| 28 | S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 29 | << Attr.getSpelling() << St->getBeginLoc(); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 30 | if (isa<SwitchCase>(St)) { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 31 | SourceLocation L = S.getLocForEndOfToken(Range.getEnd()); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 32 | S.Diag(L, diag::note_fallthrough_insert_semi_fixit) |
| 33 | << FixItHint::CreateInsertion(L, ";"); |
| 34 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 35 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 36 | } |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 37 | auto *FnScope = S.getCurFunction(); |
| 38 | if (FnScope->SwitchStack.empty()) { |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 39 | S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 40 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 41 | } |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 42 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 43 | // If this is spelled as the standard C++17 attribute, but not in C++17, warn |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 44 | // about using it as an extension. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 45 | if (!S.getLangOpts().CPlusPlus17 && A.isCXX11Attribute() && |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 46 | !A.getScopeName()) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 47 | S.Diag(A.getLoc(), diag::ext_cxx17_attr) << A; |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 48 | |
| 49 | FnScope->setHasFallthroughStmt(); |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 50 | return ::new (S.Context) FallThroughAttr(S.Context, A); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 53 | static Attr *handleSuppressAttr(Sema &S, Stmt *St, const ParsedAttr &A, |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 54 | SourceRange Range) { |
| 55 | if (A.getNumArgs() < 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 56 | S.Diag(A.getLoc(), diag::err_attribute_too_few_arguments) << A << 1; |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 57 | return nullptr; |
| 58 | } |
| 59 | |
| 60 | std::vector<StringRef> DiagnosticIdentifiers; |
| 61 | for (unsigned I = 0, E = A.getNumArgs(); I != E; ++I) { |
| 62 | StringRef RuleName; |
| 63 | |
| 64 | if (!S.checkStringLiteralArgumentAttr(A, I, RuleName, nullptr)) |
| 65 | return nullptr; |
| 66 | |
| 67 | // FIXME: Warn if the rule name is unknown. This is tricky because only |
| 68 | // clang-tidy knows about available rules. |
| 69 | DiagnosticIdentifiers.push_back(RuleName); |
| 70 | } |
| 71 | |
| 72 | return ::new (S.Context) SuppressAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 73 | S.Context, A, DiagnosticIdentifiers.data(), DiagnosticIdentifiers.size()); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 76 | static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A, |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 77 | SourceRange) { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 78 | IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0); |
| 79 | IdentifierLoc *OptionLoc = A.getArgAsIdent(1); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 80 | IdentifierLoc *StateLoc = A.getArgAsIdent(2); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 81 | Expr *ValueExpr = A.getArgAsExpr(3); |
| 82 | |
Sjoerd Meijer | 6b61519 | 2019-07-10 13:34:57 +0000 | [diff] [blame] | 83 | StringRef PragmaName = |
| 84 | llvm::StringSwitch<StringRef>(PragmaNameLoc->Ident->getName()) |
| 85 | .Cases("unroll", "nounroll", "unroll_and_jam", "nounroll_and_jam", |
| 86 | PragmaNameLoc->Ident->getName()) |
| 87 | .Default("clang loop"); |
| 88 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 89 | if (St->getStmtClass() != Stmt::DoStmtClass && |
| 90 | St->getStmtClass() != Stmt::ForStmtClass && |
| 91 | St->getStmtClass() != Stmt::CXXForRangeStmtClass && |
| 92 | St->getStmtClass() != Stmt::WhileStmtClass) { |
Sjoerd Meijer | 6b61519 | 2019-07-10 13:34:57 +0000 | [diff] [blame] | 93 | std::string Pragma = "#pragma " + std::string(PragmaName); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 94 | S.Diag(St->getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 95 | return nullptr; |
| 96 | } |
| 97 | |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 98 | LoopHintAttr::OptionType Option; |
| 99 | LoopHintAttr::LoopHintState State; |
Sjoerd Meijer | 6b61519 | 2019-07-10 13:34:57 +0000 | [diff] [blame] | 100 | |
| 101 | auto SetHints = [&Option, &State](LoopHintAttr::OptionType O, |
| 102 | LoopHintAttr::LoopHintState S) { |
| 103 | Option = O; |
| 104 | State = S; |
| 105 | }; |
| 106 | |
| 107 | if (PragmaName == "nounroll") { |
| 108 | SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable); |
| 109 | } else if (PragmaName == "unroll") { |
| 110 | // #pragma unroll N |
| 111 | if (ValueExpr) |
| 112 | SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric); |
| 113 | else |
| 114 | SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable); |
| 115 | } else if (PragmaName == "nounroll_and_jam") { |
| 116 | SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Disable); |
| 117 | } else if (PragmaName == "unroll_and_jam") { |
| 118 | // #pragma unroll_and_jam N |
| 119 | if (ValueExpr) |
| 120 | SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric); |
| 121 | else |
| 122 | SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 123 | } else { |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 124 | // #pragma clang loop ... |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 125 | assert(OptionLoc && OptionLoc->Ident && |
| 126 | "Attribute must have valid option info."); |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 127 | Option = llvm::StringSwitch<LoopHintAttr::OptionType>( |
| 128 | OptionLoc->Ident->getName()) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 129 | .Case("vectorize", LoopHintAttr::Vectorize) |
| 130 | .Case("vectorize_width", LoopHintAttr::VectorizeWidth) |
| 131 | .Case("interleave", LoopHintAttr::Interleave) |
Sjoerd Meijer | a48f58c | 2019-07-25 07:33:13 +0000 | [diff] [blame] | 132 | .Case("vectorize_predicate", LoopHintAttr::VectorizePredicate) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 133 | .Case("interleave_count", LoopHintAttr::InterleaveCount) |
| 134 | .Case("unroll", LoopHintAttr::Unroll) |
| 135 | .Case("unroll_count", LoopHintAttr::UnrollCount) |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 136 | .Case("pipeline", LoopHintAttr::PipelineDisabled) |
| 137 | .Case("pipeline_initiation_interval", |
| 138 | LoopHintAttr::PipelineInitiationInterval) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 139 | .Case("distribute", LoopHintAttr::Distribute) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 140 | .Default(LoopHintAttr::Vectorize); |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 141 | if (Option == LoopHintAttr::VectorizeWidth || |
| 142 | Option == LoopHintAttr::InterleaveCount || |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 143 | Option == LoopHintAttr::UnrollCount || |
| 144 | Option == LoopHintAttr::PipelineInitiationInterval) { |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 145 | assert(ValueExpr && "Attribute must have a valid value expression."); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 146 | if (S.CheckLoopHintExpr(ValueExpr, St->getBeginLoc())) |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 147 | return nullptr; |
| 148 | State = LoopHintAttr::Numeric; |
| 149 | } else if (Option == LoopHintAttr::Vectorize || |
| 150 | Option == LoopHintAttr::Interleave || |
Sjoerd Meijer | a48f58c | 2019-07-25 07:33:13 +0000 | [diff] [blame] | 151 | Option == LoopHintAttr::VectorizePredicate || |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 152 | Option == LoopHintAttr::Unroll || |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 153 | Option == LoopHintAttr::Distribute || |
| 154 | Option == LoopHintAttr::PipelineDisabled) { |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 155 | assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument"); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 156 | if (StateLoc->Ident->isStr("disable")) |
| 157 | State = LoopHintAttr::Disable; |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 158 | else if (StateLoc->Ident->isStr("assume_safety")) |
| 159 | State = LoopHintAttr::AssumeSafety; |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 160 | else if (StateLoc->Ident->isStr("full")) |
| 161 | State = LoopHintAttr::Full; |
| 162 | else if (StateLoc->Ident->isStr("enable")) |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 163 | State = LoopHintAttr::Enable; |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 164 | else |
| 165 | llvm_unreachable("bad loop hint argument"); |
| 166 | } else |
| 167 | llvm_unreachable("bad loop hint"); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 168 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 169 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 170 | return LoopHintAttr::CreateImplicit(S.Context, Option, State, ValueExpr, A); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 173 | static void |
| 174 | CheckForIncompatibleAttributes(Sema &S, |
| 175 | const SmallVectorImpl<const Attr *> &Attrs) { |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 176 | // There are 6 categories of loop hints attributes: vectorize, interleave, |
| 177 | // unroll, unroll_and_jam, pipeline and distribute. Except for distribute they |
| 178 | // come in two variants: a state form and a numeric form. The state form |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 179 | // selectively defaults/enables/disables the transformation for the loop |
| 180 | // (for unroll, default indicates full unrolling rather than enabling the |
| 181 | // transformation). The numeric form form provides an integer hint (for |
| 182 | // example, unroll count) to the transformer. The following array accumulates |
| 183 | // the hints encountered while iterating through the attributes to check for |
| 184 | // compatibility. |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 185 | struct { |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 186 | const LoopHintAttr *StateAttr; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 187 | const LoopHintAttr *NumericAttr; |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 188 | } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, |
Sjoerd Meijer | a48f58c | 2019-07-25 07:33:13 +0000 | [diff] [blame] | 189 | {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}, |
| 190 | {nullptr, nullptr}}; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 191 | |
| 192 | for (const auto *I : Attrs) { |
| 193 | const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I); |
| 194 | |
| 195 | // Skip non loop hint attributes |
| 196 | if (!LH) |
| 197 | continue; |
| 198 | |
Craig Topper | ec9be54 | 2015-12-23 05:44:43 +0000 | [diff] [blame] | 199 | LoopHintAttr::OptionType Option = LH->getOption(); |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 200 | enum { |
| 201 | Vectorize, |
| 202 | Interleave, |
| 203 | Unroll, |
| 204 | UnrollAndJam, |
| 205 | Distribute, |
Sjoerd Meijer | a48f58c | 2019-07-25 07:33:13 +0000 | [diff] [blame] | 206 | Pipeline, |
| 207 | VectorizePredicate |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 208 | } Category; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 209 | switch (Option) { |
| 210 | case LoopHintAttr::Vectorize: |
| 211 | case LoopHintAttr::VectorizeWidth: |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 212 | Category = Vectorize; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 213 | break; |
| 214 | case LoopHintAttr::Interleave: |
| 215 | case LoopHintAttr::InterleaveCount: |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 216 | Category = Interleave; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 217 | break; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 218 | case LoopHintAttr::Unroll: |
| 219 | case LoopHintAttr::UnrollCount: |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 220 | Category = Unroll; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 221 | break; |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 222 | case LoopHintAttr::UnrollAndJam: |
| 223 | case LoopHintAttr::UnrollAndJamCount: |
| 224 | Category = UnrollAndJam; |
| 225 | break; |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 226 | case LoopHintAttr::Distribute: |
| 227 | // Perform the check for duplicated 'distribute' hints. |
| 228 | Category = Distribute; |
| 229 | break; |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 230 | case LoopHintAttr::PipelineDisabled: |
| 231 | case LoopHintAttr::PipelineInitiationInterval: |
| 232 | Category = Pipeline; |
| 233 | break; |
Sjoerd Meijer | a48f58c | 2019-07-25 07:33:13 +0000 | [diff] [blame] | 234 | case LoopHintAttr::VectorizePredicate: |
| 235 | Category = VectorizePredicate; |
| 236 | break; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 237 | }; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 238 | |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 239 | assert(Category < sizeof(HintAttrs) / sizeof(HintAttrs[0])); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 240 | auto &CategoryState = HintAttrs[Category]; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 241 | const LoopHintAttr *PrevAttr; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 242 | if (Option == LoopHintAttr::Vectorize || |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 243 | Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll || |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 244 | Option == LoopHintAttr::UnrollAndJam || |
Sjoerd Meijer | a48f58c | 2019-07-25 07:33:13 +0000 | [diff] [blame] | 245 | Option == LoopHintAttr::VectorizePredicate || |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 246 | Option == LoopHintAttr::PipelineDisabled || |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 247 | Option == LoopHintAttr::Distribute) { |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 248 | // Enable|Disable|AssumeSafety hint. For example, vectorize(enable). |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 249 | PrevAttr = CategoryState.StateAttr; |
| 250 | CategoryState.StateAttr = LH; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 251 | } else { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 252 | // Numeric hint. For example, vectorize_width(8). |
| 253 | PrevAttr = CategoryState.NumericAttr; |
| 254 | CategoryState.NumericAttr = LH; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Tyler Nowicki | 884fc61 | 2014-07-29 17:21:32 +0000 | [diff] [blame] | 257 | PrintingPolicy Policy(S.Context.getLangOpts()); |
| 258 | SourceLocation OptionLoc = LH->getRange().getBegin(); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 259 | if (PrevAttr) |
| 260 | // Cannot specify same type of attribute twice. |
| 261 | S.Diag(OptionLoc, diag::err_pragma_loop_compatibility) |
Tyler Nowicki | 884fc61 | 2014-07-29 17:21:32 +0000 | [diff] [blame] | 262 | << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy) |
| 263 | << LH->getDiagnosticName(Policy); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 264 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 265 | if (CategoryState.StateAttr && CategoryState.NumericAttr && |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 266 | (Category == Unroll || Category == UnrollAndJam || |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 267 | CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) { |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 268 | // Disable hints are not compatible with numeric hints of the same |
| 269 | // category. As a special case, numeric unroll hints are also not |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 270 | // compatible with enable or full form of the unroll pragma because these |
| 271 | // directives indicate full unrolling. |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 272 | S.Diag(OptionLoc, diag::err_pragma_loop_compatibility) |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 273 | << /*Duplicate=*/false |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 274 | << CategoryState.StateAttr->getDiagnosticName(Policy) |
Tyler Nowicki | 884fc61 | 2014-07-29 17:21:32 +0000 | [diff] [blame] | 275 | << CategoryState.NumericAttr->getDiagnosticName(Policy); |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 276 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 280 | static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const ParsedAttr &A, |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 281 | SourceRange Range) { |
Egor Churaev | 24939d4 | 2016-12-13 14:02:35 +0000 | [diff] [blame] | 282 | // Although the feature was introduced only in OpenCL C v2.0 s6.11.5, it's |
| 283 | // useful for OpenCL 1.x too and doesn't require HW support. |
| 284 | // opencl_unroll_hint can have 0 arguments (compiler |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 285 | // determines unrolling factor) or 1 argument (the unroll factor provided |
| 286 | // by the user). |
| 287 | |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 288 | unsigned NumArgs = A.getNumArgs(); |
| 289 | |
| 290 | if (NumArgs > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 291 | S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A << 1; |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 292 | return nullptr; |
| 293 | } |
| 294 | |
| 295 | unsigned UnrollFactor = 0; |
| 296 | |
| 297 | if (NumArgs == 1) { |
| 298 | Expr *E = A.getArgAsExpr(0); |
| 299 | llvm::APSInt ArgVal(32); |
| 300 | |
| 301 | if (!E->isIntegerConstantExpr(ArgVal, S.Context)) { |
| 302 | S.Diag(A.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 303 | << A << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 304 | return nullptr; |
| 305 | } |
| 306 | |
| 307 | int Val = ArgVal.getSExtValue(); |
| 308 | |
| 309 | if (Val <= 0) { |
| 310 | S.Diag(A.getRange().getBegin(), |
| 311 | diag::err_attribute_requires_positive_integer) |
Andrew Savonichev | 1a562348 | 2018-09-17 10:39:46 +0000 | [diff] [blame] | 312 | << A << /* positive */ 0; |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 313 | return nullptr; |
| 314 | } |
| 315 | UnrollFactor = Val; |
| 316 | } |
| 317 | |
| 318 | return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor); |
| 319 | } |
| 320 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 321 | static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &A, |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 322 | SourceRange Range) { |
| 323 | switch (A.getKind()) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 324 | case ParsedAttr::UnknownAttribute: |
Simon Pilgrim | 6d2d96e | 2018-12-17 12:25:42 +0000 | [diff] [blame] | 325 | S.Diag(A.getLoc(), A.isDeclspecAttribute() |
| 326 | ? (unsigned)diag::warn_unhandled_ms_attribute_ignored |
| 327 | : (unsigned)diag::warn_unknown_attribute_ignored) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 328 | << A; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 329 | return nullptr; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 330 | case ParsedAttr::AT_FallThrough: |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 331 | return handleFallThroughAttr(S, St, A, Range); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 332 | case ParsedAttr::AT_LoopHint: |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 333 | return handleLoopHintAttr(S, St, A, Range); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 334 | case ParsedAttr::AT_OpenCLUnrollHint: |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 335 | return handleOpenCLUnrollHint(S, St, A, Range); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 336 | case ParsedAttr::AT_Suppress: |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 337 | return handleSuppressAttr(S, St, A, Range); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 338 | default: |
| 339 | // if we're here, then we parsed a known attribute, but didn't recognize |
| 340 | // it as a statement attribute => it is declaration attribute |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 341 | S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 342 | << A << St->getBeginLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 343 | return nullptr; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 347 | StmtResult Sema::ProcessStmtAttributes(Stmt *S, |
| 348 | const ParsedAttributesView &AttrList, |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 349 | SourceRange Range) { |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 350 | SmallVector<const Attr*, 8> Attrs; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 351 | for (const ParsedAttr &AL : AttrList) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 352 | if (Attr *a = ProcessStmtAttribute(*this, S, AL, Range)) |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 353 | Attrs.push_back(a); |
| 354 | } |
| 355 | |
| 356 | CheckForIncompatibleAttributes(*this, Attrs); |
| 357 | |
| 358 | if (Attrs.empty()) |
| 359 | return S; |
| 360 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 361 | return ActOnAttributedStmt(Range.getBegin(), Attrs, S); |
| 362 | } |