Skip to content

Commit 9f16791

Browse files
authored
[flang][OpenMP] Update GetOmpObjectList, move to parser utils (#154389)
`GetOmpObjectList` takes a clause, and returns the pointer to the contained OmpObjectList, or nullptr if the clause does not contain one. Some clauses with object list were not recognized: handle all clauses, and move the implementation to flang/Parser/openmp-utils.cpp.
1 parent 2be52f3 commit 9f16791

File tree

5 files changed

+69
-37
lines changed

5 files changed

+69
-37
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ template <typename T> OmpDirectiveName GetOmpDirectiveName(const T &x) {
158158
return detail::DirectiveNameScope::GetOmpDirectiveName(x);
159159
}
160160

161+
const OmpObjectList *GetOmpObjectList(const OmpClause &clause);
162+
161163
} // namespace Fortran::parser::omp
162164

163165
#endif // FORTRAN_PARSER_OPENMP_UTILS_H

flang/lib/Parser/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_flang_library(FortranParser
1212
message.cpp
1313
openacc-parsers.cpp
1414
openmp-parsers.cpp
15+
openmp-utils.cpp
1516
parse-tree.cpp
1617
parsing.cpp
1718
preprocessor.cpp

flang/lib/Parser/openmp-utils.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//===-- flang/Parser/openmp-utils.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+
// Common OpenMP utilities.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "flang/Parser/openmp-utils.h"
14+
15+
#include "flang/Common/template.h"
16+
#include "flang/Common/visit.h"
17+
18+
#include <tuple>
19+
#include <type_traits>
20+
#include <variant>
21+
22+
namespace Fortran::parser::omp {
23+
24+
const OmpObjectList *GetOmpObjectList(const OmpClause &clause) {
25+
// Clauses with OmpObjectList as its data member
26+
using MemberObjectListClauses = std::tuple<OmpClause::Copyin,
27+
OmpClause::Copyprivate, OmpClause::Exclusive, OmpClause::Firstprivate,
28+
OmpClause::HasDeviceAddr, OmpClause::Inclusive, OmpClause::IsDevicePtr,
29+
OmpClause::Link, OmpClause::Private, OmpClause::Shared,
30+
OmpClause::UseDeviceAddr, OmpClause::UseDevicePtr>;
31+
32+
// Clauses with OmpObjectList in the tuple
33+
using TupleObjectListClauses = std::tuple<OmpClause::AdjustArgs,
34+
OmpClause::Affinity, OmpClause::Aligned, OmpClause::Allocate,
35+
OmpClause::Enter, OmpClause::From, OmpClause::InReduction,
36+
OmpClause::Lastprivate, OmpClause::Linear, OmpClause::Map,
37+
OmpClause::Reduction, OmpClause::TaskReduction, OmpClause::To>;
38+
39+
// TODO:: Generate the tuples using TableGen.
40+
return common::visit(
41+
common::visitors{
42+
[&](const OmpClause::Depend &x) -> const OmpObjectList * {
43+
if (auto *taskDep{std::get_if<OmpDependClause::TaskDep>(&x.v.u)}) {
44+
return &std::get<OmpObjectList>(taskDep->t);
45+
} else {
46+
return nullptr;
47+
}
48+
},
49+
[&](const auto &x) -> const OmpObjectList * {
50+
using Ty = std::decay_t<decltype(x)>;
51+
if constexpr (common::HasMember<Ty, MemberObjectListClauses>) {
52+
return &x.v;
53+
} else if constexpr (common::HasMember<Ty,
54+
TupleObjectListClauses>) {
55+
return &std::get<OmpObjectList>(x.v.t);
56+
} else {
57+
return nullptr;
58+
}
59+
},
60+
},
61+
clause.u);
62+
}
63+
64+
} // namespace Fortran::parser::omp

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

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "flang/Parser/char-block.h"
2121
#include "flang/Parser/characters.h"
2222
#include "flang/Parser/message.h"
23+
#include "flang/Parser/openmp-utils.h"
2324
#include "flang/Parser/parse-tree-visitor.h"
2425
#include "flang/Parser/parse-tree.h"
2526
#include "flang/Parser/tools.h"
@@ -57,6 +58,7 @@
5758
namespace Fortran::semantics {
5859

5960
using namespace Fortran::semantics::omp;
61+
using namespace Fortran::parser::omp;
6062

6163
// Use when clause falls under 'struct OmpClause' in 'parse-tree.h'.
6264
#define CHECK_SIMPLE_CLAUSE(X, Y) \
@@ -4540,42 +4542,6 @@ const parser::Name *OmpStructureChecker::GetObjectName(
45404542
return NameHelper::Visit(object);
45414543
}
45424544

4543-
const parser::OmpObjectList *OmpStructureChecker::GetOmpObjectList(
4544-
const parser::OmpClause &clause) {
4545-
4546-
// Clauses with OmpObjectList as its data member
4547-
using MemberObjectListClauses =
4548-
std::tuple<parser::OmpClause::Copyprivate, parser::OmpClause::Copyin,
4549-
parser::OmpClause::Firstprivate, parser::OmpClause::Link,
4550-
parser::OmpClause::Private, parser::OmpClause::Shared,
4551-
parser::OmpClause::UseDevicePtr, parser::OmpClause::UseDeviceAddr>;
4552-
4553-
// Clauses with OmpObjectList in the tuple
4554-
using TupleObjectListClauses =
4555-
std::tuple<parser::OmpClause::Aligned, parser::OmpClause::Allocate,
4556-
parser::OmpClause::From, parser::OmpClause::Lastprivate,
4557-
parser::OmpClause::Map, parser::OmpClause::Reduction,
4558-
parser::OmpClause::To, parser::OmpClause::Enter>;
4559-
4560-
// TODO:: Generate the tuples using TableGen.
4561-
// Handle other constructs with OmpObjectList such as OpenMPThreadprivate.
4562-
return common::visit(
4563-
common::visitors{
4564-
[&](const auto &x) -> const parser::OmpObjectList * {
4565-
using Ty = std::decay_t<decltype(x)>;
4566-
if constexpr (common::HasMember<Ty, MemberObjectListClauses>) {
4567-
return &x.v;
4568-
} else if constexpr (common::HasMember<Ty,
4569-
TupleObjectListClauses>) {
4570-
return &(std::get<parser::OmpObjectList>(x.v.t));
4571-
} else {
4572-
return nullptr;
4573-
}
4574-
},
4575-
},
4576-
clause.u);
4577-
}
4578-
45794545
void OmpStructureChecker::Enter(
45804546
const parser::OmpClause::AtomicDefaultMemOrder &x) {
45814547
CheckAllowedRequiresClause(llvm::omp::Clause::OMPC_atomic_default_mem_order);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ class OmpStructureChecker
326326
const parser::OmpObjectList &ompObjectList);
327327
void CheckIfContiguous(const parser::OmpObject &object);
328328
const parser::Name *GetObjectName(const parser::OmpObject &object);
329-
const parser::OmpObjectList *GetOmpObjectList(const parser::OmpClause &);
330329
void CheckPredefinedAllocatorRestriction(const parser::CharBlock &source,
331330
const parser::OmpObjectList &ompObjectList);
332331
void CheckPredefinedAllocatorRestriction(

0 commit comments

Comments
 (0)