Skip to content

Commit 89f930a

Browse files
ErnestoSuzahiraam
andauthored
[clang][OpenMP] Fix/enforce order-concurrent-nestable rules (#135463)
OpenMP has restrictions on directives allowed to be strictly nested inside a construct with the order(concurrent) clause specified. - OpenMP 5.0, 5.1, and 5.2 allows: 'loop', 'parallel', 'simd', and combined directives starting with 'parallel'. - OpenMP 6.0 allows: the above directives plus 'atomic' and all loop-transformation directives. Furthermore, a region that corresponds to a construct with order(concurrent) specified may not contain calls to the OpenMP runtime API. This PR fixes the following issues in the current implementation: With -fopenmp-version=50: none of the nesting restrictions above were enforced With -fopenmp-version=60: 1. Clang did not reject OpenMP runtime APIs encountered in the region. 2. Clang erroneously rejected combined directives starting with parallel. --------- Co-authored-by: Zahira Ammarguellat <[email protected]>
1 parent 9ef9167 commit 89f930a

File tree

4 files changed

+61
-51
lines changed

4 files changed

+61
-51
lines changed

clang/include/clang/Basic/OpenMPKinds.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,11 @@ bool isOpenMPCapturingDirective(OpenMPDirectiveKind DKind);
411411
/// directive that can be nested within region corresponding to construct
412412
/// on which order clause was specified with concurrent as ordering argument.
413413
/// \param DKind Specified directive.
414+
/// \param LangOpts Used for getting the OpenMP version.
414415
/// \return true - if the above condition is met for this directive
415416
/// otherwise - false.
416-
bool isOpenMPOrderConcurrentNestableDirective(OpenMPDirectiveKind DKind);
417+
bool isOpenMPOrderConcurrentNestableDirective(OpenMPDirectiveKind DKind,
418+
const LangOptions &LangOpts);
417419
}
418420

419421
template <>

clang/lib/Basic/OpenMPKinds.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,20 @@ bool clang::isOpenMPCapturingDirective(OpenMPDirectiveKind DKind) {
768768
}
769769

770770
bool clang::isOpenMPOrderConcurrentNestableDirective(
771-
OpenMPDirectiveKind DKind) {
772-
return DKind == OMPD_atomic || DKind == OMPD_loop || DKind == OMPD_simd ||
773-
DKind == OMPD_parallel || isOpenMPLoopTransformationDirective(DKind);
771+
OpenMPDirectiveKind DKind, const LangOptions &LangOpts) {
772+
// Directives strictly nestable in a construct with order(concurrent) are:
773+
// OpenMP 5.x: loop, parallel, simd, combined directive starting with parallel
774+
// OpenMP 6.0: above plus atomic and all loop-transformation directives
775+
776+
if (DKind == OMPD_loop || DKind == OMPD_parallel || DKind == OMPD_simd ||
777+
isOpenMPCombinedParallelADirective(DKind))
778+
return true;
779+
780+
if (LangOpts.OpenMP >= 60)
781+
return DKind == OMPD_atomic ||
782+
isOpenMPLoopTransformationDirective(DKind);
783+
784+
return false;
774785
}
775786

776787
void clang::getOpenMPCaptureRegions(

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4789,26 +4789,12 @@ static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
47894789
getLeafOrCompositeConstructs(ParentRegion, LeafOrComposite);
47904790
OpenMPDirectiveKind EnclosingConstruct = ParentLOC.back();
47914791

4792-
if (Stack->isParentOrderConcurrent()) {
4793-
bool InvalidOrderNesting = false;
4794-
if ((SemaRef.LangOpts.OpenMP == 51 || SemaRef.LangOpts.OpenMP == 52) &&
4795-
CurrentRegion != OMPD_simd && CurrentRegion != OMPD_loop &&
4796-
CurrentRegion != OMPD_parallel &&
4797-
!isOpenMPCombinedParallelADirective(CurrentRegion)) {
4798-
InvalidOrderNesting = true;
4799-
} else if (SemaRef.LangOpts.OpenMP >= 60 &&
4800-
!isOpenMPOrderConcurrentNestableDirective(CurrentRegion)) {
4801-
// OpenMP 6.0 [12.3 order Clause, Restrictions]
4802-
// Only regions that correspond to order-concurrent-nestable constructs
4803-
// or order-concurrent-nestable routines may be strictly nested regions
4804-
// of regions that correspond to constructs on which the order clause is
4805-
// specified with concurrent as the ordering argument.
4806-
InvalidOrderNesting = true;
4807-
}
4808-
if (InvalidOrderNesting) {
4809-
SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_order)
4810-
<< getOpenMPDirectiveName(CurrentRegion);
4811-
}
4792+
if (SemaRef.LangOpts.OpenMP >= 50 && Stack->isParentOrderConcurrent() &&
4793+
!isOpenMPOrderConcurrentNestableDirective(CurrentRegion,
4794+
SemaRef.LangOpts)) {
4795+
SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_order)
4796+
<< getOpenMPDirectiveName(CurrentRegion);
4797+
return true;
48124798
}
48134799
if (isOpenMPSimdDirective(ParentRegion) &&
48144800
((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) ||
@@ -7132,7 +7118,7 @@ ExprResult SemaOpenMP::ActOnOpenMPCall(ExprResult Call, Scope *Scope,
71327118
if (!CalleeFnDecl)
71337119
return Call;
71347120

7135-
if (getLangOpts().OpenMP >= 51 && getLangOpts().OpenMP < 60 &&
7121+
if (getLangOpts().OpenMP >= 50 && getLangOpts().OpenMP <= 60 &&
71367122
CalleeFnDecl->getIdentifier() &&
71377123
CalleeFnDecl->getName().starts_with_insensitive("omp_")) {
71387124
// checking for any calls inside an Order region
@@ -16373,21 +16359,20 @@ OMPClause *SemaOpenMP::ActOnOpenMPOrderClause(
1637316359
<< getOpenMPClauseName(OMPC_order);
1637416360
return nullptr;
1637516361
}
16376-
if (getLangOpts().OpenMP >= 51) {
16377-
if (Modifier == OMPC_ORDER_MODIFIER_unknown && MLoc.isValid()) {
16378-
Diag(MLoc, diag::err_omp_unexpected_clause_value)
16379-
<< getListOfPossibleValues(OMPC_order,
16380-
/*First=*/OMPC_ORDER_MODIFIER_unknown + 1,
16381-
/*Last=*/OMPC_ORDER_MODIFIER_last)
16382-
<< getOpenMPClauseName(OMPC_order);
16383-
} else {
16384-
DSAStack->setRegionHasOrderConcurrent(/*HasOrderConcurrent=*/true);
16385-
if (DSAStack->getCurScope()) {
16386-
// mark the current scope with 'order' flag
16387-
unsigned existingFlags = DSAStack->getCurScope()->getFlags();
16388-
DSAStack->getCurScope()->setFlags(existingFlags |
16389-
Scope::OpenMPOrderClauseScope);
16390-
}
16362+
if (getLangOpts().OpenMP >= 51 && Modifier == OMPC_ORDER_MODIFIER_unknown &&
16363+
MLoc.isValid()) {
16364+
Diag(MLoc, diag::err_omp_unexpected_clause_value)
16365+
<< getListOfPossibleValues(OMPC_order,
16366+
/*First=*/OMPC_ORDER_MODIFIER_unknown + 1,
16367+
/*Last=*/OMPC_ORDER_MODIFIER_last)
16368+
<< getOpenMPClauseName(OMPC_order);
16369+
} else if (getLangOpts().OpenMP >= 50) {
16370+
DSAStack->setRegionHasOrderConcurrent(/*HasOrderConcurrent=*/true);
16371+
if (DSAStack->getCurScope()) {
16372+
// mark the current scope with 'order' flag
16373+
unsigned existingFlags = DSAStack->getCurScope()->getFlags();
16374+
DSAStack->getCurScope()->setFlags(existingFlags |
16375+
Scope::OpenMPOrderClauseScope);
1639116376
}
1639216377
}
1639316378
return new (getASTContext()) OMPOrderClause(

clang/test/OpenMP/for_order_messages.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,47 @@
88
// RUN: %clang_cc1 -fsyntax-only -fopenmp-simd -fopenmp-version=52 -triple x86_64-unknown-unknown -verify=expected,omp51 %s -Wuninitialized
99
// RUN: %clang_cc1 -fsyntax-only -fopenmp-simd -fopenmp-version=60 -triple x86_64-unknown-unknown -verify=expected,omp60 %s -Wuninitialized
1010

11+
// Constructs strictly nestable in a construct with order(concurrent) specified vary by OpenMP version:
12+
// OMP5.0,5.1,5.2: loop, parallel, simd, and combined constructs with parallel as the first component.
13+
// OMP6.0: in addition to the ones allowed in OMP5.x, also atomic and all loop-transformation constructs.
14+
1115
extern int omp_get_num_threads (void);
1216

1317
int main(int argc, char **argv) {
1418
int A = 0;
1519
#pragma omp parallel for order(concurrent)
1620
for (int i = 0; i < 10; ++i)
17-
omp_get_num_threads(); // omp51-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}}
21+
omp_get_num_threads(); // omp50-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}} omp51-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}} omp60-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}}
1822

1923
#pragma omp parallel for order(reproducible:concurrent) // omp50-error {{expected 'concurrent' in OpenMP clause 'order'}}
2024
for (int i = 0; i < 10; ++i)
21-
omp_get_num_threads(); // omp51-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}}
25+
omp_get_num_threads(); // omp51-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}} omp60-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}}
2226

2327
#pragma omp parallel for order(unconstrained:concurrent) // omp50-error {{expected 'concurrent' in OpenMP clause 'order'}}
2428
for (int i = 0; i < 10; ++i)
25-
omp_get_num_threads(); // omp51-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}}
29+
omp_get_num_threads(); // omp51-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}} omp60-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}}
2630

2731
#pragma omp parallel for order(concurrent)
2832
for (int i = 0; i < 10; ++i) {
2933
for (int j = 0; j < 10; ++j) {
30-
omp_get_num_threads(); // omp51-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}}
34+
omp_get_num_threads(); // omp50-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}} omp51-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}} omp60-error {{calls to OpenMP runtime API are not allowed within a region that corresponds to a construct with an order clause that specifies concurrent}}
3135
}
3236
}
3337

38+
// nested atomic: OK in OMP6.0 but not in OMP5.x
3439
#pragma omp parallel for order(concurrent)
3540
for (int i = 0; i < 10; ++i) {
36-
#pragma omp atomic //omp51-error {{construct 'atomic' not allowed in a region associated with a directive with 'order' clause}}
41+
#pragma omp atomic //omp50-error {{construct 'atomic' not allowed in a region associated with a directive with 'order' clause}} omp51-error {{construct 'atomic' not allowed in a region associated with a directive with 'order' clause}}
3742
A++;
3843
}
3944

45+
// nested loop-transformation construct: OK in OMP6.0 but not in OMP5.x
46+
#pragma omp parallel for order(concurrent)
47+
for (int i = 0; i < 10; ++i) {
48+
#pragma omp unroll //omp50-error {{construct 'unroll' not allowed in a region associated with a directive with 'order' clause}} omp51-error {{construct 'unroll' not allowed in a region associated with a directive with 'order' clause}}
49+
for (int j = 0; j < 10; ++j);
50+
}
51+
4052
#pragma omp parallel for order(reproducible: concurrent) // omp50-error {{expected 'concurrent' in OpenMP clause 'order'}}
4153
for (int i = 0; i < 10; ++i) {
4254
#pragma omp target //omp51-error {{construct 'target' not allowed in a region associated with a directive with 'order' clause}} omp60-error {{construct 'target' not allowed in a region associated with a directive with 'order' clause}}
@@ -51,47 +63,47 @@ int main(int argc, char **argv) {
5163

5264
#pragma omp loop bind(parallel) order(concurrent)
5365
for (int i = 0; i < 10; ++i) {
54-
#pragma omp parallel for //omp60-error {{construct 'parallel for' not allowed in a region associated with a directive with 'order' clause}}
66+
#pragma omp parallel for
5567
for (int j = 0; j < 10; ++j) {
5668
A += j;
5769
}
5870
}
5971

6072
#pragma omp distribute order(concurrent)
6173
for (int i = 0; i < 10; ++i) {
62-
#pragma omp parallel for simd //omp60-error {{construct 'parallel for simd' not allowed in a region associated with a directive with 'order' clause}}
74+
#pragma omp parallel for simd
6375
for (int j = 0; j < 10; ++j) {
6476
A += j;
6577
}
6678
}
6779

6880
#pragma omp for order(concurrent)
6981
for (int i = 0; i < 10; ++i) {
70-
#pragma omp parallel master //omp60-error {{construct 'parallel master' not allowed in a region associated with a directive with 'order' clause}}
82+
#pragma omp parallel master
7183
for (int j = 0; j < 10; ++j) {
7284
A += j;
7385
}
7486
}
7587

7688
#pragma omp for order(concurrent)
7789
for (int i = 0; i < 10; ++i) {
78-
#pragma omp parallel master taskloop //omp60-error {{construct 'parallel master taskloop' not allowed in a region associated with a directive with 'order' clause}}
90+
#pragma omp parallel master taskloop
7991
for (int j = 0; j < 10; ++j) {
8092
A += j;
8193
}
8294
}
8395

8496
#pragma omp for order(concurrent)
8597
for (int i = 0; i < 10; ++i) {
86-
#pragma omp parallel master taskloop simd //omp60-error {{construct 'parallel master taskloop simd' not allowed in a region associated with a directive with 'order' clause}}
98+
#pragma omp parallel master taskloop simd
8799
for (int j = 0; j < 10; ++j) {
88100
A += j;
89101
}
90102
}
91103

92104
#pragma omp for order(concurrent)
93105
for (int i = 0; i < 10; ++i) {
94-
#pragma omp parallel sections //omp60-error {{construct 'parallel sections' not allowed in a region associated with a directive with 'order' clause}}
106+
#pragma omp parallel sections
95107
{
96108
A++;
97109
}

0 commit comments

Comments
 (0)