From bc52967835fd22933d01bc6ea55ff79f37e1f211 Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Fri, 28 Sep 2018 19:33:14 +0000 Subject: [PATCH] [OPENMP]Fix PR39084: Check datasharing attributes of reduction variables only. According to OpenMP, the reduction item must be shared in parent region. But the item can be an array section or array subscript. In this case, we should not check for the datasharing of the base declaration. llvm-svn: 343356 --- clang/lib/Sema/SemaOpenMP.cpp | 118 +++++++++++++-------------- clang/test/OpenMP/for_ast_print.cpp | 4 +- clang/test/OpenMP/for_reduction_messages.cpp | 12 +-- 3 files changed, 66 insertions(+), 68 deletions(-) diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp index bcf2c5c..75351eb 100644 --- a/clang/lib/Sema/SemaOpenMP.cpp +++ b/clang/lib/Sema/SemaOpenMP.cpp @@ -10631,65 +10631,68 @@ static bool actOnOMPReductionKindClause( } continue; } + + OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] // If a list-item is a reference type then it must bind to the same object // for all threads of the team. - if (!ASE && !OASE && VD) { - VarDecl *VDDef = VD->getDefinition(); - if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { - DSARefChecker Check(Stack); - if (Check.Visit(VDDef->getInit())) { - S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) - << getOpenMPClauseName(ClauseKind) << ERange; - S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; - continue; + if (!ASE && !OASE) { + if (VD) { + VarDecl *VDDef = VD->getDefinition(); + if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { + DSARefChecker Check(Stack); + if (Check.Visit(VDDef->getInit())) { + S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) + << getOpenMPClauseName(ClauseKind) << ERange; + S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; + continue; + } } } - } - // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced - // in a Construct] - // Variables with the predetermined data-sharing attributes may not be - // listed in data-sharing attributes clauses, except for the cases - // listed below. For these exceptions only, listing a predetermined - // variable in a data-sharing attribute clause is allowed and overrides - // the variable's predetermined data-sharing attributes. - // OpenMP [2.14.3.6, Restrictions, p.3] - // Any number of reduction clauses can be specified on the directive, - // but a list item can appear only once in the reduction clauses for that - // directive. - DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); - if (DVar.CKind == OMPC_reduction) { - S.Diag(ELoc, diag::err_omp_once_referenced) - << getOpenMPClauseName(ClauseKind); - if (DVar.RefExpr) - S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); - continue; - } - if (DVar.CKind != OMPC_unknown) { - S.Diag(ELoc, diag::err_omp_wrong_dsa) - << getOpenMPClauseName(DVar.CKind) - << getOpenMPClauseName(OMPC_reduction); - reportOriginalDsa(S, Stack, D, DVar); - continue; - } - - // OpenMP [2.14.3.6, Restrictions, p.1] - // A list item that appears in a reduction clause of a worksharing - // construct must be shared in the parallel regions to which any of the - // worksharing regions arising from the worksharing construct bind. - OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); - if (isOpenMPWorksharingDirective(CurrDir) && - !isOpenMPParallelDirective(CurrDir) && - !isOpenMPTeamsDirective(CurrDir)) { - DVar = Stack->getImplicitDSA(D, true); - if (DVar.CKind != OMPC_shared) { - S.Diag(ELoc, diag::err_omp_required_access) - << getOpenMPClauseName(OMPC_reduction) - << getOpenMPClauseName(OMPC_shared); + // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced + // in a Construct] + // Variables with the predetermined data-sharing attributes may not be + // listed in data-sharing attributes clauses, except for the cases + // listed below. For these exceptions only, listing a predetermined + // variable in a data-sharing attribute clause is allowed and overrides + // the variable's predetermined data-sharing attributes. + // OpenMP [2.14.3.6, Restrictions, p.3] + // Any number of reduction clauses can be specified on the directive, + // but a list item can appear only once in the reduction clauses for that + // directive. + DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); + if (DVar.CKind == OMPC_reduction) { + S.Diag(ELoc, diag::err_omp_once_referenced) + << getOpenMPClauseName(ClauseKind); + if (DVar.RefExpr) + S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); + continue; + } + if (DVar.CKind != OMPC_unknown) { + S.Diag(ELoc, diag::err_omp_wrong_dsa) + << getOpenMPClauseName(DVar.CKind) + << getOpenMPClauseName(OMPC_reduction); reportOriginalDsa(S, Stack, D, DVar); continue; } + + // OpenMP [2.14.3.6, Restrictions, p.1] + // A list item that appears in a reduction clause of a worksharing + // construct must be shared in the parallel regions to which any of the + // worksharing regions arising from the worksharing construct bind. + if (isOpenMPWorksharingDirective(CurrDir) && + !isOpenMPParallelDirective(CurrDir) && + !isOpenMPTeamsDirective(CurrDir)) { + DVar = Stack->getImplicitDSA(D, true); + if (DVar.CKind != OMPC_shared) { + S.Diag(ELoc, diag::err_omp_required_access) + << getOpenMPClauseName(OMPC_reduction) + << getOpenMPClauseName(OMPC_shared); + reportOriginalDsa(S, Stack, D, DVar); + continue; + } + } } // Try to find 'declare reduction' corresponding construct before using @@ -11223,8 +11226,7 @@ OMPClause *Sema::ActOnOpenMPLinearClause( SourceLocation ELoc; SourceRange ERange; Expr *SimpleRefExpr = RefExpr; - auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, - /*AllowArraySection=*/false); + auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); if (Res.second) { // It will be analyzed later. Vars.push_back(RefExpr); @@ -11364,8 +11366,7 @@ static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, SourceLocation ELoc; SourceRange ERange; Expr *SimpleRefExpr = RefExpr; - auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange, - /*AllowArraySection=*/false); + auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); ValueDecl *D = Res.first; if (Res.second || !D) { Updates.push_back(nullptr); @@ -11446,8 +11447,7 @@ OMPClause *Sema::ActOnOpenMPAlignedClause( SourceLocation ELoc; SourceRange ERange; Expr *SimpleRefExpr = RefExpr; - auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, - /*AllowArraySection=*/false); + auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); if (Res.second) { // It will be analyzed later. Vars.push_back(RefExpr); @@ -11619,8 +11619,7 @@ OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef VarList, SourceLocation ELoc; SourceRange ERange; Expr *SimpleRefExpr = RefExpr; - auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, - /*AllowArraySection=*/false); + auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); if (Res.second) { // It will be analyzed later. Vars.push_back(RefExpr); @@ -11818,8 +11817,7 @@ Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, } SourceLocation ELoc; SourceRange ERange; - auto Res = getPrivateItem(*this, LHS, ELoc, ERange, - /*AllowArraySection=*/false); + auto Res = getPrivateItem(*this, LHS, ELoc, ERange); if (Res.second) { // It will be analyzed later. Vars.push_back(RefExpr); diff --git a/clang/test/OpenMP/for_ast_print.cpp b/clang/test/OpenMP/for_ast_print.cpp index 82318ff..760d44c 100644 --- a/clang/test/OpenMP/for_ast_print.cpp +++ b/clang/test/OpenMP/for_ast_print.cpp @@ -145,8 +145,8 @@ int main(int argc, char **argv) { int b = argc, c, d, e, f, g; static int a; // CHECK: static int a; -#pragma omp for schedule(guided, argc) - // CHECK-NEXT: #pragma omp for schedule(guided, argc) +#pragma omp for schedule(guided, argc) reduction(+:argv[0][:1]) + // CHECK-NEXT: #pragma omp for schedule(guided, argc) reduction(+: argv[0][:1]) for (int i = 0; i < 2; ++i) a = 2; // CHECK-NEXT: for (int i = 0; i < 2; ++i) diff --git a/clang/test/OpenMP/for_reduction_messages.cpp b/clang/test/OpenMP/for_reduction_messages.cpp index a4f15aa..520097d 100644 --- a/clang/test/OpenMP/for_reduction_messages.cpp +++ b/clang/test/OpenMP/for_reduction_messages.cpp @@ -214,12 +214,12 @@ T tmain(T argc) { #pragma omp for reduction(+ : fl) // expected-error 2 {{reduction variable must be shared}} for (int i = 0; i < 10; ++i) foo(); -#pragma omp parallel private(qa) // expected-note 2 {{defined as private}} -#pragma omp for reduction(+ : qa[1], get()[0]) // expected-error 2 {{reduction variable must be shared}} expected-error {{expected variable name as a base of the array subscript}} +#pragma omp parallel private(qa) +#pragma omp for reduction(+ : qa[1], get()[0]) // expected-error {{expected variable name as a base of the array subscript}} for (int i = 0; i < 10; ++i) foo(); #pragma omp parallel shared(qa) -#pragma omp for reduction(+ : qa[1], qa[0]) // expected-error 2 {{variable can appear only once in OpenMP 'reduction' clause}} expected-note 2 {{previously referenced here}} +#pragma omp for reduction(+ : qa[1], qa[0]) for (int i = 0; i < 10; ++i) foo(); #pragma omp parallel reduction(* : fl) // expected-note 2 {{defined as reduction}} @@ -379,12 +379,12 @@ int main(int argc, char **argv) { #pragma omp for reduction(+ : fl) // expected-error {{reduction variable must be shared}} for (int i = 0; i < 10; ++i) foo(); -#pragma omp parallel private(argv) // expected-note {{defined as private}} -#pragma omp for reduction(+ : argv[1], get()[0]) // expected-error {{reduction variable must be shared}} expected-error {{expected variable name as a base of the array subscript}} +#pragma omp parallel private(argv) +#pragma omp for reduction(+ : argv[1], get()[0]) // expected-error {{expected variable name as a base of the array subscript}} expected-error {{invalid operands to binary expression ('char *' and 'char *')}} for (int i = 0; i < 10; ++i) foo(); #pragma omp parallel shared(qa) -#pragma omp for reduction(+ : qa[1], qa[0]) // expected-error {{variable can appear only once in OpenMP 'reduction' clause}} expected-note {{previously referenced here}} +#pragma omp for reduction(+ : qa[1], qa[0]) for (int i = 0; i < 10; ++i) foo(); #pragma omp parallel reduction(* : fl) // expected-note {{defined as reduction}} -- 2.7.4