Skip to content

Commit 26fc3aa

Browse files
authored
[OpenMP] Missing implicit otherwise clause in metadirective. (#127113)
Compiling this: `int main() {` ` #pragma omp metadirective when(use r= {condition(0)}` `: parallel for)` `for (int i=0; i<10; i++)` ; }` is generating an error: `error: expected expression` The compiler is interpreting this as if it's compiling a `#pragma omp metadirective` with no `otherwise` clause. In the OMP5.2 specs chapter 7.4 it's mentioned that: `If no otherwise clause is specified the effect is as if one was specified without an associated directive variant.` This patch fixes the issue.
1 parent 89e7f4d commit 26fc3aa

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,15 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
28832883
/*ReadDirectiveWithinMetadirective=*/true);
28842884
break;
28852885
}
2886+
// If no match is found and no otherwise clause is present, skip
2887+
// OMP5.2 Chapter 7.4: If no otherwise clause is specified the effect is as
2888+
// if one was specified without an associated directive variant.
2889+
if (BestIdx == -1 && Idx == 1) {
2890+
assert(Tok.is(tok::annot_pragma_openmp_end) &&
2891+
"Expecting the end of the pragma here");
2892+
ConsumeAnnotationToken();
2893+
return StmtEmpty();
2894+
}
28862895
break;
28872896
}
28882897
case OMPD_threadprivate: {

clang/test/OpenMP/metadirective_ast_print.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ void foo(void) {
7777
: parallel) default(nothing)
7878
for (int i = 0; i < 16; i++)
7979
;
80+
81+
#pragma omp metadirective when(user = {condition(0)} \
82+
: parallel for) otherwise()
83+
for (int i=0; i<10; i++)
84+
;
85+
#pragma omp metadirective when(user = {condition(0)} \
86+
: parallel for)
87+
for (int i=0; i<10; i++)
88+
;
89+
#pragma omp metadirective when(user = {condition(0)} \
90+
: parallel for) when(implementation = {extension(match_none)} \
91+
: parallel) default(parallel for)
92+
for (int i=0; i<10; i++)
93+
;
94+
95+
#pragma omp metadirective when(user = {condition(1)} \
96+
: parallel for) otherwise()
97+
for (int i=0; i<10; i++)
98+
;
99+
#pragma omp metadirective when(user = {condition(1)} \
100+
: parallel for)
101+
for (int i=0; i<10; i++)
102+
;
103+
#pragma omp metadirective when(user = {condition(1)} \
104+
: parallel for) when(implementation = {extension(match_none)} \
105+
: parallel) default(parallel for)
106+
for (int i=0; i<10; i++)
107+
;
80108
}
81109

82110
// CHECK: void bar(void);
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-linux -emit-llvm %s -o - | FileCheck %s
2+
// expected-no-diagnostics
3+
4+
#ifndef HEADER
5+
#define HEADER
6+
7+
void func1() {
8+
#pragma omp metadirective when(user = {condition(0)} \
9+
: parallel for) otherwise()
10+
for (int i = 0; i < 100; i++)
11+
;
12+
13+
#pragma omp metadirective when(user = {condition(0)} \
14+
: parallel for)
15+
for (int i = 0; i < 100; i++)
16+
;
17+
18+
#pragma omp metadirective when(user = {condition(0)} \
19+
: parallel for) \
20+
when(implementation = {extension(match_none)} \
21+
: parallel) default(parallel for)
22+
23+
for (int i = 0; i < 100; i++)
24+
;
25+
26+
27+
}
28+
29+
// CHECK-LABEL: define dso_local void @_Z5func1v()
30+
// CHECK: entry
31+
// CHECK-NEXT: [[I:%.*]] = alloca i32, align 4
32+
// CHECK-NEXT: [[I1:%.*]] = alloca i32, align 4
33+
// CHECK-NEXT: store i32 0, ptr [[I]], align 4
34+
// CHECK-NEXT: br label %[[FOR_COND:.*]]
35+
// CHECK: [[FOR_COND]]:
36+
// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[I]], align 4
37+
// CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], 100
38+
// CHECK-NEXT: br i1 [[CMP]], label %[[FOR_BODY:.*]], label %[[FOR_END:.*]]
39+
// CHECK: [[FOR_BODY]]:
40+
// CHECK-NEXT: br label %[[FOR_INC:.*]]
41+
// CHECK: [[FOR_INC]]:
42+
// CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[I]], align 4
43+
// CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP1]], 1
44+
// CHECK-NEXT: store i32 [[INC]], ptr [[I]], align 4
45+
// CHECK-NEXT: br label %[[FOR_COND]], !llvm.loop [[LOOP3:![0-9]+]]
46+
// CHECK: [[FOR_END]]:
47+
// CHECK-NEXT: store i32 0, ptr [[I1]], align 4
48+
// CHECK-NEXT: br label %[[FOR_COND2:.*]]
49+
// CHECK: [[FOR_COND2]]:
50+
// CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[I1]], align 4
51+
// CHECK-NEXT: [[CMP3:%.*]] = icmp slt i32 [[TMP2]], 100
52+
// CHECK-NEXT: br i1 [[CMP3]], label %[[FOR_BODY4:.*]], label %[[FOR_END7:.*]]
53+
// CHECK: [[FOR_BODY4]]:
54+
// CHECK-NEXT: br label %[[FOR_INC5:.*]]
55+
// CHECK: [[FOR_INC5]]:
56+
// CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[I1]], align 4
57+
// CHECK-NEXT: [[INC6:%.*]] = add nsw i32 [[TMP3]], 1
58+
// CHECK-NEXT: store i32 [[INC6]], ptr [[I1]], align 4
59+
// CHECK-NEXT: br label %[[FOR_COND2]], !llvm.loop [[LOOP5:![0-9]+]]
60+
// CHECK: [[FOR_END7]]:
61+
// CHECK: ret void
62+
63+
void func2() {
64+
#pragma omp metadirective when(user = {condition(1)} \
65+
: parallel for) otherwise()
66+
for (int i = 0; i < 100; i++)
67+
;
68+
69+
#pragma omp metadirective when(user = {condition(1)} \
70+
: parallel for)
71+
for (int i = 0; i < 100; i++)
72+
;
73+
}
74+
75+
// CHECK-LABEL: define dso_local void @_Z5func2v()
76+
// CHECK: entry
77+
// CHECK-NEXT: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @[[GLOB2:[0-9]+]], i32 0, ptr @_Z5func2v.omp_outlined)
78+
// CHECK-NEXT: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @[[GLOB2]], i32 0, ptr @_Z5func2v.omp_outlined.1)
79+
// CHECK-NEXT: ret void
80+
81+
82+
void func3() {
83+
#pragma omp metadirective when(user = {condition(0)} \
84+
: parallel for) \
85+
when(implementation = {extension(match_none)} \
86+
: parallel) default(parallel for)
87+
88+
for (int i = 0; i < 100; i++)
89+
;
90+
91+
}
92+
93+
// CHECK-LABEL: define dso_local void @_Z5func3v()
94+
// CHECK: entry
95+
// CHECK-NEXT: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @1, i32 0, ptr @_Z5func3v.omp_outlined)
96+
// CHECK-NEXT: ret void
97+
// CHECK-NEXT: }
98+
99+
// CHECK-LABEL: define internal void @_Z5func3v.omp_outlined
100+
// CHECK-SAME: (ptr noalias noundef [[DOTGLOBAL_TID_:%.*]],
101+
// CHECK-SAME: ptr noalias noundef [[DOTBOUND_TID_:%.*]])
102+
// CHECK-NEXT: entry
103+
// CHECK-NEXT: [[GLOB_TID__ADDR:%.*]] = alloca ptr, align 8
104+
// CHECK-NEXT: [[BOUND_TID__ADDR:%.*]] = alloca ptr, align 8
105+
// CHECK-NEXT: [[I:%.*]] = alloca i32, align 4
106+
// CHECK-NEXT: store ptr [[DOTGLOBAL_TID_]], ptr [[GLOB_TID__ADDR]], align 8
107+
// CHECK-NEXT: store ptr [[DOTBOUND_TID_]], ptr [[BOUND_TID__ADDR]], align 8
108+
// CHECK-NEXT: store i32 0, ptr [[I]], align 4
109+
// CHECK-NEXT: br label %for.cond
110+
// CHECK:for.cond:
111+
// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[I]], align 4
112+
// CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], 100
113+
// CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY:%.*]], label [[FOR_END:%.*]]
114+
// CHECK:for.body:
115+
// CHECK-NEXT: br label [[FOR_INC:%.*]]
116+
// CHECK:for.inc:
117+
// CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[I]], align 4
118+
// CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP1]], 1
119+
// CHECK-NEXT: store i32 [[INC]], ptr [[I]], align 4
120+
// CHECK-NEXT: br label [[FOR_COND:%.*]]
121+
// CHECK:for.end:
122+
// CHECK-NEXT: ret void
123+
// CHECK-NEXT:}
124+
125+
#endif

llvm/include/llvm/Frontend/OpenMP/OMPContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ bool isVariantApplicableInContext(const VariantMatchInfo &VMI,
188188
bool DeviceSetOnly = false);
189189

190190
/// Return the index (into \p VMIs) of the variant with the highest score
191-
/// from the ones applicble in \p Ctx. See llvm::isVariantApplicableInContext.
191+
/// from the ones applicable in \p Ctx. See llvm::isVariantApplicableInContext.
192192
int getBestVariantMatchForContext(const SmallVectorImpl<VariantMatchInfo> &VMIs,
193193
const OMPContext &Ctx);
194194

0 commit comments

Comments
 (0)