Skip to content

Commit 9aa3dca

Browse files
committed
[flang][openacc] Semantic checks for OpenACC 3.0 clauses validity
Summary: This patch adds semantic checking for the OpenACC 3.0 clauses validity. Reviewers: sscalpone, tskeith, klausler, ichoyjx, DavidTruby, jdoerfert Reviewed By: tskeith, klausler Subscribers: mgorny, llvm-commits Tags: #llvm, #flang Differential Revision: https://reviews.llvm.org/D83807
1 parent 75c0f0d commit 9aa3dca

File tree

10 files changed

+950
-24
lines changed

10 files changed

+950
-24
lines changed

flang/lib/Semantics/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
add_flang_library(FortranSemantics
33
assignment.cpp
44
attr.cpp
5+
canonicalize-acc.cpp
56
canonicalize-do.cpp
67
canonicalize-omp.cpp
8+
check-acc-structure.cpp
79
check-allocate.cpp
810
check-arithmeticif.cpp
911
check-call.cpp
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===-- lib/Semantics/canonicalize-acc.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+
9+
#include "canonicalize-acc.h"
10+
#include "flang/Parser/parse-tree-visitor.h"
11+
#include "flang/Semantics/tools.h"
12+
13+
// After Loop Canonicalization, rewrite OpenACC parse tree to make OpenACC
14+
// Constructs more structured which provide explicit scopes for later
15+
// structural checks and semantic analysis.
16+
// 1. move structured DoConstruct into
17+
// OpenACCLoopConstruct. Compilation will not proceed in case of errors
18+
// after this pass.
19+
namespace Fortran::semantics {
20+
21+
using namespace parser::literals;
22+
23+
class CanonicalizationOfAcc {
24+
public:
25+
template <typename T> bool Pre(T &) { return true; }
26+
template <typename T> void Post(T &) {}
27+
CanonicalizationOfAcc(parser::Messages &messages) : messages_{messages} {}
28+
29+
void Post(parser::Block &block) {
30+
for (auto it{block.begin()}; it != block.end(); ++it) {
31+
if (auto *accLoop{parser::Unwrap<parser::OpenACCLoopConstruct>(*it)}) {
32+
RewriteOpenACCLoopConstruct(*accLoop, block, it);
33+
}
34+
} // Block list
35+
}
36+
37+
private:
38+
void RewriteOpenACCLoopConstruct(parser::OpenACCLoopConstruct &x,
39+
parser::Block &block, parser::Block::iterator it) {
40+
// Check the sequence of DoConstruct in the same iteration
41+
//
42+
// Original:
43+
// ExecutableConstruct -> OpenACCConstruct -> OpenACCLoopConstruct
44+
// ACCBeginLoopDirective
45+
// ExecutableConstruct -> DoConstruct
46+
//
47+
// After rewriting:
48+
// ExecutableConstruct -> OpenACCConstruct -> OpenACCLoopConstruct
49+
// AccBeginLoopDirective
50+
// DoConstruct
51+
parser::Block::iterator nextIt;
52+
auto &beginDir{std::get<parser::AccBeginLoopDirective>(x.t)};
53+
auto &dir{std::get<parser::AccLoopDirective>(beginDir.t)};
54+
55+
nextIt = it;
56+
if (++nextIt != block.end()) {
57+
if (auto *doCons{parser::Unwrap<parser::DoConstruct>(*nextIt)}) {
58+
if (doCons->GetLoopControl()) {
59+
// move DoConstruct
60+
std::get<std::optional<parser::DoConstruct>>(x.t) =
61+
std::move(*doCons);
62+
nextIt = block.erase(nextIt);
63+
} else {
64+
messages_.Say(dir.source,
65+
"DO loop after the %s directive must have loop control"_err_en_US,
66+
parser::ToUpperCaseLetters(dir.source.ToString()));
67+
}
68+
return; // found do-loop
69+
}
70+
}
71+
messages_.Say(dir.source,
72+
"A DO loop must follow the %s directive"_err_en_US,
73+
parser::ToUpperCaseLetters(dir.source.ToString()));
74+
}
75+
76+
parser::Messages &messages_;
77+
};
78+
79+
bool CanonicalizeAcc(parser::Messages &messages, parser::Program &program) {
80+
CanonicalizationOfAcc acc{messages};
81+
Walk(program, acc);
82+
return !messages.AnyFatalError();
83+
}
84+
} // namespace Fortran::semantics
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- lib/Semantics/canonicalize-acc.h ------------------------*- C++ -*-===//
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+
9+
#ifndef FORTRAN_SEMANTICS_CANONICALIZE_ACC_H_
10+
#define FORTRAN_SEMANTICS_CANONICALIZE_ACC_H_
11+
12+
namespace Fortran::parser {
13+
struct Program;
14+
class Messages;
15+
} // namespace Fortran::parser
16+
17+
namespace Fortran::semantics {
18+
bool CanonicalizeAcc(parser::Messages &messages, parser::Program &program);
19+
}
20+
21+
#endif // FORTRAN_SEMANTICS_CANONICALIZE_ACC_H_

0 commit comments

Comments
 (0)