Skip to content

Commit db20592

Browse files
authored
[flang][openacc] Add check for acc cache directive (#65807)
OpenACC 3.3 - 2.10 The cache directive may appear at the top of (inside of) a loop. This patch adds a semantic check to ensure the cache directive is inside a loop.
1 parent 210e7b3 commit db20592

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ void AccStructureChecker::Enter(const parser::OpenACCCacheConstruct &x) {
335335
const auto &verbatim = std::get<parser::Verbatim>(x.t);
336336
PushContextAndClauseSets(verbatim.source, llvm::acc::Directive::ACCD_cache);
337337
SetContextDirectiveSource(verbatim.source);
338+
if (loopNestLevel == 0) {
339+
context_.Say(verbatim.source,
340+
"The CACHE directive must be inside a loop"_err_en_US);
341+
}
338342
}
339343
void AccStructureChecker::Leave(const parser::OpenACCCacheConstruct &x) {
340344
dirContext_.pop_back();
@@ -655,6 +659,14 @@ void AccStructureChecker::Enter(const parser::SeparateModuleSubprogram &) {
655659
declareSymbols.clear();
656660
}
657661

662+
void AccStructureChecker::Enter(const parser::DoConstruct &) {
663+
++loopNestLevel;
664+
}
665+
666+
void AccStructureChecker::Leave(const parser::DoConstruct &) {
667+
--loopNestLevel;
668+
}
669+
658670
llvm::StringRef AccStructureChecker::getDirectiveName(
659671
llvm::acc::Directive directive) {
660672
return llvm::acc::getOpenACCDirectiveName(directive);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class AccStructureChecker
7171
void Enter(const parser::SubroutineSubprogram &);
7272
void Enter(const parser::FunctionSubprogram &);
7373
void Enter(const parser::SeparateModuleSubprogram &);
74+
void Enter(const parser::DoConstruct &);
75+
void Leave(const parser::DoConstruct &);
7476

7577
#define GEN_FLANG_CLAUSE_CHECK_ENTER
7678
#include "llvm/Frontend/OpenACC/ACC.inc"
@@ -88,6 +90,7 @@ class AccStructureChecker
8890
llvm::StringRef getDirectiveName(llvm::acc::Directive directive) override;
8991

9092
llvm::SmallDenseSet<Symbol *> declareSymbols;
93+
unsigned loopNestLevel = 0;
9194
};
9295

9396
} // namespace Fortran::semantics

flang/test/Semantics/OpenACC/acc-cache-validity.f90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ program openacc_cache_validity
1919
type(atype), dimension(10) :: ta
2020
real(8), dimension(N) :: a
2121

22+
do i = 1, N
23+
2224
!$acc cache(a(i))
2325
!$acc cache(a(1:2,3:4))
2426
!$acc cache(a)
@@ -40,4 +42,9 @@ program openacc_cache_validity
4042
!ERROR: Only array element or subarray are allowed in CACHE directive
4143
!$acc cache(/i/)
4244

45+
end do
46+
47+
!ERROR: The CACHE directive must be inside a loop
48+
!$acc cache(a)
49+
4350
end program openacc_cache_validity

0 commit comments

Comments
 (0)