Skip to content

Commit 35e743d

Browse files
ravurvi20urvi-rav
andauthored
default clause replaced by otherwise clause for metadirective in OpenMP 5.2 (llvm#128640)
This PR replaces the `default` clause with the `otherwise` clause for the `metadirective` in OpenMP. The `otherwise` clause serves as a fallback condition when no directive from the when clauses is selected. In the `when` clause, context selectors define traits evaluated to determine the directive to be applied. The previous merge was reverted due to a failing test case, which has now been resolved. --------- Co-authored-by: urvi-rav <[email protected]>
1 parent 7e71db8 commit 35e743d

File tree

7 files changed

+517
-155
lines changed

7 files changed

+517
-155
lines changed

clang/docs/OpenMPSupport.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ implementation.
207207
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
208208
| misc | library shutdown (omp_pause_resource[_all]) | :good:`done` | D55078 |
209209
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
210-
| misc | metadirectives | :part:`mostly done` | D91944 |
210+
| misc | metadirectives | :part:`mostly done` | D91944, https://github.com/llvm/llvm-project/pull/128640 |
211211
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
212212
| misc | conditional modifier for lastprivate clause | :good:`done` | |
213213
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,8 @@ def err_omp_expected_colon : Error<"missing ':' in %0">;
16681668
def err_omp_missing_comma : Error< "missing ',' after %0">;
16691669
def err_omp_expected_context_selector
16701670
: Error<"expected valid context selector in %0">;
1671+
def warn_omp_default_deprecated : Warning<"'default' clause for"
1672+
" 'metadirective' is deprecated; use 'otherwise' instead">, InGroup<Deprecated>;
16711673
def err_omp_requires_out_inout_depend_type : Error<
16721674
"reserved locator 'omp_all_memory' requires 'out' or 'inout' "
16731675
"dependency types">;

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,6 +2759,15 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
27592759
OpenMPClauseKind CKind = Tok.isAnnotation()
27602760
? OMPC_unknown
27612761
: getOpenMPClauseKind(PP.getSpelling(Tok));
2762+
// Check if the clause is unrecognized.
2763+
if (CKind == OMPC_unknown)
2764+
Diag(Tok, diag::err_omp_expected_clause) << "metadirective";
2765+
if (getLangOpts().OpenMP < 52 && CKind == OMPC_otherwise)
2766+
Diag(Tok, diag::err_omp_unexpected_clause)
2767+
<< getOpenMPClauseName(CKind) << "metadirective";
2768+
if (CKind == OMPC_default && getLangOpts().OpenMP >= 52)
2769+
Diag(Tok, diag::warn_omp_default_deprecated);
2770+
27622771
SourceLocation Loc = ConsumeToken();
27632772

27642773
// Parse '('.
@@ -2785,6 +2794,7 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
27852794
return Directive;
27862795
}
27872796
}
2797+
27882798
// Skip Directive for now. We will parse directive in the second iteration
27892799
int paren = 0;
27902800
while (Tok.isNot(tok::r_paren) || paren != 0) {

clang/test/OpenMP/metadirective_ast_print.c

Lines changed: 179 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,149 @@
1-
// RUN: %clang_cc1 -verify -fopenmp -triple x86_64-unknown-linux-gnu -x c -std=c99 -ast-print %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -verify -fopenmp -triple x86_64-unknown-linux-gnu -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=DEFAULT
22

3-
// RUN: %clang_cc1 -verify -fopenmp-simd -triple x86_64-unknown-linux-gnu -x c -std=c99 -ast-print %s -o - | FileCheck %s
3+
// RUN: %clang_cc1 -verify -fopenmp-simd -triple x86_64-unknown-linux-gnu -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=DEFAULT
44

5-
// RUN: %clang_cc1 -verify -fopenmp -triple amdgcn-amd-amdhsa -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=CHECK-AMDGCN
5+
// RUN: %clang_cc1 -verify -fopenmp -triple amdgcn-amd-amdhsa -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=DEFAULT-AMDGCN
66

7-
// RUN: %clang_cc1 -verify -fopenmp-simd -triple amdgcn-amd-amdhsa -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=CHECK-AMDGCN
7+
// RUN: %clang_cc1 -verify -fopenmp-simd -triple amdgcn-amd-amdhsa -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=DEFAULT-AMDGCN
8+
9+
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=52 -DOMP52 -triple x86_64-unknown-linux-gnu -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=OMP52
10+
11+
// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-version=52 -DOMP52 -triple x86_64-unknown-linux-gnu -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=OMP52
12+
13+
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=52 -DOMP52 -triple amdgcn-amd-amdhsa -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=OMP52-AMDGCN
14+
15+
// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-version=52 -DOMP52 -triple amdgcn-amd-amdhsa -x c -std=c99 -ast-print %s -o - | FileCheck %s --check-prefix=OMP52-AMDGCN
816
// expected-no-diagnostics
917

1018
#ifndef HEADER
1119
#define HEADER
1220

21+
#ifdef OMP52
1322
void bar(void);
1423

1524
#define N 10
16-
void foo(void) {
25+
void foo1(void) {
26+
#pragma omp metadirective when(device = {kind(cpu)} \
27+
: parallel) otherwise()
28+
bar();
29+
#pragma omp metadirective when(implementation = {vendor(score(0) \
30+
: llvm)}, \
31+
device = {kind(cpu)} \
32+
: parallel) otherwise(target teams)
33+
bar();
34+
#pragma omp metadirective when(device = {kind(gpu)} \
35+
: target teams) when(implementation = {vendor(llvm)} \
36+
: parallel) otherwise()
37+
bar();
38+
#pragma omp metadirective otherwise(target) when(implementation = {vendor(score(5) \
39+
: llvm)}, \
40+
device = {kind(cpu, host)} \
41+
: parallel)
42+
bar();
43+
#pragma omp metadirective when(user = {condition(N > 10)} \
44+
: target) when(user = {condition(N == 10)} \
45+
: parallel)
46+
bar();
47+
#pragma omp metadirective when(device = {kind(host)} \
48+
: parallel for)
49+
for (int i = 0; i < 100; i++)
50+
;
51+
#pragma omp metadirective when(implementation = {extension(match_all)} \
52+
: parallel) otherwise(parallel for)
53+
for (int i = 0; i < 100; i++)
54+
;
55+
#pragma omp metadirective when(implementation = {extension(match_any)} \
56+
: parallel) otherwise(parallel for)
57+
for (int i = 0; i < 100; i++)
58+
;
59+
#pragma omp metadirective when(implementation = {extension(match_none)} \
60+
: parallel) otherwise(parallel for)
61+
for (int i = 0; i < 100; i++)
62+
;
63+
64+
// Test metadirective with nested OpenMP directive.
65+
int array[16];
66+
#pragma omp metadirective when(user = {condition(1)} \
67+
: parallel for)
68+
for (int i = 0; i < 16; i++) {
69+
#pragma omp simd
70+
for (int j = 0; j < 16; j++)
71+
array[i] = i;
72+
}
73+
74+
#pragma omp metadirective when(device={arch("amdgcn")}: \
75+
teams distribute parallel for)\
76+
otherwise(parallel for)
77+
for (int i = 0; i < 100; i++)
78+
;
79+
80+
#pragma omp metadirective when(implementation = {extension(match_all)} \
81+
: nothing) otherwise(parallel for)
82+
for (int i = 0; i < 16; i++)
83+
;
84+
85+
#pragma omp metadirective when(implementation = {extension(match_any)} \
86+
: parallel) otherwise(nothing)
87+
for (int i = 0; i < 16; i++)
88+
;
89+
90+
91+
#pragma omp metadirective when(user = {condition(0)} \
92+
: parallel for)
93+
for (int i=0; i<10; i++)
94+
;
95+
#pragma omp metadirective when(user = {condition(0)} \
96+
: parallel for) when(implementation = {extension(match_none)} \
97+
: parallel) otherwise(parallel for)
98+
for (int i=0; i<10; i++)
99+
;
100+
101+
102+
#pragma omp metadirective when(user = {condition(1)} \
103+
: parallel for)
104+
for (int i=0; i<10; i++)
105+
;
106+
#pragma omp metadirective when(user = {condition(1)} \
107+
: parallel for) when(implementation = {extension(match_none)} \
108+
: parallel) otherwise(parallel for)
109+
for (int i=0; i<10; i++)
110+
;
111+
}
112+
113+
// OMP52: void bar(void);
114+
// OMP52: void foo1(void)
115+
// OMP52-NEXT: #pragma omp parallel
116+
// OMP52-NEXT: bar()
117+
// OMP52-NEXT: #pragma omp parallel
118+
// OMP52-NEXT: bar()
119+
// OMP52-NEXT: #pragma omp parallel
120+
// OMP52-NEXT: bar()
121+
// OMP52-NEXT: #pragma omp parallel
122+
// OMP52-NEXT: bar()
123+
// OMP52-NEXT: #pragma omp parallel
124+
// OMP52-NEXT: bar()
125+
// OMP52-NEXT: #pragma omp parallel for
126+
// OMP52-NEXT: for (int i = 0; i < 100; i++)
127+
// OMP52: #pragma omp parallel
128+
// OMP52-NEXT: for (int i = 0; i < 100; i++)
129+
// OMP52: #pragma omp parallel for
130+
// OMP52-NEXT: for (int i = 0; i < 100; i++)
131+
// OMP52: #pragma omp parallel
132+
// OMP52-NEXT: for (int i = 0; i < 100; i++)
133+
// OMP52: #pragma omp parallel for
134+
// OMP52-NEXT: for (int i = 0; i < 16; i++) {
135+
// OMP52-NEXT: #pragma omp simd
136+
// OMP52-NEXT: for (int j = 0; j < 16; j++)
137+
// OMP52-AMDGCN: #pragma omp teams distribute parallel for
138+
// OMP52-AMDGCN-NEXT: for (int i = 0; i < 100; i++)
139+
// OMP52: for (int i = 0; i < 16; i++)
140+
// OMP52: for (int i = 0; i < 16; i++)
141+
142+
#else
143+
void bar(void);
144+
145+
#define N 10
146+
void foo2(void) {
17147
#pragma omp metadirective when(device = {kind(cpu)} \
18148
: parallel) default()
19149
bar();
@@ -78,10 +208,7 @@ void foo(void) {
78208
for (int i = 0; i < 16; i++)
79209
;
80210

81-
#pragma omp metadirective when(user = {condition(0)} \
82-
: parallel for) otherwise()
83-
for (int i=0; i<10; i++)
84-
;
211+
85212
#pragma omp metadirective when(user = {condition(0)} \
86213
: parallel for)
87214
for (int i=0; i<10; i++)
@@ -92,10 +219,7 @@ void foo(void) {
92219
for (int i=0; i<10; i++)
93220
;
94221

95-
#pragma omp metadirective when(user = {condition(1)} \
96-
: parallel for) otherwise()
97-
for (int i=0; i<10; i++)
98-
;
222+
99223
#pragma omp metadirective when(user = {condition(1)} \
100224
: parallel for)
101225
for (int i=0; i<10; i++)
@@ -105,35 +229,49 @@ void foo(void) {
105229
: parallel) default(parallel for)
106230
for (int i=0; i<10; i++)
107231
;
232+
#if _OPENMP >= 202111
233+
#pragma omp metadirective when(user = {condition(0)} \
234+
: parallel for) otherwise()
235+
for (int i=0; i<10; i++)
236+
;
237+
238+
#pragma omp metadirective when(user = {condition(1)} \
239+
: parallel for) otherwise()
240+
for (int i=0; i<10; i++)
241+
;
242+
#endif
108243
}
109244

110-
// CHECK: void bar(void);
111-
// CHECK: void foo(void)
112-
// CHECK-NEXT: #pragma omp parallel
113-
// CHECK-NEXT: bar()
114-
// CHECK-NEXT: #pragma omp parallel
115-
// CHECK-NEXT: bar()
116-
// CHECK-NEXT: #pragma omp parallel
117-
// CHECK-NEXT: bar()
118-
// CHECK-NEXT: #pragma omp parallel
119-
// CHECK-NEXT: bar()
120-
// CHECK-NEXT: #pragma omp parallel
121-
// CHECK-NEXT: bar()
122-
// CHECK-NEXT: #pragma omp parallel for
123-
// CHECK-NEXT: for (int i = 0; i < 100; i++)
124-
// CHECK: #pragma omp parallel
125-
// CHECK-NEXT: for (int i = 0; i < 100; i++)
126-
// CHECK: #pragma omp parallel for
127-
// CHECK-NEXT: for (int i = 0; i < 100; i++)
128-
// CHECK: #pragma omp parallel
129-
// CHECK-NEXT: for (int i = 0; i < 100; i++)
130-
// CHECK: #pragma omp parallel for
131-
// CHECK-NEXT: for (int i = 0; i < 16; i++) {
132-
// CHECK-NEXT: #pragma omp simd
133-
// CHECK-NEXT: for (int j = 0; j < 16; j++)
134-
// CHECK-AMDGCN: #pragma omp teams distribute parallel for
135-
// CHECK-AMDGCN-NEXT: for (int i = 0; i < 100; i++)
136-
// CHECK: for (int i = 0; i < 16; i++)
137-
// CHECK: for (int i = 0; i < 16; i++)
245+
// DEFAULT: void bar(void);
246+
// DEFAULT: void foo2(void)
247+
// DEFAULT-NEXT: #pragma omp parallel
248+
// DEFAULT-NEXT: bar()
249+
// DEFAULT-NEXT: #pragma omp parallel
250+
// DEFAULT-NEXT: bar()
251+
// DEFAULT-NEXT: #pragma omp parallel
252+
// DEFAULT-NEXT: bar()
253+
// DEFAULT-NEXT: #pragma omp parallel
254+
// DEFAULT-NEXT: bar()
255+
// DEFAULT-NEXT: #pragma omp parallel
256+
// DEFAULT-NEXT: bar()
257+
// DEFAULT-NEXT: #pragma omp parallel for
258+
// DEFAULT-NEXT: for (int i = 0; i < 100; i++)
259+
// DEFAULT: #pragma omp parallel
260+
// DEFAULT-NEXT: for (int i = 0; i < 100; i++)
261+
// DEFAULT: #pragma omp parallel for
262+
// DEFAULT-NEXT: for (int i = 0; i < 100; i++)
263+
// DEFAULT: #pragma omp parallel
264+
// DEFAULT-NEXT: for (int i = 0; i < 100; i++)
265+
// DEFAULT: #pragma omp parallel for
266+
// DEFAULT-NEXT: for (int i = 0; i < 16; i++) {
267+
// DEFAULT-NEXT: #pragma omp simd
268+
// DEFAULT-NEXT: for (int j = 0; j < 16; j++)
269+
// DEFAULT-AMDGCN: #pragma omp teams distribute parallel for
270+
// DEFAULT-AMDGCN-NEXT: for (int i = 0; i < 100; i++)
271+
// DEFAULT: for (int i = 0; i < 16; i++)
272+
// DEFAULT: for (int i = 0; i < 16; i++)
273+
138274

139275
#endif
276+
#endif
277+

0 commit comments

Comments
 (0)