Skip to content

Commit a3c6a45

Browse files
Add OpenMP version based checks
1 parent 42cc3e7 commit a3c6a45

File tree

4 files changed

+83
-68
lines changed

4 files changed

+83
-68
lines changed

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

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,51 +2735,54 @@ void OmpStructureChecker::Leave(const parser::OmpClauseList &) {
27352735

27362736
if (GetContext().directive == llvm::omp::Directive::OMPD_task) {
27372737
if (auto *detachClause{FindClause(llvm::omp::Clause::OMPC_detach)}) {
2738-
// OpenMP 5.0: Task construct restrictions
2739-
CheckNotAllowedIfClause(
2740-
llvm::omp::Clause::OMPC_detach, {llvm::omp::Clause::OMPC_mergeable});
2741-
2742-
// OpenMP 5.2: Task construct restrictions
2743-
if (FindClause(llvm::omp::Clause::OMPC_final)) {
2744-
context_.Say(GetContext().clauseSource,
2745-
"If a DETACH clause appears on a directive, then the encountering task must not be a FINAL task"_err_en_US);
2746-
}
2738+
unsigned version{context_.langOptions().OpenMPVersion};
2739+
if (version == 50 || version == 51) {
2740+
// OpenMP 5.0: 2.10.1 Task construct restrictions
2741+
CheckNotAllowedIfClause(
2742+
llvm::omp::Clause::OMPC_detach, {llvm::omp::Clause::OMPC_mergeable});
2743+
} else if (version >= 52) {
2744+
// OpenMP 5.2: 12.5.2 Detach construct restrictions
2745+
if (FindClause(llvm::omp::Clause::OMPC_final)) {
2746+
context_.Say(GetContext().clauseSource,
2747+
"If a DETACH clause appears on a directive, then the encountering task must not be a FINAL task"_err_en_US);
2748+
}
27472749

2748-
const auto &detach{
2749-
std::get<parser::OmpClause::Detach>(detachClause->u)};
2750-
if (const auto *name{parser::Unwrap<parser::Name>(detach.v.v)}) {
2751-
if (name->symbol) {
2752-
Symbol *eventHandleSym{name->symbol->GetUltimate()};
2753-
auto checkVarAppearsInDataEnvClause = [&](const parser::OmpObjectList
2754-
&objs,
2755-
std::string clause) {
2756-
for (const auto &obj : objs.v) {
2757-
if (const parser::Name *objName{
2758-
parser::Unwrap<parser::Name>(obj)}) {
2759-
if (objName->symbol->GetUltimate() == eventHandleSym) {
2760-
context_.Say(GetContext().clauseSource,
2761-
"A variable: `%s` that appears in a DETACH clause cannot appear on %s clause on the same construct"_err_en_US,
2762-
objName->source, clause);
2750+
const auto &detach{
2751+
std::get<parser::OmpClause::Detach>(detachClause->u)};
2752+
if (const auto *name{parser::Unwrap<parser::Name>(detach.v.v)}) {
2753+
if (name->symbol) {
2754+
Symbol *eventHandleSym{name->symbol};
2755+
auto checkVarAppearsInDataEnvClause = [&](const parser::OmpObjectList
2756+
&objs,
2757+
std::string clause) {
2758+
for (const auto &obj : objs.v) {
2759+
if (const parser::Name *objName{
2760+
parser::Unwrap<parser::Name>(obj)}) {
2761+
if (&objName->symbol->GetUltimate() == eventHandleSym) {
2762+
context_.Say(GetContext().clauseSource,
2763+
"A variable: `%s` that appears in a DETACH clause cannot appear on %s clause on the same construct"_err_en_US,
2764+
objName->source, clause);
2765+
}
27632766
}
27642767
}
2768+
};
2769+
if (auto *dataEnvClause{
2770+
FindClause(llvm::omp::Clause::OMPC_private)}) {
2771+
const auto &pClause{
2772+
std::get<parser::OmpClause::Private>(dataEnvClause->u)};
2773+
checkVarAppearsInDataEnvClause(pClause.v, "PRIVATE");
2774+
} else if (auto *dataEnvClause{
2775+
FindClause(llvm::omp::Clause::OMPC_firstprivate)}) {
2776+
const auto &fpClause{
2777+
std::get<parser::OmpClause::Firstprivate>(dataEnvClause->u)};
2778+
checkVarAppearsInDataEnvClause(fpClause.v, "FIRSTPRIVATE");
2779+
} else if (auto *dataEnvClause{
2780+
FindClause(llvm::omp::Clause::OMPC_in_reduction)}) {
2781+
const auto &irClause{
2782+
std::get<parser::OmpClause::InReduction>(dataEnvClause->u)};
2783+
checkVarAppearsInDataEnvClause(
2784+
std::get<parser::OmpObjectList>(irClause.v.t), "IN_REDUCTION");
27652785
}
2766-
};
2767-
if (auto *dataEnvClause{
2768-
FindClause(llvm::omp::Clause::OMPC_private)}) {
2769-
const auto &pClause{
2770-
std::get<parser::OmpClause::Private>(dataEnvClause->u)};
2771-
checkVarAppearsInDataEnvClause(pClause.v, "PRIVATE");
2772-
} else if (auto *dataEnvClause{
2773-
FindClause(llvm::omp::Clause::OMPC_firstprivate)}) {
2774-
const auto &fpClause{
2775-
std::get<parser::OmpClause::Firstprivate>(dataEnvClause->u)};
2776-
checkVarAppearsInDataEnvClause(fpClause.v, "FIRSTPRIVATE");
2777-
} else if (auto *dataEnvClause{
2778-
FindClause(llvm::omp::Clause::OMPC_in_reduction)}) {
2779-
const auto &irClause{
2780-
std::get<parser::OmpClause::InReduction>(dataEnvClause->u)};
2781-
checkVarAppearsInDataEnvClause(
2782-
std::get<parser::OmpObjectList>(irClause.v.t), "IN_REDUCTION");
27832786
}
27842787
}
27852788
}
@@ -3804,23 +3807,25 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
38043807
}
38053808

38063809
void OmpStructureChecker::Enter(const parser::OmpClause::Detach &x) {
3807-
// OpenMP 5.0: Task construct restrictions
38083810
CheckAllowedClause(llvm::omp::Clause::OMPC_detach);
3811+
unsigned version{context_.langOptions().OpenMPVersion};
3812+
// OpenMP 5.2: 12.5.2 Detach clause restrictions
3813+
if (version >= 52) {
3814+
CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v.v, "DETACH");
3815+
}
38093816

3810-
// OpenMP 5.2: Detach clause restrictions
3811-
CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v.v, "DETACH");
38123817
if (const auto *name{parser::Unwrap<parser::Name>(x.v.v)}) {
38133818
if (name->symbol) {
3814-
if (IsPointer(*name->symbol)) {
3819+
if (version >= 52 && IsPointer(*name->symbol)) {
38153820
context_.Say(GetContext().clauseSource,
38163821
"The event-handle: `%s` must not have the POINTER attribute"_err_en_US,
38173822
name->ToString());
38183823
}
3819-
}
3820-
if (!name->symbol->GetType()->IsNumeric(TypeCategory::Integer)) {
3821-
context_.Say(GetContext().clauseSource,
3822-
"The event-handle: `%s` must be of type integer(kind=omp_event_handle_kind)"_err_en_US,
3823-
name->ToString());
3824+
if (!name->symbol->GetType()->IsNumeric(TypeCategory::Integer)) {
3825+
context_.Say(GetContext().clauseSource,
3826+
"The event-handle: `%s` must be of type integer(kind=omp_event_handle_kind)"_err_en_US,
3827+
name->ToString());
3828+
}
38243829
}
38253830
}
38263831
}

flang/test/Semantics/OpenMP/detach01.f90

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
11
! REQUIRES: openmp_runtime
2-
! RUN: %python %S/../test_errors.py %s %flang_fc1 %openmp_flags -fopenmp-version=50
2+
! RUN: %python %S/../test_errors.py %s %flang_fc1 %openmp_flags -fopenmp-version=52
33

4-
! OpenMP Version 5.2
5-
! Various checks for DETACH Clause (12.5.2)
4+
! OpenMP Version 5.2: 12.5.2
5+
! Various checks for DETACH Clause
66

7-
program test_detach
8-
use omp_lib
7+
program detach01
8+
use omp_lib, only: omp_event_handle_kind
99
implicit none
1010
real :: e, x
1111
integer(omp_event_handle_kind) :: event_01, event_02(2)
1212
integer(omp_event_handle_kind), pointer :: event_03
1313

14-
1514
type :: t
1615
integer(omp_event_handle_kind) :: event
1716
end type
18-
1917
type(t) :: t_01
2018

2119
!ERROR: The event-handle: `e` must be of type integer(kind=omp_event_handle_kind)
2220
!$omp task detach(e)
2321
x = x + 1
2422
!$omp end task
2523

26-
!ERROR: At most one DETACH clause can appear on the TASK directive
27-
!$omp task detach(event_01) detach(event_01)
28-
x = x + 1
29-
!$omp end task
30-
31-
!ERROR: Clause MERGEABLE is not allowed if clause DETACH appears on the TASK directive
32-
!$omp task detach(event_01) mergeable
33-
x = x + 1
34-
!$omp end task
35-
3624
!ERROR: If a DETACH clause appears on a directive, then the encountering task must not be a FINAL task
3725
!$omp task detach(event_01) final(.false.)
3826
x = x + 1
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
! REQUIRES: openmp_runtime
2+
! RUN: %python %S/../test_errors.py %s %flang_fc1 %openmp_flags -fopenmp-version=50
3+
! RUN: %python %S/../test_errors.py %s %flang_fc1 %openmp_flags -fopenmp-version=51
4+
5+
! OpenMP Version 5.0: 2.10.1
6+
! Various checks for DETACH Clause
7+
8+
program detach02
9+
use omp_lib, only: omp_event_handle_kind
10+
integer(omp_event_handle_kind) :: event_01, event_02
11+
12+
!TODO: Throw following error for the versions 5.0 and 5.1
13+
!ERR: At most one DETACH clause can appear on the TASK directive
14+
!!$omp task detach(event_01) detach(event_02)
15+
! x = x + 1
16+
!!$omp end task
17+
18+
!ERROR: Clause MERGEABLE is not allowed if clause DETACH appears on the TASK directive
19+
!$omp task detach(event_01) mergeable
20+
x = x + 1
21+
!$omp end task
22+
end program

llvm/include/llvm/Frontend/OpenMP/OMP.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@ def OMP_Task : Directive<"task"> {
10901090
VersionedClause<OMPC_Affinity, 50>,
10911091
VersionedClause<OMPC_Allocate>,
10921092
VersionedClause<OMPC_Depend>,
1093+
VersionedClause<OMPC_Detach, 50>,
10931094
VersionedClause<OMPC_FirstPrivate>,
10941095
VersionedClause<OMPC_InReduction>,
10951096
VersionedClause<OMPC_Mergeable>,
@@ -1099,7 +1100,6 @@ def OMP_Task : Directive<"task"> {
10991100
];
11001101
let allowedOnceClauses = [
11011102
VersionedClause<OMPC_Default>,
1102-
VersionedClause<OMPC_Detach, 50>,
11031103
VersionedClause<OMPC_Final>,
11041104
VersionedClause<OMPC_If>,
11051105
VersionedClause<OMPC_Priority>,

0 commit comments

Comments
 (0)