Debian Codegraph @

flang/lib/Semantics/check-acc-structure.cpp

raw
1 //===-- lib/Semantics/check-acc-structure.cpp -----------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 #include "check-acc-structure.h"
9 #include "flang/Common/enum-set.h"
10 #include "flang/Parser/parse-tree.h"
11 #include "flang/Semantics/tools.h"
12
13 #define CHECK_SIMPLE_CLAUSE(X, Y) \
14 void AccStructureChecker::Enter(const parser::AccClause::X &) { \
15 CheckAllowed(llvm::acc::Clause::Y); \
16 }
17
18 #define CHECK_REQ_SCALAR_INT_CONSTANT_CLAUSE(X, Y) \
19 void AccStructureChecker::Enter(const parser::AccClause::X &c) { \
20 CheckAllowed(llvm::acc::Clause::Y); \
21 RequiresConstantPositiveParameter(llvm::acc::Clause::Y, c.v); \
22 }
23
24 using ReductionOpsSet =
25 Fortran::common::EnumSet<Fortran::parser::ReductionOperator::Operator,
26 Fortran::parser::ReductionOperator::Operator_enumSize>;
27
28 static ReductionOpsSet reductionIntegerSet{
29 Fortran::parser::ReductionOperator::Operator::Plus,
30 Fortran::parser::ReductionOperator::Operator::Multiply,
31 Fortran::parser::ReductionOperator::Operator::Max,
32 Fortran::parser::ReductionOperator::Operator::Min,
33 Fortran::parser::ReductionOperator::Operator::Iand,
34 Fortran::parser::ReductionOperator::Operator::Ior,
35 Fortran::parser::ReductionOperator::Operator::Ieor};
36
37 static ReductionOpsSet reductionRealSet{
38 Fortran::parser::ReductionOperator::Operator::Plus,
39 Fortran::parser::ReductionOperator::Operator::Multiply,
40 Fortran::parser::ReductionOperator::Operator::Max,
41 Fortran::parser::ReductionOperator::Operator::Min};
42
43 static ReductionOpsSet reductionComplexSet{
44 Fortran::parser::ReductionOperator::Operator::Plus,
45 Fortran::parser::ReductionOperator::Operator::Multiply};
46
47 static ReductionOpsSet reductionLogicalSet{
48 Fortran::parser::ReductionOperator::Operator::And,
49 Fortran::parser::ReductionOperator::Operator::Or,
50 Fortran::parser::ReductionOperator::Operator::Eqv,
51 Fortran::parser::ReductionOperator::Operator::Neqv};
52
53 namespace Fortran::semantics {
54
55 static constexpr inline AccClauseSet
56 computeConstructOnlyAllowedAfterDeviceTypeClauses{
57 llvm::acc::Clause::ACCC_async, llvm::acc::Clause::ACCC_wait,
58 llvm::acc::Clause::ACCC_num_gangs, llvm::acc::Clause::ACCC_num_workers,
59 llvm::acc::Clause::ACCC_vector_length};
60
61 static constexpr inline AccClauseSet loopOnlyAllowedAfterDeviceTypeClauses{
62 llvm::acc::Clause::ACCC_auto, llvm::acc::Clause::ACCC_collapse,
63 llvm::acc::Clause::ACCC_independent, llvm::acc::Clause::ACCC_gang,
64 llvm::acc::Clause::ACCC_seq, llvm::acc::Clause::ACCC_tile,
65 llvm::acc::Clause::ACCC_vector, llvm::acc::Clause::ACCC_worker};
66
67 static constexpr inline AccClauseSet updateOnlyAllowedAfterDeviceTypeClauses{
68 llvm::acc::Clause::ACCC_async, llvm::acc::Clause::ACCC_wait};
69
70 static constexpr inline AccClauseSet routineOnlyAllowedAfterDeviceTypeClauses{
71 llvm::acc::Clause::ACCC_bind, llvm::acc::Clause::ACCC_gang,
72 llvm::acc::Clause::ACCC_vector, llvm::acc::Clause::ACCC_worker,
73 llvm::acc::Clause::ACCC_seq};
74
75 static constexpr inline AccClauseSet routineMutuallyExclusiveClauses{
76 llvm::acc::Clause::ACCC_gang, llvm::acc::Clause::ACCC_worker,
77 llvm::acc::Clause::ACCC_vector, llvm::acc::Clause::ACCC_seq};
78
79 bool AccStructureChecker::CheckAllowedModifier(llvm::acc::Clause clause) {
80 if (GetContext().directive == llvm::acc::ACCD_enter_data ||
81 GetContext().directive == llvm::acc::ACCD_exit_data) {
82 context_.Say(GetContext().clauseSource,
83 "Modifier is not allowed for the %s clause "
84 "on the %s directive"_err_en_US,
85 parser::ToUpperCaseLetters(getClauseName(clause).str()),
86 ContextDirectiveAsFortran());
87 return true;
88 }
89 return false;
90 }
91
92 bool AccStructureChecker::IsComputeConstruct(
93 llvm::acc::Directive directive) const {
94 return directive == llvm::acc::ACCD_parallel ||
95 directive == llvm::acc::ACCD_parallel_loop ||
96 directive == llvm::acc::ACCD_serial ||
97 directive == llvm::acc::ACCD_serial_loop ||
98 directive == llvm::acc::ACCD_kernels ||
99 directive == llvm::acc::ACCD_kernels_loop;
100 }
101
102 bool AccStructureChecker::IsInsideComputeConstruct() const {
103 if (dirContext_.size() <= 1) {
104 return false;
105 }
106
107 // Check all nested context skipping the first one.
108 for (std::size_t i = dirContext_.size() - 1; i > 0; --i) {
109 if (IsComputeConstruct(dirContext_[i - 1].directive)) {
110 return true;
111 }
112 }
113 return false;
114 }
115
116 void AccStructureChecker::CheckNotInComputeConstruct() {
117 if (IsInsideComputeConstruct()) {
118 context_.Say(GetContext().directiveSource,
119 "Directive %s may not be called within a compute region"_err_en_US,
120 ContextDirectiveAsFortran());
121 }
122 }
123
124 void AccStructureChecker::Enter(const parser::AccClause &x) {
125 SetContextClause(x);
126 }
127
128 void AccStructureChecker::Leave(const parser::AccClauseList &) {}
129
130 void AccStructureChecker::Enter(const parser::OpenACCBlockConstruct &x) {
131 const auto &beginBlockDir{std::get<parser::AccBeginBlockDirective>(x.t)};
132 const auto &endBlockDir{std::get<parser::AccEndBlockDirective>(x.t)};
133 const auto &beginAccBlockDir{
134 std::get<parser::AccBlockDirective>(beginBlockDir.t)};
135
136 CheckMatching(beginAccBlockDir, endBlockDir.v);
137 PushContextAndClauseSets(beginAccBlockDir.source, beginAccBlockDir.v);
138 }
139
140 void AccStructureChecker::Leave(const parser::OpenACCBlockConstruct &x) {
141 const auto &beginBlockDir{std::get<parser::AccBeginBlockDirective>(x.t)};
142 const auto &blockDir{std::get<parser::AccBlockDirective>(beginBlockDir.t)};
143 const parser::Block &block{std::get<parser::Block>(x.t)};
144 switch (blockDir.v) {
145 case llvm::acc::Directive::ACCD_kernels:
146 case llvm::acc::Directive::ACCD_parallel:
147 case llvm::acc::Directive::ACCD_serial:
148 // Restriction - line 1004-1005
149 CheckOnlyAllowedAfter(llvm::acc::Clause::ACCC_device_type,
150 computeConstructOnlyAllowedAfterDeviceTypeClauses);
151 // Restriction - line 1001
152 CheckNoBranching(block, GetContext().directive, blockDir.source);
153 break;
154 case llvm::acc::Directive::ACCD_data:
155 // Restriction - 2.6.5 pt 1
156 // Only a warning is emitted here for portability reason.
157 CheckRequireAtLeastOneOf(/*warnInsteadOfError=*/true);
158 // Restriction is not formally in the specification but all compilers emit
159 // an error and it is likely to be omitted from the spec.
160 CheckNoBranching(block, GetContext().directive, blockDir.source);
161 break;
162 case llvm::acc::Directive::ACCD_host_data:
163 // Restriction - line 1746
164 CheckRequireAtLeastOneOf();
165 break;
166 default:
167 break;
168 }
169 dirContext_.pop_back();
170 }
171
172 void AccStructureChecker::Enter(
173 const parser::OpenACCStandaloneDeclarativeConstruct &x) {
174 const auto &declarativeDir{std::get<parser::AccDeclarativeDirective>(x.t)};
175 PushContextAndClauseSets(declarativeDir.source, declarativeDir.v);
176 }
177
178 void AccStructureChecker::Leave(
179 const parser::OpenACCStandaloneDeclarativeConstruct &x) {
180 // Restriction - line 2409
181 CheckAtLeastOneClause();
182
183 // Restriction - line 2417-2418 - In a Fortran module declaration section,
184 // only create, copyin, device_resident, and link clauses are allowed.
185 const auto &declarativeDir{std::get<parser::AccDeclarativeDirective>(x.t)};
186 const auto &scope{context_.FindScope(declarativeDir.source)};
187 const Scope &containingScope{GetProgramUnitContaining(scope)};
188 if (containingScope.kind() == Scope::Kind::Module) {
189 for (auto cl : GetContext().actualClauses) {
190 if (cl != llvm::acc::Clause::ACCC_create &&
191 cl != llvm::acc::Clause::ACCC_copyin &&
192 cl != llvm::acc::Clause::ACCC_device_resident &&
193 cl != llvm::acc::Clause::ACCC_link) {
194 context_.Say(GetContext().directiveSource,
195 "%s clause is not allowed on the %s directive in module "
196 "declaration "
197 "section"_err_en_US,
198 parser::ToUpperCaseLetters(
199 llvm::acc::getOpenACCClauseName(cl).str()),
200 ContextDirectiveAsFortran());
201 }
202 }
203 }
204 dirContext_.pop_back();
205 }
206
207 void AccStructureChecker::Enter(const parser::OpenACCCombinedConstruct &x) {
208 const auto &beginCombinedDir{
209 std::get<parser::AccBeginCombinedDirective>(x.t)};
210 const auto &combinedDir{
211 std::get<parser::AccCombinedDirective>(beginCombinedDir.t)};
212
213 // check matching, End directive is optional
214 if (const auto &endCombinedDir{
215 std::get<std::optional<parser::AccEndCombinedDirective>>(x.t)}) {
216 CheckMatching<parser::AccCombinedDirective>(combinedDir, endCombinedDir->v);
217 }
218
219 PushContextAndClauseSets(combinedDir.source, combinedDir.v);
220 }
221
222 void AccStructureChecker::Leave(const parser::OpenACCCombinedConstruct &x) {
223 const auto &beginBlockDir{std::get<parser::AccBeginCombinedDirective>(x.t)};
224 const auto &combinedDir{
225 std::get<parser::AccCombinedDirective>(beginBlockDir.t)};
226 auto &doCons{std::get<std::optional<parser::DoConstruct>>(x.t)};
227 switch (combinedDir.v) {
228 case llvm::acc::Directive::ACCD_kernels_loop:
229 case llvm::acc::Directive::ACCD_parallel_loop:
230 case llvm::acc::Directive::ACCD_serial_loop:
231 // Restriction - line 1004-1005
232 CheckOnlyAllowedAfter(llvm::acc::Clause::ACCC_device_type,
233 computeConstructOnlyAllowedAfterDeviceTypeClauses |
234 loopOnlyAllowedAfterDeviceTypeClauses);
235 if (doCons) {
236 const parser::Block &block{std::get<parser::Block>(doCons->t)};
237 CheckNoBranching(block, GetContext().directive, beginBlockDir.source);
238 }
239 break;
240 default:
241 break;
242 }
243 dirContext_.pop_back();
244 }
245
246 void AccStructureChecker::Enter(const parser::OpenACCLoopConstruct &x) {
247 const auto &beginDir{std::get<parser::AccBeginLoopDirective>(x.t)};
248 const auto &loopDir{std::get<parser::AccLoopDirective>(beginDir.t)};
249 PushContextAndClauseSets(loopDir.source, loopDir.v);
250 }
251
252 void AccStructureChecker::Leave(const parser::OpenACCLoopConstruct &x) {
253 const auto &beginDir{std::get<parser::AccBeginLoopDirective>(x.t)};
254 const auto &loopDir{std::get<parser::AccLoopDirective>(beginDir.t)};
255 if (loopDir.v == llvm::acc::Directive::ACCD_loop) {
256 // Restriction - line 1818-1819
257 CheckOnlyAllowedAfter(llvm::acc::Clause::ACCC_device_type,
258 loopOnlyAllowedAfterDeviceTypeClauses);
259 // Restriction - line 1834
260 CheckNotAllowedIfClause(llvm::acc::Clause::ACCC_seq,
261 {llvm::acc::Clause::ACCC_gang, llvm::acc::Clause::ACCC_vector,
262 llvm::acc::Clause::ACCC_worker});
263 }
264 dirContext_.pop_back();
265 }
266
267 void AccStructureChecker::Enter(const parser::OpenACCStandaloneConstruct &x) {
268 const auto &standaloneDir{std::get<parser::AccStandaloneDirective>(x.t)};
269 PushContextAndClauseSets(standaloneDir.source, standaloneDir.v);
270 }
271
272 void AccStructureChecker::Leave(const parser::OpenACCStandaloneConstruct &x) {
273 const auto &standaloneDir{std::get<parser::AccStandaloneDirective>(x.t)};
274 switch (standaloneDir.v) {
275 case llvm::acc::Directive::ACCD_enter_data:
276 case llvm::acc::Directive::ACCD_exit_data:
277 // Restriction - line 1310-1311 (ENTER DATA)
278 // Restriction - line 1312-1313 (EXIT DATA)
279 CheckRequireAtLeastOneOf();
280 break;
281 case llvm::acc::Directive::ACCD_set:
282 // Restriction - line 2610
283 CheckRequireAtLeastOneOf();
284 // Restriction - line 2602
285 CheckNotInComputeConstruct();
286 break;
287 case llvm::acc::Directive::ACCD_update:
288 // Restriction - line 2636
289 CheckRequireAtLeastOneOf();
290 // Restriction - line 2669
291 CheckOnlyAllowedAfter(llvm::acc::Clause::ACCC_device_type,
292 updateOnlyAllowedAfterDeviceTypeClauses);
293 break;
294 case llvm::acc::Directive::ACCD_init:
295 case llvm::acc::Directive::ACCD_shutdown:
296 // Restriction - line 2525 (INIT)
297 // Restriction - line 2561 (SHUTDOWN)
298 CheckNotInComputeConstruct();
299 break;
300 default:
301 break;
302 }
303 dirContext_.pop_back();
304 }
305
306 void AccStructureChecker::Enter(const parser::OpenACCRoutineConstruct &x) {
307 PushContextAndClauseSets(x.source, llvm::acc::Directive::ACCD_routine);
308 const auto &optName{std::get<std::optional<parser::Name>>(x.t)};
309 if (!optName) {
310 const auto &verbatim{std::get<parser::Verbatim>(x.t)};
311 const auto &scope{context_.FindScope(verbatim.source)};
312 const Scope &containingScope{GetProgramUnitContaining(scope)};
313 if (containingScope.kind() == Scope::Kind::Module) {
314 context_.Say(GetContext().directiveSource,
315 "ROUTINE directive without name must appear within the specification "
316 "part of a subroutine or function definition, or within an interface "
317 "body for a subroutine or function in an interface block"_err_en_US);
318 }
319 }
320 }
321 void AccStructureChecker::Leave(const parser::OpenACCRoutineConstruct &) {
322 // Restriction - line 2790
323 CheckRequireAtLeastOneOf();
324 // Restriction - line 2788-2789
325 CheckOnlyAllowedAfter(llvm::acc::Clause::ACCC_device_type,
326 routineOnlyAllowedAfterDeviceTypeClauses);
327 dirContext_.pop_back();
328 }
329
330 void AccStructureChecker::Enter(const parser::OpenACCWaitConstruct &x) {
331 const auto &verbatim{std::get<parser::Verbatim>(x.t)};
332 PushContextAndClauseSets(verbatim.source, llvm::acc::Directive::ACCD_wait);
333 }
334 void AccStructureChecker::Leave(const parser::OpenACCWaitConstruct &x) {
335 dirContext_.pop_back();
336 }
337
338 void AccStructureChecker::Enter(const parser::OpenACCAtomicConstruct &x) {
339 PushContextAndClauseSets(x.source, llvm::acc::Directive::ACCD_atomic);
340 }
341 void AccStructureChecker::Leave(const parser::OpenACCAtomicConstruct &x) {
342 dirContext_.pop_back();
343 }
344
345 void AccStructureChecker::Enter(const parser::AccAtomicUpdate &x) {
346 const parser::AssignmentStmt &assignment{
347 std::get<parser::Statement<parser::AssignmentStmt>>(x.t).statement};
348 const auto &var{std::get<parser::Variable>(assignment.t)};
349 const auto &expr{std::get<parser::Expr>(assignment.t)};
350 const auto *rhs{GetExpr(context_, expr)};
351 const auto *lhs{GetExpr(context_, var)};
352 if (lhs && rhs) {
353 if (lhs->Rank() != 0)
354 context_.Say(expr.source,
355 "LHS of atomic update statement must be scalar"_err_en_US);
356 if (rhs->Rank() != 0)
357 context_.Say(var.GetSource(),
358 "RHS of atomic update statement must be scalar"_err_en_US);
359 }
360 }
361
362 void AccStructureChecker::Enter(const parser::OpenACCCacheConstruct &x) {
363 const auto &verbatim = std::get<parser::Verbatim>(x.t);
364 PushContextAndClauseSets(verbatim.source, llvm::acc::Directive::ACCD_cache);
365 SetContextDirectiveSource(verbatim.source);
366 if (loopNestLevel == 0) {
367 context_.Say(verbatim.source,
368 "The CACHE directive must be inside a loop"_err_en_US);
369 }
370 }
371 void AccStructureChecker::Leave(const parser::OpenACCCacheConstruct &x) {
372 dirContext_.pop_back();
373 }
374
375 // Clause checkers
376 CHECK_SIMPLE_CLAUSE(Auto, ACCC_auto)
377 CHECK_SIMPLE_CLAUSE(Async, ACCC_async)
378 CHECK_SIMPLE_CLAUSE(Attach, ACCC_attach)
379 CHECK_SIMPLE_CLAUSE(Bind, ACCC_bind)
380 CHECK_SIMPLE_CLAUSE(Capture, ACCC_capture)
381 CHECK_SIMPLE_CLAUSE(Default, ACCC_default)
382 CHECK_SIMPLE_CLAUSE(DefaultAsync, ACCC_default_async)
383 CHECK_SIMPLE_CLAUSE(Delete, ACCC_delete)
384 CHECK_SIMPLE_CLAUSE(Detach, ACCC_detach)
385 CHECK_SIMPLE_CLAUSE(Device, ACCC_device)
386 CHECK_SIMPLE_CLAUSE(DeviceNum, ACCC_device_num)
387 CHECK_SIMPLE_CLAUSE(Finalize, ACCC_finalize)
388 CHECK_SIMPLE_CLAUSE(Firstprivate, ACCC_firstprivate)
389 CHECK_SIMPLE_CLAUSE(Host, ACCC_host)
390 CHECK_SIMPLE_CLAUSE(IfPresent, ACCC_if_present)
391 CHECK_SIMPLE_CLAUSE(Independent, ACCC_independent)
392 CHECK_SIMPLE_CLAUSE(NoCreate, ACCC_no_create)
393 CHECK_SIMPLE_CLAUSE(Nohost, ACCC_nohost)
394 CHECK_SIMPLE_CLAUSE(Private, ACCC_private)
395 CHECK_SIMPLE_CLAUSE(Read, ACCC_read)
396 CHECK_SIMPLE_CLAUSE(UseDevice, ACCC_use_device)
397 CHECK_SIMPLE_CLAUSE(Wait, ACCC_wait)
398 CHECK_SIMPLE_CLAUSE(Write, ACCC_write)
399 CHECK_SIMPLE_CLAUSE(Unknown, ACCC_unknown)
400
401 void AccStructureChecker::CheckMultipleOccurrenceInDeclare(
402 const parser::AccObjectList &list, llvm::acc::Clause clause) {
403 if (GetContext().directive != llvm::acc::Directive::ACCD_declare)
404 return;
405 for (const auto &object : list.v) {
406 common::visit(
407 common::visitors{
408 [&](const parser::Designator &designator) {
409 if (const auto *name = getDesignatorNameIfDataRef(designator)) {
410 if (declareSymbols.contains(&name->symbol->GetUltimate())) {
411 if (declareSymbols[&name->symbol->GetUltimate()] == clause) {
412 if (context_.languageFeatures().ShouldWarn(
413 common::UsageWarning::OpenAccUsage)) {
414 context_.Say(GetContext().clauseSource,
415 "'%s' in the %s clause is already present in the "
416 "same "
417 "clause in this module"_warn_en_US,
418 name->symbol->name(),
419 parser::ToUpperCaseLetters(
420 llvm::acc::getOpenACCClauseName(clause).str()));
421 }
422 } else {
423 context_.Say(GetContext().clauseSource,
424 "'%s' in the %s clause is already present in another "
425 "%s clause in this module"_err_en_US,
426 name->symbol->name(),
427 parser::ToUpperCaseLetters(
428 llvm::acc::getOpenACCClauseName(clause).str()),
429 parser::ToUpperCaseLetters(
430 llvm::acc::getOpenACCClauseName(
431 declareSymbols[&name->symbol->GetUltimate()])
432 .str()));
433 }
434 }
435 declareSymbols.insert({&name->symbol->GetUltimate(), clause});
436 }
437 },
438 [&](const parser::Name &name) {
439 // TODO: check common block
440 }},
441 object.u);
442 }
443 }
444
445 void AccStructureChecker::CheckMultipleOccurrenceInDeclare(
446 const parser::AccObjectListWithModifier &list, llvm::acc::Clause clause) {
447 const auto &objectList = std::get<Fortran::parser::AccObjectList>(list.t);
448 CheckMultipleOccurrenceInDeclare(objectList, clause);
449 }
450
451 void AccStructureChecker::Enter(const parser::AccClause::Create &c) {
452 CheckAllowed(llvm::acc::Clause::ACCC_create);
453 const auto &modifierClause{c.v};
454 if (const auto &modifier{
455 std::get<std::optional<parser::AccDataModifier>>(modifierClause.t)}) {
456 if (modifier->v != parser::AccDataModifier::Modifier::Zero) {
457 context_.Say(GetContext().clauseSource,
458 "Only the ZERO modifier is allowed for the %s clause "
459 "on the %s directive"_err_en_US,
460 parser::ToUpperCaseLetters(
461 llvm::acc::getOpenACCClauseName(llvm::acc::Clause::ACCC_create)
462 .str()),
463 ContextDirectiveAsFortran());
464 }
465 if (GetContext().directive == llvm::acc::Directive::ACCD_declare) {
466 context_.Say(GetContext().clauseSource,
467 "The ZERO modifier is not allowed for the %s clause "
468 "on the %s directive"_err_en_US,
469 parser::ToUpperCaseLetters(
470 llvm::acc::getOpenACCClauseName(llvm::acc::Clause::ACCC_create)
471 .str()),
472 ContextDirectiveAsFortran());
473 }
474 }
475 CheckMultipleOccurrenceInDeclare(
476 modifierClause, llvm::acc::Clause::ACCC_create);
477 }
478
479 void AccStructureChecker::Enter(const parser::AccClause::Copyin &c) {
480 CheckAllowed(llvm::acc::Clause::ACCC_copyin);
481 const auto &modifierClause{c.v};
482 if (const auto &modifier{
483 std::get<std::optional<parser::AccDataModifier>>(modifierClause.t)}) {
484 if (CheckAllowedModifier(llvm::acc::Clause::ACCC_copyin)) {
485 return;
486 }
487 if (modifier->v != parser::AccDataModifier::Modifier::ReadOnly) {
488 context_.Say(GetContext().clauseSource,
489 "Only the READONLY modifier is allowed for the %s clause "
490 "on the %s directive"_err_en_US,
491 parser::ToUpperCaseLetters(
492 llvm::acc::getOpenACCClauseName(llvm::acc::Clause::ACCC_copyin)
493 .str()),
494 ContextDirectiveAsFortran());
495 }
496 }
497 CheckMultipleOccurrenceInDeclare(
498 modifierClause, llvm::acc::Clause::ACCC_copyin);
499 }
500
501 void AccStructureChecker::Enter(const parser::AccClause::Copyout &c) {
502 CheckAllowed(llvm::acc::Clause::ACCC_copyout);
503 const auto &modifierClause{c.v};
504 if (const auto &modifier{
505 std::get<std::optional<parser::AccDataModifier>>(modifierClause.t)}) {
506 if (CheckAllowedModifier(llvm::acc::Clause::ACCC_copyout)) {
507 return;
508 }
509 if (modifier->v != parser::AccDataModifier::Modifier::Zero) {
510 context_.Say(GetContext().clauseSource,
511 "Only the ZERO modifier is allowed for the %s clause "
512 "on the %s directive"_err_en_US,
513 parser::ToUpperCaseLetters(
514 llvm::acc::getOpenACCClauseName(llvm::acc::Clause::ACCC_copyout)
515 .str()),
516 ContextDirectiveAsFortran());
517 }
518 if (GetContext().directive == llvm::acc::Directive::ACCD_declare) {
519 context_.Say(GetContext().clauseSource,
520 "The ZERO modifier is not allowed for the %s clause "
521 "on the %s directive"_err_en_US,
522 parser::ToUpperCaseLetters(
523 llvm::acc::getOpenACCClauseName(llvm::acc::Clause::ACCC_copyout)
524 .str()),
525 ContextDirectiveAsFortran());
526 }
527 }
528 CheckMultipleOccurrenceInDeclare(
529 modifierClause, llvm::acc::Clause::ACCC_copyout);
530 }
531
532 void AccStructureChecker::Enter(const parser::AccClause::DeviceType &d) {
533 CheckAllowed(llvm::acc::Clause::ACCC_device_type);
534 if (GetContext().directive == llvm::acc::Directive::ACCD_set &&
535 d.v.v.size() > 1) {
536 context_.Say(GetContext().clauseSource,
537 "The %s clause on the %s directive accepts only one value"_err_en_US,
538 parser::ToUpperCaseLetters(
539 llvm::acc::getOpenACCClauseName(llvm::acc::Clause::ACCC_device_type)
540 .str()),
541 ContextDirectiveAsFortran());
542 }
543 ResetCrtGroup();
544 }
545
546 void AccStructureChecker::Enter(const parser::AccClause::Seq &g) {
547 llvm::acc::Clause crtClause = llvm::acc::Clause::ACCC_seq;
548 if (GetContext().directive == llvm::acc::Directive::ACCD_routine) {
549 CheckMutuallyExclusivePerGroup(crtClause,
550 llvm::acc::Clause::ACCC_device_type, routineMutuallyExclusiveClauses);
551 }
552 CheckAllowed(crtClause);
553 }
554
555 void AccStructureChecker::Enter(const parser::AccClause::Vector &g) {
556 llvm::acc::Clause crtClause = llvm::acc::Clause::ACCC_vector;
557 if (GetContext().directive == llvm::acc::Directive::ACCD_routine) {
558 CheckMutuallyExclusivePerGroup(crtClause,
559 llvm::acc::Clause::ACCC_device_type, routineMutuallyExclusiveClauses);
560 }
561 CheckAllowed(crtClause);
562 if (GetContext().directive != llvm::acc::Directive::ACCD_routine) {
563 CheckAllowedOncePerGroup(crtClause, llvm::acc::Clause::ACCC_device_type);
564 }
565 }
566
567 void AccStructureChecker::Enter(const parser::AccClause::Worker &g) {
568 llvm::acc::Clause crtClause = llvm::acc::Clause::ACCC_worker;
569 if (GetContext().directive == llvm::acc::Directive::ACCD_routine) {
570 CheckMutuallyExclusivePerGroup(crtClause,
571 llvm::acc::Clause::ACCC_device_type, routineMutuallyExclusiveClauses);
572 }
573 CheckAllowed(crtClause);
574 if (GetContext().directive != llvm::acc::Directive::ACCD_routine) {
575 CheckAllowedOncePerGroup(crtClause, llvm::acc::Clause::ACCC_device_type);
576 }
577 }
578
579 void AccStructureChecker::Enter(const parser::AccClause::Tile &g) {
580 CheckAllowed(llvm::acc::Clause::ACCC_tile);
581 CheckAllowedOncePerGroup(
582 llvm::acc::Clause::ACCC_tile, llvm::acc::Clause::ACCC_device_type);
583 }
584
585 void AccStructureChecker::Enter(const parser::AccClause::Gang &g) {
586 llvm::acc::Clause crtClause = llvm::acc::Clause::ACCC_gang;
587 if (GetContext().directive == llvm::acc::Directive::ACCD_routine) {
588 CheckMutuallyExclusivePerGroup(crtClause,
589 llvm::acc::Clause::ACCC_device_type, routineMutuallyExclusiveClauses);
590 }
591 CheckAllowed(crtClause);
592 if (GetContext().directive != llvm::acc::Directive::ACCD_routine) {
593 CheckAllowedOncePerGroup(crtClause, llvm::acc::Clause::ACCC_device_type);
594 }
595
596 if (g.v) {
597 bool hasNum = false;
598 bool hasDim = false;
599 bool hasStatic = false;
600 const Fortran::parser::AccGangArgList &x = *g.v;
601 for (const Fortran::parser::AccGangArg &gangArg : x.v) {
602 if (std::get_if<Fortran::parser::AccGangArg::Num>(&gangArg.u)) {
603 hasNum = true;
604 } else if (std::get_if<Fortran::parser::AccGangArg::Dim>(&gangArg.u)) {
605 hasDim = true;
606 } else if (std::get_if<Fortran::parser::AccGangArg::Static>(&gangArg.u)) {
607 hasStatic = true;
608 }
609 }
610
611 if (GetContext().directive == llvm::acc::Directive::ACCD_routine &&
612 (hasStatic || hasNum)) {
613 context_.Say(GetContext().clauseSource,
614 "Only the dim argument is allowed on the %s clause on the %s directive"_err_en_US,
615 parser::ToUpperCaseLetters(
616 llvm::acc::getOpenACCClauseName(llvm::acc::Clause::ACCC_gang)
617 .str()),
618 ContextDirectiveAsFortran());
619 }
620
621 if (hasDim && hasNum) {
622 context_.Say(GetContext().clauseSource,
623 "The num argument is not allowed when dim is specified"_err_en_US);
624 }
625 }
626 }
627
628 void AccStructureChecker::Enter(const parser::AccClause::NumGangs &n) {
629 CheckAllowed(llvm::acc::Clause::ACCC_num_gangs,
630 /*warnInsteadOfError=*/GetContext().directive ==
631 llvm::acc::Directive::ACCD_serial ||
632 GetContext().directive == llvm::acc::Directive::ACCD_serial_loop);
633 CheckAllowedOncePerGroup(
634 llvm::acc::Clause::ACCC_num_gangs, llvm::acc::Clause::ACCC_device_type);
635
636 if (n.v.size() > 3)
637 context_.Say(GetContext().clauseSource,
638 "NUM_GANGS clause accepts a maximum of 3 arguments"_err_en_US);
639 }
640
641 void AccStructureChecker::Enter(const parser::AccClause::NumWorkers &n) {
642 CheckAllowed(llvm::acc::Clause::ACCC_num_workers,
643 /*warnInsteadOfError=*/GetContext().directive ==
644 llvm::acc::Directive::ACCD_serial ||
645 GetContext().directive == llvm::acc::Directive::ACCD_serial_loop);
646 CheckAllowedOncePerGroup(
647 llvm::acc::Clause::ACCC_num_workers, llvm::acc::Clause::ACCC_device_type);
648 }
649
650 void AccStructureChecker::Enter(const parser::AccClause::VectorLength &n) {
651 CheckAllowed(llvm::acc::Clause::ACCC_vector_length,
652 /*warnInsteadOfError=*/GetContext().directive ==
653 llvm::acc::Directive::ACCD_serial ||
654 GetContext().directive == llvm::acc::Directive::ACCD_serial_loop);
655 CheckAllowedOncePerGroup(llvm::acc::Clause::ACCC_vector_length,
656 llvm::acc::Clause::ACCC_device_type);
657 }
658
659 void AccStructureChecker::Enter(const parser::AccClause::Reduction &reduction) {
660 CheckAllowed(llvm::acc::Clause::ACCC_reduction);
661
662 // From OpenACC 3.3
663 // At a minimum, the supported data types include Fortran logical as well as
664 // the numerical data types (e.g. integer, real, double precision, complex).
665 // However, for each reduction operator, the supported data types include only
666 // the types permitted as operands to the corresponding operator in the base
667 // language where (1) for max and min, the corresponding operator is less-than
668 // and (2) for other operators, the operands and the result are the same type.
669 //
670 // The following check that the reduction operator is supported with the given
671 // type.
672 const parser::AccObjectListWithReduction &list{reduction.v};
673 const auto &op{std::get<parser::ReductionOperator>(list.t)};
674 const auto &objects{std::get<parser::AccObjectList>(list.t)};
675
676 for (const auto &object : objects.v) {
677 common::visit(
678 common::visitors{
679 [&](const parser::Designator &designator) {
680 if (const auto *name = getDesignatorNameIfDataRef(designator)) {
681 const auto *type{name->symbol->GetType()};
682 if (type->IsNumeric(TypeCategory::Integer) &&
683 !reductionIntegerSet.test(op.v)) {
684 context_.Say(GetContext().clauseSource,
685 "reduction operator not supported for integer type"_err_en_US);
686 } else if (type->IsNumeric(TypeCategory::Real) &&
687 !reductionRealSet.test(op.v)) {
688 context_.Say(GetContext().clauseSource,
689 "reduction operator not supported for real type"_err_en_US);
690 } else if (type->IsNumeric(TypeCategory::Complex) &&
691 !reductionComplexSet.test(op.v)) {
692 context_.Say(GetContext().clauseSource,
693 "reduction operator not supported for complex type"_err_en_US);
694 } else if (type->category() ==
695 Fortran::semantics::DeclTypeSpec::Category::Logical &&
696 !reductionLogicalSet.test(op.v)) {
697 context_.Say(GetContext().clauseSource,
698 "reduction operator not supported for logical type"_err_en_US);
699 }
700 // TODO: check composite type.
701 }
702 },
703 [&](const Fortran::parser::Name &name) {
704 // TODO: check common block
705 }},
706 object.u);
707 }
708 }
709
710 void AccStructureChecker::Enter(const parser::AccClause::Self &x) {
711 CheckAllowed(llvm::acc::Clause::ACCC_self);
712 const std::optional<parser::AccSelfClause> &accSelfClause = x.v;
713 if (GetContext().directive == llvm::acc::Directive::ACCD_update &&
714 ((accSelfClause &&
715 std::holds_alternative<std::optional<parser::ScalarLogicalExpr>>(
716 (*accSelfClause).u)) ||
717 !accSelfClause)) {
718 context_.Say(GetContext().clauseSource,
719 "SELF clause on the %s directive must have a var-list"_err_en_US,
720 ContextDirectiveAsFortran());
721 } else if (GetContext().directive != llvm::acc::Directive::ACCD_update &&
722 accSelfClause &&
723 std::holds_alternative<parser::AccObjectList>((*accSelfClause).u)) {
724 const auto &accObjectList =
725 std::get<parser::AccObjectList>((*accSelfClause).u);
726 if (accObjectList.v.size() != 1) {
727 context_.Say(GetContext().clauseSource,
728 "SELF clause on the %s directive only accepts optional scalar logical"
729 " expression"_err_en_US,
730 ContextDirectiveAsFortran());
731 }
732 }
733 }
734
735 void AccStructureChecker::Enter(const parser::AccClause::Collapse &x) {
736 CheckAllowed(llvm::acc::Clause::ACCC_collapse);
737 CheckAllowedOncePerGroup(
738 llvm::acc::Clause::ACCC_collapse, llvm::acc::Clause::ACCC_device_type);
739 const parser::AccCollapseArg &accCollapseArg = x.v;
740 const auto &collapseValue{
741 std::get<parser::ScalarIntConstantExpr>(accCollapseArg.t)};
742 RequiresConstantPositiveParameter(
743 llvm::acc::Clause::ACCC_collapse, collapseValue);
744 }
745
746 void AccStructureChecker::Enter(const parser::AccClause::Present &x) {
747 CheckAllowed(llvm::acc::Clause::ACCC_present);
748 CheckMultipleOccurrenceInDeclare(x.v, llvm::acc::Clause::ACCC_present);
749 }
750
751 void AccStructureChecker::Enter(const parser::AccClause::Copy &x) {
752 CheckAllowed(llvm::acc::Clause::ACCC_copy);
753 CheckMultipleOccurrenceInDeclare(x.v, llvm::acc::Clause::ACCC_copy);
754 }
755
756 void AccStructureChecker::Enter(const parser::AccClause::Deviceptr &x) {
757 CheckAllowed(llvm::acc::Clause::ACCC_deviceptr);
758 CheckMultipleOccurrenceInDeclare(x.v, llvm::acc::Clause::ACCC_deviceptr);
759 }
760
761 void AccStructureChecker::Enter(const parser::AccClause::DeviceResident &x) {
762 CheckAllowed(llvm::acc::Clause::ACCC_device_resident);
763 CheckMultipleOccurrenceInDeclare(
764 x.v, llvm::acc::Clause::ACCC_device_resident);
765 }
766
767 void AccStructureChecker::Enter(const parser::AccClause::Link &x) {
768 CheckAllowed(llvm::acc::Clause::ACCC_link);
769 CheckMultipleOccurrenceInDeclare(x.v, llvm::acc::Clause::ACCC_link);
770 }
771
772 void AccStructureChecker::Enter(const parser::AccClause::If &x) {
773 CheckAllowed(llvm::acc::Clause::ACCC_if);
774 if (const auto *expr{GetExpr(x.v)}) {
775 if (auto type{expr->GetType()}) {
776 if (type->category() == TypeCategory::Integer ||
777 type->category() == TypeCategory::Logical) {
778 return; // LOGICAL and INTEGER type supported for the if clause.
779 }
780 }
781 }
782 context_.Say(
783 GetContext().clauseSource, "Must have LOGICAL or INTEGER type"_err_en_US);
784 }
785
786 void AccStructureChecker::Enter(const parser::OpenACCEndConstruct &x) {
787 if (context_.languageFeatures().ShouldWarn(
788 common::UsageWarning::OpenAccUsage)) {
789 context_.Say(x.source, "Misplaced OpenACC end directive"_warn_en_US);
790 }
791 }
792
793 void AccStructureChecker::Enter(const parser::Module &) {
794 declareSymbols.clear();
795 }
796
797 void AccStructureChecker::Enter(const parser::FunctionSubprogram &x) {
798 declareSymbols.clear();
799 }
800
801 void AccStructureChecker::Enter(const parser::SubroutineSubprogram &) {
802 declareSymbols.clear();
803 }
804
805 void AccStructureChecker::Enter(const parser::SeparateModuleSubprogram &) {
806 declareSymbols.clear();
807 }
808
809 void AccStructureChecker::Enter(const parser::DoConstruct &) {
810 ++loopNestLevel;
811 }
812
813 void AccStructureChecker::Leave(const parser::DoConstruct &) {
814 --loopNestLevel;
815 }
816
817 llvm::StringRef AccStructureChecker::getDirectiveName(
818 llvm::acc::Directive directive) {
819 return llvm::acc::getOpenACCDirectiveName(directive);
820 }
821
822 llvm::StringRef AccStructureChecker::getClauseName(llvm::acc::Clause clause) {
823 return llvm::acc::getOpenACCClauseName(clause);
824 }
825
826 } // namespace Fortran::semantics