Skip to content

Commit f8eebe6

Browse files
kparzyszmahesh-attarde
authored andcommitted
[flang][OpenMP] Check contatining scoping unit in DECLARE_SIMD (llvm#161556)
Check if the name on DECLARE_SIMD is the name of the containing scoping unit. Fixes llvm#161516
1 parent 3d60bca commit f8eebe6

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

flang/include/flang/Semantics/openmp-utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ template <typename T, typename U = std::remove_const_t<T>> U AsRvalue(T &t) {
3737

3838
template <typename T> T &&AsRvalue(T &&t) { return std::move(t); }
3939

40+
const Scope &GetScopingUnit(const Scope &scope);
41+
4042
// There is no consistent way to get the source of an ActionStmt, but there
4143
// is "source" in Statement<T>. This structure keeps the ActionStmt with the
4244
// extracted source for further use.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,9 +1361,19 @@ void OmpStructureChecker::Enter(const parser::OpenMPDeclareSimdConstruct &x) {
13611361
return;
13621362
}
13631363

1364+
auto isValidSymbol{[](const Symbol *sym) {
1365+
if (IsProcedure(*sym) || IsFunction(*sym)) {
1366+
return true;
1367+
}
1368+
if (const Symbol *owner{GetScopingUnit(sym->owner()).symbol()}) {
1369+
return IsProcedure(*owner) || IsFunction(*owner);
1370+
}
1371+
return false;
1372+
}};
1373+
13641374
const parser::OmpArgument &arg{args.v.front()};
13651375
if (auto *sym{GetArgumentSymbol(arg)}) {
1366-
if (!IsProcedure(*sym) && !IsFunction(*sym)) {
1376+
if (!isValidSymbol(sym)) {
13671377
auto &msg{context_.Say(arg.source,
13681378
"The name '%s' should refer to a procedure"_err_en_US, sym->name())};
13691379
if (sym->test(Symbol::Flag::Implicit)) {

flang/lib/Semantics/openmp-utils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@
4141
namespace Fortran::semantics::omp {
4242
using namespace Fortran::parser::omp;
4343

44+
const Scope &GetScopingUnit(const Scope &scope) {
45+
const Scope *iter{&scope};
46+
for (; !iter->IsTopLevel(); iter = &iter->parent()) {
47+
switch (iter->kind()) {
48+
case Scope::Kind::BlockConstruct:
49+
case Scope::Kind::BlockData:
50+
case Scope::Kind::DerivedType:
51+
case Scope::Kind::MainProgram:
52+
case Scope::Kind::Module:
53+
case Scope::Kind::Subprogram:
54+
return *iter;
55+
default:
56+
break;
57+
}
58+
}
59+
return *iter;
60+
}
61+
4462
SourcedActionStmt GetActionStmt(const parser::ExecutionPartConstruct *x) {
4563
if (x == nullptr) {
4664
return SourcedActionStmt{};

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -379,24 +379,6 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
379379
explicit OmpAttributeVisitor(SemanticsContext &context)
380380
: DirectiveAttributeVisitor(context) {}
381381

382-
static const Scope &scopingUnit(const Scope &scope) {
383-
const Scope *iter{&scope};
384-
for (; !iter->IsTopLevel(); iter = &iter->parent()) {
385-
switch (iter->kind()) {
386-
case Scope::Kind::BlockConstruct:
387-
case Scope::Kind::BlockData:
388-
case Scope::Kind::DerivedType:
389-
case Scope::Kind::MainProgram:
390-
case Scope::Kind::Module:
391-
case Scope::Kind::Subprogram:
392-
return *iter;
393-
default:
394-
break;
395-
}
396-
}
397-
return *iter;
398-
}
399-
400382
template <typename A> void Walk(const A &x) { parser::Walk(x, *this); }
401383
template <typename A> bool Pre(const A &) { return true; }
402384
template <typename A> void Post(const A &) {}
@@ -3086,8 +3068,8 @@ void OmpAttributeVisitor::ResolveOmpDesignator(
30863068
checkScope = ompFlag == Symbol::Flag::OmpExecutableAllocateDirective;
30873069
}
30883070
if (checkScope) {
3089-
if (scopingUnit(GetContext().scope) !=
3090-
scopingUnit(symbol->GetUltimate().owner())) {
3071+
if (omp::GetScopingUnit(GetContext().scope) !=
3072+
omp::GetScopingUnit(symbol->GetUltimate().owner())) {
30913073
context_.Say(designator.source, // 2.15.3
30923074
"List items must be declared in the same scoping unit in which the %s directive appears"_err_en_US,
30933075
parser::ToUpperCaseLetters(

flang/test/Semantics/OpenMP/declare-simd.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ subroutine f00
1919
subroutine f01
2020
end
2121

22+
integer function f02
23+
!Ok, expect no diagnostics
24+
!$omp declare_simd(f02)
25+
end
26+
2227
end module

0 commit comments

Comments
 (0)