Skip to content

Commit

Permalink
[OPENMP50]Add basic support for inscan reduction modifier.
Browse files Browse the repository at this point in the history
Added basic support (parsing/sema checks) for the inscan modifier in the
reduction clauses.
  • Loading branch information
alexey-bataev committed Mar 27, 2020
1 parent 08776de commit 36ed0ce
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 57 deletions.
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10101,6 +10101,19 @@ def err_omp_depobj_single_clause_expected : Error<
"exactly one of 'depend', 'destroy', or 'update' clauses is expected">;
def err_omp_scan_single_clause_expected : Error<
"exactly one of 'inclusive' or 'exclusive' clauses is expected">;
def err_omp_inclusive_exclusive_not_reduction : Error<
"the list item must appear in 'reduction' clause with the 'inscan' modifier "
"of the parent directive">;
def err_omp_reduction_not_inclusive_exclusive : Error<
"the inscan reduction list item must appear as a list item in an 'inclusive' or"
" 'exclusive' clause on an inner 'omp scan' directive">;
def err_omp_wrong_inscan_reduction : Error<
"'inscan' modifier can be used only in 'omp for', 'omp simd', 'omp for simd',"
" 'omp parallel for', or 'omp parallel for simd' directive">;
def err_omp_inscan_reduction_expected : Error<
"expected 'reduction' clause with the 'inscan' modifier">;
def note_omp_previous_inscan_reduction : Note<
"'reduction' clause with 'inscan' modifier is used here">;
def err_omp_expected_predefined_allocator : Error<
"expected one of the predefined allocators for the variables with the static "
"storage: 'omp_default_mem_alloc', 'omp_large_cap_mem_alloc', "
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/OpenMPKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ OPENMP_DEPOBJ_CLAUSE(update)

// Modifiers for 'reduction' clause.
OPENMP_REDUCTION_MODIFIER(default)
OPENMP_REDUCTION_MODIFIER(inscan)

#undef OPENMP_REDUCTION_MODIFIER
#undef OPENMP_SCAN_CLAUSE
Expand Down
141 changes: 130 additions & 11 deletions clang/lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ class DSAStackTy {
struct DSAVarData {
OpenMPDirectiveKind DKind = OMPD_unknown;
OpenMPClauseKind CKind = OMPC_unknown;
unsigned Modifier = 0;
const Expr *RefExpr = nullptr;
DeclRefExpr *PrivateCopy = nullptr;
SourceLocation ImplicitDSALoc;
DSAVarData() = default;
DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
const Expr *RefExpr, DeclRefExpr *PrivateCopy,
SourceLocation ImplicitDSALoc)
: DKind(DKind), CKind(CKind), RefExpr(RefExpr),
SourceLocation ImplicitDSALoc, unsigned Modifier)
: DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
};
using OperatorOffsetTy =
Expand All @@ -80,6 +81,7 @@ class DSAStackTy {
private:
struct DSAInfo {
OpenMPClauseKind Attributes = OMPC_unknown;
unsigned Modifier = 0;
/// Pointer to a reference expression and a flag which shows that the
/// variable is marked as lastprivate(true) or not (false).
llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
Expand Down Expand Up @@ -164,6 +166,8 @@ class DSAStackTy {
/// List of globals marked as declare target link in this target region
/// (isOpenMPTargetExecutionDirective(Directive) == true).
llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
/// List of decls used in inclusive/exclusive clauses of the scan directive.
llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Scope *CurScope, SourceLocation Loc)
: Directive(DKind), DirectiveName(Name), CurScope(CurScope),
Expand Down Expand Up @@ -469,9 +473,20 @@ class DSAStackTy {
/// parent directive.
const ValueDecl *getParentLoopControlVariable(unsigned I) const;

/// Marks the specified decl \p D as used in scan directive.
void markDeclAsUsedInScanDirective(ValueDecl *D) {
if (SharingMapTy *Stack = getSecondOnStackOrNull())
Stack->UsedInScanDirective.insert(D);
}

/// Checks if the specified declaration was used in the inner scan directive.
bool isUsedInScanDirective(ValueDecl *D) const {
return getTopOfStack().UsedInScanDirective.count(D) > 0;
}

/// Adds explicit data sharing attribute to the specified declaration.
void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
DeclRefExpr *PrivateCopy = nullptr);
DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0);

/// Adds additional information for the reduction items with the reduction id
/// represented as an operator.
Expand Down Expand Up @@ -1079,6 +1094,7 @@ DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
DVar.PrivateCopy = Data.PrivateCopy;
DVar.CKind = Data.Attributes;
DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
DVar.Modifier = Data.Modifier;
return DVar;
}

Expand Down Expand Up @@ -1226,19 +1242,21 @@ const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
}

void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
DeclRefExpr *PrivateCopy) {
DeclRefExpr *PrivateCopy, unsigned Modifier) {
D = getCanonicalDecl(D);
if (A == OMPC_threadprivate) {
DSAInfo &Data = Threadprivates[D];
Data.Attributes = A;
Data.RefExpr.setPointer(E);
Data.PrivateCopy = nullptr;
Data.Modifier = Modifier;
} else {
DSAInfo &Data = getTopOfStack().SharingMap[D];
assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
(A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
(A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
(isLoopControlVariable(D).first && A == OMPC_private));
Data.Modifier = Modifier;
if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
Data.RefExpr.setInt(/*IntVal=*/true);
return;
Expand All @@ -1250,6 +1268,7 @@ void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Data.PrivateCopy = PrivateCopy;
if (PrivateCopy) {
DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
Data.Modifier = Modifier;
Data.Attributes = A;
Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
Data.PrivateCopy = nullptr;
Expand Down Expand Up @@ -1355,7 +1374,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
"set.");
TaskgroupDescriptor = I->TaskgroupReductionRef;
return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
Data.PrivateCopy, I->DefaultAttrLoc);
Data.PrivateCopy, I->DefaultAttrLoc, /*Modifier=*/0);
}
return DSAVarData();
}
Expand All @@ -1380,7 +1399,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
"set.");
TaskgroupDescriptor = I->TaskgroupReductionRef;
return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
Data.PrivateCopy, I->DefaultAttrLoc);
Data.PrivateCopy, I->DefaultAttrLoc, /*Modifier=*/0);
}
return DSAVarData();
}
Expand Down Expand Up @@ -1455,6 +1474,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
if (TI != Threadprivates.end()) {
DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
DVar.CKind = OMPC_threadprivate;
DVar.Modifier = TI->getSecond().Modifier;
return DVar;
}
if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Expand Down Expand Up @@ -1546,6 +1566,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
DVar.CKind = Data.Attributes;
DVar.ImplicitDSALoc = I->DefaultAttrLoc;
DVar.DKind = I->Directive;
DVar.Modifier = Data.Modifier;
return DVar;
}

Expand Down Expand Up @@ -1592,6 +1613,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
DVar.CKind = Data.Attributes;
DVar.ImplicitDSALoc = I->DefaultAttrLoc;
DVar.DKind = I->Directive;
DVar.Modifier = Data.Modifier;
}

return DVar;
Expand Down Expand Up @@ -2315,11 +2337,64 @@ void Sema::EndOpenMPClause() {
DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
}

static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
ArrayRef<OMPClause *> Clauses);
static std::pair<ValueDecl *, bool>
getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
SourceRange &ERange, bool AllowArraySection = false);

/// Check consistency of the reduction clauses.
static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
ArrayRef<OMPClause *> Clauses) {
bool InscanFound = false;
SourceLocation InscanLoc;
// OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
// A reduction clause without the inscan reduction-modifier may not appear on
// a construct on which a reduction clause with the inscan reduction-modifier
// appears.
for (OMPClause *C : Clauses) {
if (C->getClauseKind() != OMPC_reduction)
continue;
auto *RC = cast<OMPReductionClause>(C);
if (RC->getModifier() == OMPC_REDUCTION_inscan) {
InscanFound = true;
InscanLoc = RC->getModifierLoc();
break;
}
}
if (InscanFound) {
for (OMPClause *C : Clauses) {
if (C->getClauseKind() != OMPC_reduction)
continue;
auto *RC = cast<OMPReductionClause>(C);
if (RC->getModifier() != OMPC_REDUCTION_inscan) {
S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown
? RC->getBeginLoc()
: RC->getModifierLoc(),
diag::err_omp_inscan_reduction_expected);
S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction);
continue;
}
for (Expr *Ref : RC->varlists()) {
assert(Ref && "NULL expr in OpenMP nontemporal clause.");
SourceLocation ELoc;
SourceRange ERange;
Expr *SimpleRefExpr = Ref;
auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
/*AllowArraySection=*/true);
ValueDecl *D = Res.first;
if (!D)
continue;
if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) {
S.Diag(Ref->getExprLoc(),
diag::err_omp_reduction_not_inclusive_exclusive)
<< Ref->getSourceRange();
}
}
}
}
}

static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
ArrayRef<OMPClause *> Clauses);
static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
bool WithInit);

Expand Down Expand Up @@ -2396,6 +2471,7 @@ void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
// Check allocate clauses.
if (!CurContext->isDependentContext())
checkAllocateClauses(*this, DSAStack, D->clauses());
checkReductionClauses(*this, DSAStack, D->clauses());
}

DSAStack->pop();
Expand Down Expand Up @@ -14111,9 +14187,11 @@ struct ReductionData {
SmallVector<Decl *, 4> ExprCaptures;
/// List of postupdate expressions.
SmallVector<Expr *, 4> ExprPostUpdates;
/// Reduction modifier.
unsigned RedModifier = 0;
ReductionData() = delete;
/// Reserves required memory for the reduction data.
ReductionData(unsigned Size) {
ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) {
Vars.reserve(Size);
Privates.reserve(Size);
LHSs.reserve(Size);
Expand Down Expand Up @@ -14831,7 +14909,8 @@ static bool actOnOMPReductionKindClause(
}
// All reduction items are still marked as reduction (to do not increase
// code base size).
Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref,
RD.RedModifier);
if (CurrDir == OMPD_taskgroup) {
if (DeclareReductionRef.isUsable())
Stack->addTaskgroupReductionData(D, ReductionIdRange,
Expand All @@ -14858,8 +14937,22 @@ OMPClause *Sema::ActOnOpenMPReductionClause(
<< getOpenMPClauseName(OMPC_reduction);
return nullptr;
}
// OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
// A reduction clause with the inscan reduction-modifier may only appear on a
// worksharing-loop construct, a worksharing-loop SIMD construct, a simd
// construct, a parallel worksharing-loop construct or a parallel
// worksharing-loop SIMD construct.
if (Modifier == OMPC_REDUCTION_inscan &&
(DSAStack->getCurrentDirective() != OMPD_for &&
DSAStack->getCurrentDirective() != OMPD_for_simd &&
DSAStack->getCurrentDirective() != OMPD_simd &&
DSAStack->getCurrentDirective() != OMPD_parallel_for &&
DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction);
return nullptr;
}

ReductionData RD(VarList.size());
ReductionData RD(VarList.size(), Modifier);
if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
StartLoc, LParenLoc, ColonLoc, EndLoc,
ReductionIdScopeSpec, ReductionId,
Expand Down Expand Up @@ -18161,6 +18254,19 @@ OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
if (!D)
continue;

const DSAStackTy::DSAVarData DVar =
DSAStack->getTopDSA(D, /*FromParent=*/true);
// OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
// A list item that appears in the inclusive or exclusive clause must appear
// in a reduction clause with the inscan modifier on the enclosing
// worksharing-loop, worksharing-loop SIMD, or simd construct.
if (DVar.CKind != OMPC_reduction ||
DVar.Modifier != OMPC_REDUCTION_inscan)
Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
<< RefExpr->getSourceRange();

if (DSAStack->getParentDirective() != OMPD_unknown)
DSAStack->markDeclAsUsedInScanDirective(D);
Vars.push_back(RefExpr);
}

Expand Down Expand Up @@ -18189,6 +18295,19 @@ OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
if (!D)
continue;

const DSAStackTy::DSAVarData DVar =
DSAStack->getTopDSA(D, /*FromParent=*/true);
// OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
// A list item that appears in the inclusive or exclusive clause must appear
// in a reduction clause with the inscan modifier on the enclosing
// worksharing-loop, worksharing-loop SIMD, or simd construct.
if (DVar.CKind != OMPC_reduction ||
DVar.Modifier != OMPC_REDUCTION_inscan)
Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
<< RefExpr->getSourceRange();

if (DSAStack->getParentDirective() != OMPD_unknown)
DSAStack->markDeclAsUsedInScanDirective(D);
Vars.push_back(RefExpr);
}

Expand Down
2 changes: 1 addition & 1 deletion clang/test/OpenMP/nesting_of_regions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2892,7 +2892,7 @@ void foo() {
}
#pragma omp parallel for simd
for (int i = 0; i < 10; ++i) {
#pragma omp scan // omp45-error {{OpenMP constructs may not be nested inside a simd region}} omp50-error {{exactly one of 'inclusive' or 'exclusive' clauses is expected}}
#pragma omp scan // omp45-error {{OpenMP constructs may not be nested inside a simd region}} omp50-error {{exactly one of 'inclusive' or 'exclusive' clauses is expected}}
bar();
}
#pragma omp parallel for simd
Expand Down
36 changes: 30 additions & 6 deletions clang/test/OpenMP/parallel_for_reduction_messages.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// RUN: %clang_cc1 -verify -fopenmp -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -std=c++98 -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -std=c++11 -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=50 -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=50 -std=c++98 -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=50 -std=c++11 -ferror-limit 150 -o - %s -Wuninitialized

// RUN: %clang_cc1 -verify -fopenmp-simd -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp-simd -std=c++98 -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp-simd -std=c++11 -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-version=50 -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-version=50 -std=c++98 -ferror-limit 150 -o - %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-version=50 -std=c++11 -ferror-limit 150 -o - %s -Wuninitialized

extern int omp_default_mem_alloc;
void xxx(int argc) {
Expand Down Expand Up @@ -78,6 +78,14 @@ class S5 {
#pragma omp for reduction(+:a) // expected-error {{reduction variable must be shared}}
for (int i = 0; i < 10; ++i)
::foo();
#pragma omp parallel for reduction(inscan, +:a)
for (int i = 0; i < 10; ++i) {
#pragma omp scan inclusive(a)
}
#pragma omp parallel for reduction(inscan, +:a)
for (int i = 0; i < 10; ++i) {
#pragma omp scan exclusive(a)
}
}
};
class S6 { // expected-note 3 {{candidate function (the implicit copy assignment operator) not viable: no known conversion from 'int' to 'const S6' for 1st argument}}
Expand Down Expand Up @@ -333,5 +341,21 @@ int main(int argc, char **argv) {
for (int i = 0; i < 10; ++i)
m++;

#pragma omp parallel for reduction(inscan, + : m) reduction(*: fl) reduction(default, &&: j) // expected-error 2 {{expected 'reduction' clause with the 'inscan' modifier}} expected-note 2 {{'reduction' clause with 'inscan' modifier is used here}}
for (int i = 0; i < 10; ++i) {
#pragma omp scan exclusive(m)
m++;
}
#pragma omp parallel for reduction(inscan, + : m, fl, j) // expected-error 2 {{the inscan reduction list item must appear as a list item in an 'inclusive' or 'exclusive' clause on an inner 'omp scan' directive}}
for (int i = 0; i < 10; ++i) {
#pragma omp scan exclusive(m)
m++;
}
#pragma omp parallel for reduction(inscan, + : m, fl, j) // expected-error 2 {{the inscan reduction list item must appear as a list item in an 'inclusive' or 'exclusive' clause on an inner 'omp scan' directive}}
for (int i = 0; i < 10; ++i) {
#pragma omp scan inclusive(m)
m++;
}

return tmain(argc) + tmain(fl); // expected-note {{in instantiation of function template specialization 'tmain<int>' requested here}} expected-note {{in instantiation of function template specialization 'tmain<float>' requested here}}
}
Loading

0 comments on commit 36ed0ce

Please sign in to comment.