Skip to content

Commit f90abac

Browse files
committed
[OpenMP] Use compound operators for reduction combiner if available.
The OpenMP spec seems to require the compound operators be used for +, *, &, |, and ^ reduction. So use these if a class has those operators. If not try the simple operators as we did previously to limit the impact to existing code. Fixes: https://bugs.llvm.org/show_bug.cgi?id=48584 Differential Revision: https://reviews.llvm.org/D101941
1 parent 2075f2b commit f90abac

File tree

42 files changed

+2861
-668
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2861
-668
lines changed

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16315,6 +16315,21 @@ static bool checkOMPArraySectionConstantForReduction(
1631516315
return true;
1631616316
}
1631716317

16318+
static BinaryOperatorKind
16319+
getRelatedCompoundReductionOp(BinaryOperatorKind BOK) {
16320+
if (BOK == BO_Add)
16321+
return BO_AddAssign;
16322+
if (BOK == BO_Mul)
16323+
return BO_MulAssign;
16324+
if (BOK == BO_And)
16325+
return BO_AndAssign;
16326+
if (BOK == BO_Or)
16327+
return BO_OrAssign;
16328+
if (BOK == BO_Xor)
16329+
return BO_XorAssign;
16330+
return BOK;
16331+
}
16332+
1631816333
static bool actOnOMPReductionKindClause(
1631916334
Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
1632016335
ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
@@ -16840,25 +16855,35 @@ static bool actOnOMPReductionKindClause(
1684016855
CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc,
1684116856
S.CurFPFeatureOverrides());
1684216857
} else {
16843-
ReductionOp = S.BuildBinOp(
16844-
Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
16845-
if (ReductionOp.isUsable()) {
16846-
if (BOK != BO_LT && BOK != BO_GT) {
16847-
ReductionOp =
16848-
S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
16849-
BO_Assign, LHSDRE, ReductionOp.get());
16850-
} else {
16851-
auto *ConditionalOp = new (Context)
16852-
ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
16853-
Type, VK_LValue, OK_Ordinary);
16854-
ReductionOp =
16855-
S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
16856-
BO_Assign, LHSDRE, ConditionalOp);
16858+
BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK);
16859+
if (Type->isRecordType() && CombBOK != BOK) {
16860+
Sema::TentativeAnalysisScope Trap(S);
16861+
ReductionOp =
16862+
S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
16863+
CombBOK, LHSDRE, RHSDRE);
16864+
}
16865+
if (!ReductionOp.isUsable()) {
16866+
ReductionOp =
16867+
S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), BOK,
16868+
LHSDRE, RHSDRE);
16869+
if (ReductionOp.isUsable()) {
16870+
if (BOK != BO_LT && BOK != BO_GT) {
16871+
ReductionOp =
16872+
S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
16873+
BO_Assign, LHSDRE, ReductionOp.get());
16874+
} else {
16875+
auto *ConditionalOp = new (Context)
16876+
ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc,
16877+
RHSDRE, Type, VK_LValue, OK_Ordinary);
16878+
ReductionOp =
16879+
S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
16880+
BO_Assign, LHSDRE, ConditionalOp);
16881+
}
1685716882
}
16858-
if (ReductionOp.isUsable())
16859-
ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
16860-
/*DiscardedValue*/ false);
1686116883
}
16884+
if (ReductionOp.isUsable())
16885+
ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
16886+
/*DiscardedValue*/ false);
1686216887
if (!ReductionOp.isUsable())
1686316888
continue;
1686416889
}

clang/test/OpenMP/distribute_parallel_for_reduction_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ int main(int argc, char **argv) {
404404
foo();
405405
#pragma omp target
406406
#pragma omp teams
407-
#pragma omp distribute parallel for reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{invalid operands to binary expression ('S4' and 'S4')}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}} expected-warning {{Type 'S4' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'S5' is not trivially copyable and not guaranteed to be mapped correctly}}
407+
#pragma omp distribute parallel for reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}} expected-warning {{Type 'S4' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'S5' is not trivially copyable and not guaranteed to be mapped correctly}}
408408
for (int i = 0; i < 10; ++i)
409409
foo();
410410
#pragma omp target

clang/test/OpenMP/distribute_parallel_for_simd_reduction_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ int main(int argc, char **argv) {
403403
foo();
404404
#pragma omp target
405405
#pragma omp teams
406-
#pragma omp distribute parallel for simd reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{invalid operands to binary expression ('S4' and 'S4')}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
406+
#pragma omp distribute parallel for simd reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
407407
for (int i = 0; i < 10; ++i)
408408
foo();
409409
#pragma omp target

clang/test/OpenMP/distribute_simd_reduction_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ int main(int argc, char **argv) {
409409
foo();
410410
#pragma omp target
411411
#pragma omp teams
412-
#pragma omp distribute simd reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{invalid operands to binary expression ('S4' and 'S4')}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}} expected-warning {{Type 'S4' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'S5' is not trivially copyable and not guaranteed to be mapped correctly}}}
412+
#pragma omp distribute simd reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}} expected-warning {{Type 'S4' is not trivially copyable and not guaranteed to be mapped correctly}} expected-warning {{Type 'S5' is not trivially copyable and not guaranteed to be mapped correctly}}}
413413
for (int i = 0; i < 10; ++i)
414414
foo();
415415
#pragma omp target

clang/test/OpenMP/for_reduction_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ int main(int argc, char **argv) {
354354
for (int i = 0; i < 10; ++i)
355355
foo();
356356
#pragma omp parallel
357-
#pragma omp for reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S4' and 'S4')}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
357+
#pragma omp for reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
358358
for (int i = 0; i < 10; ++i)
359359
foo();
360360
#pragma omp parallel

clang/test/OpenMP/for_simd_reduction_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ int main(int argc, char **argv) {
345345
for (int i = 0; i < 10; ++i)
346346
foo();
347347
#pragma omp parallel
348-
#pragma omp for simd reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{invalid operands to binary expression ('S4' and 'S4')}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
348+
#pragma omp for simd reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
349349
for (int i = 0; i < 10; ++i)
350350
foo();
351351
#pragma omp parallel

clang/test/OpenMP/master_taskloop_in_reduction_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ int main(int argc, char **argv) {
344344
#pragma omp master taskloop in_reduction(&& : S2::S2sc) // expected-error {{const-qualified variable cannot be in_reduction}}
345345
for (int i = 0; i < 10; ++i)
346346
foo();
347-
#pragma omp master taskloop in_reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{nvalid operands to binary expression ('S4' and 'S4')}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
347+
#pragma omp master taskloop in_reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
348348
for (int i = 0; i < 10; ++i)
349349
foo();
350350
#pragma omp taskgroup task_reduction(+:k)

clang/test/OpenMP/master_taskloop_reduction_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ int main(int argc, char **argv) {
312312
#pragma omp master taskloop reduction(&& : S2::S2sc) // expected-error {{const-qualified variable cannot be reduction}}
313313
for (int i = 0; i < 10; ++i)
314314
foo();
315-
#pragma omp master taskloop reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{invalid operands to binary expression ('S4' and 'S4')}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
315+
#pragma omp master taskloop reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
316316
for (int i = 0; i < 10; ++i)
317317
foo();
318318
#pragma omp master taskloop reduction(+ : h, k, B::x) // expected-error 2 {{threadprivate or thread local variable cannot be reduction}}

clang/test/OpenMP/master_taskloop_simd_in_reduction_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ int main(int argc, char **argv) {
344344
#pragma omp master taskloop simd in_reduction(&& : S2::S2sc) // expected-error {{const-qualified variable cannot be in_reduction}}
345345
for (int i = 0; i < 10; ++i)
346346
foo();
347-
#pragma omp master taskloop simd in_reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{nvalid operands to binary expression ('S4' and 'S4')}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
347+
#pragma omp master taskloop simd in_reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
348348
for (int i = 0; i < 10; ++i)
349349
foo();
350350
#pragma omp taskgroup task_reduction(+:k)

clang/test/OpenMP/master_taskloop_simd_reduction_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ int main(int argc, char **argv) {
312312
#pragma omp master taskloop simd reduction(&& : S2::S2sc) // expected-error {{const-qualified variable cannot be reduction}}
313313
for (int i = 0; i < 10; ++i)
314314
foo();
315-
#pragma omp master taskloop simd reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{invalid operands to binary expression ('S4' and 'S4')}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
315+
#pragma omp master taskloop simd reduction(& : e, g) // expected-error {{calling a private constructor of class 'S4'}} expected-error {{calling a private constructor of class 'S5'}} expected-error {{invalid operands to binary expression ('S5' and 'S5')}}
316316
for (int i = 0; i < 10; ++i)
317317
foo();
318318
#pragma omp master taskloop simd reduction(+ : h, k, B::x) // expected-error 2 {{threadprivate or thread local variable cannot be reduction}}

0 commit comments

Comments
 (0)