Skip to content

Commit 2f755c5

Browse files
chandraghaleChandra Ghale
andauthored
[OpenMP] Conditional modifier on lastprivate clause is producing incorrect result in C mode (#156004)
Conditional modifier on lastprivate clause is producing incorrect result when compiled with clang( C compiler). IR is not emitting while compilation with C compiler. However it is working correctly with clang++ The OpenMP hook that emits the conditional modifier (checkAndEmitLastprivateConditional) is skipped in C because assignment is a prvalue and takes the scalar path. Original Codegen Support : [eddb8](a58da1a#diff-629e03f730f901cdf96b6b48fb0aed8ef156590aaff37857b8e5ad0694beddb8) ``` C = → prvalue → EmitAnyExpr(TEK_Scalar) → ScalarExprEmitter::VisitBinAssign (hook not reached) C++ = → lvalue → EmitBinaryOperatorLValue ``` ``` Failing Test Case : #include <stdio.h> #define N 10 int A[N]; void condlastprivate() { int x, y, z, k; x = y = z = k = 0; #pragma omp parallel for lastprivate(conditional: x,y, z) lastprivate(k) for( k=0; k<N; k++){ if ((k >2 ) && (k <6)) { x = A[k]; z = A[k]+111; } else { y = A[k]+222; } } printf("Expecting: x=5, y=231, z=116 k=10 Got: x=%d y=%d z=%d k=%d \n", x,y,z,k); } int main() { for( int i=0; i<N; i++) { A[i] = i; } condlastprivate(); return 0; } ``` ``` #>./clang -fopenmp cond_c.c #> ./a.out Expecting: x=5, y=231, z=116 k=10 **Got: x=-1376379760 y=231 z=631465600** k=10 ``` --------- Co-authored-by: Chandra Ghale <[email protected]>
1 parent e7429c2 commit 2f755c5

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5216,6 +5216,11 @@ Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
52165216
CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS);
52175217
}
52185218
}
5219+
// OpenMP: Handle lastprivate(condition:) in scalar assignment
5220+
if (CGF.getLangOpts().OpenMP) {
5221+
CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF,
5222+
E->getLHS());
5223+
}
52195224

52205225
// If the result is clearly ignored, return now.
52215226
if (Ignore)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %clang_cc1 -verify -x c -triple x86_64-unknown-linux-gnu -fopenmp -fopenmp-version=52 -emit-llvm -o - %s | FileCheck %s
2+
// expected-no-diagnostics
3+
4+
#define N 100
5+
int A[N];
6+
7+
void condlastprivate() {
8+
int x, y, z, k;
9+
x = y = z = k = 0;
10+
11+
#pragma omp parallel for lastprivate(conditional: x,y,z) lastprivate(k)
12+
for (k = 0; k < N; k++) {
13+
if ((k > 2) && (k < 6)) {
14+
x = A[k];
15+
z = A[k] + 111;
16+
} else {
17+
y = A[k] + 222;
18+
}
19+
}
20+
}
21+
22+
int main() {
23+
for (int i = 0; i < N; i++)
24+
A[i] = i;
25+
condlastprivate();
26+
return 0;
27+
}
28+
29+
// CHECK: @.pl_cond.x_[[ID:[0-9]+]].iv = common global i32 0, align 4
30+
// CHECK: @pl_cond.x_[[ID]] = common global i32 0, align 4
31+
// CHECK: @.gomp_critical_user_pl_cond.x_[[ID]].var = common global [8 x i32] zeroinitializer, align 8
32+
33+
// CHECK: @.pl_cond.z_[[ID]].iv = common global i32 0, align 4
34+
// CHECK: @pl_cond.z_[[ID]] = common global i32 0, align 4
35+
// CHECK: @.gomp_critical_user_pl_cond.z_[[ID]].var = common global [8 x i32] zeroinitializer, align 8
36+
37+
// CHECK: @.pl_cond.y_[[ID]].iv = common global i32 0, align 4
38+
// CHECK: @pl_cond.y_[[ID]] = common global i32 0, align 4
39+
// CHECK: @.gomp_critical_user_pl_cond.y_[[ID]].var = common global [8 x i32] zeroinitializer, align 8
40+
41+
// CHECK-LABEL: define internal void @condlastprivate.omp_outlined(
42+
// CHECK: call void @__kmpc_critical(ptr @2, {{.*}}, ptr @.gomp_critical_user_pl_cond.x_[[ID]].var)
43+
// CHECK: store i32 %{{[0-9]+}}, ptr @pl_cond.x_[[ID]], align 4
44+
// CHECK: call void @__kmpc_end_critical(ptr @2, {{.*}}, ptr @.gomp_critical_user_pl_cond.x_[[ID]].var)
45+
46+
// CHECK: call void @__kmpc_critical(ptr @2, {{.*}}, ptr @.gomp_critical_user_pl_cond.z_[[ID]].var)
47+
// CHECK: store i32 %{{[0-9]+}}, ptr @pl_cond.z_[[ID]], align 4
48+
// CHECK: call void @__kmpc_end_critical(ptr @2, {{.*}}, ptr @.gomp_critical_user_pl_cond.z_[[ID]].var)
49+
50+
// CHECK: call void @__kmpc_critical(ptr @2, {{.*}}, ptr @.gomp_critical_user_pl_cond.y_[[ID]].var)
51+
// CHECK: store i32 %{{[0-9]+}}, ptr @pl_cond.y_[[ID]], align 4
52+
// CHECK: call void @__kmpc_end_critical(ptr @2, {{.*}}, ptr @.gomp_critical_user_pl_cond.y_[[ID]].var)
53+

0 commit comments

Comments
 (0)