Skip to content

Commit 49764dc

Browse files
committed
[OPENMP50]Add basic support for inscan reduction modifier.
Added basic support (parsing/sema checks) for the inscan modifier in the reduction clauses.
1 parent cdd1cd7 commit 49764dc

File tree

8 files changed

+237
-66
lines changed

8 files changed

+237
-66
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10078,6 +10078,19 @@ def err_omp_depobj_single_clause_expected : Error<
1007810078
"exactly one of 'depend', 'destroy', or 'update' clauses is expected">;
1007910079
def err_omp_scan_single_clause_expected : Error<
1008010080
"exactly one of 'inclusive' or 'exclusive' clauses is expected">;
10081+
def err_omp_inclusive_exclusive_not_reduction : Error<
10082+
"the list item must appear in 'reduction' clause with the 'inscan' modifier "
10083+
"of the parent directive">;
10084+
def err_omp_reduction_not_inclusive_exclusive : Error<
10085+
"the inscan reduction list item must appear as a list item in an 'inclusive' or"
10086+
" 'exclusive' clause on an inner 'omp scan' directive">;
10087+
def err_omp_wrong_inscan_reduction : Error<
10088+
"'inscan' modifier can be used only in 'omp for', 'omp simd', 'omp for simd',"
10089+
" 'omp parallel for', or 'omp parallel for simd' directive">;
10090+
def err_omp_inscan_reduction_expected : Error<
10091+
"expected 'reduction' clause with the 'inscan' modifier">;
10092+
def note_omp_previous_inscan_reduction : Note<
10093+
"'reduction' clause with 'inscan' modifier is used here">;
1008110094
def err_omp_expected_predefined_allocator : Error<
1008210095
"expected one of the predefined allocators for the variables with the static "
1008310096
"storage: 'omp_default_mem_alloc', 'omp_large_cap_mem_alloc', "

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,7 @@ OPENMP_DEPOBJ_CLAUSE(update)
11121112

11131113
// Modifiers for 'reduction' clause.
11141114
OPENMP_REDUCTION_MODIFIER(default)
1115+
OPENMP_REDUCTION_MODIFIER(inscan)
11151116

11161117
#undef OPENMP_REDUCTION_MODIFIER
11171118
#undef OPENMP_SCAN_CLAUSE

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 147 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ class DSAStackTy {
6262
struct DSAVarData {
6363
OpenMPDirectiveKind DKind = OMPD_unknown;
6464
OpenMPClauseKind CKind = OMPC_unknown;
65+
unsigned Modifier = 0;
6566
const Expr *RefExpr = nullptr;
6667
DeclRefExpr *PrivateCopy = nullptr;
6768
SourceLocation ImplicitDSALoc;
6869
DSAVarData() = default;
6970
DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
7071
const Expr *RefExpr, DeclRefExpr *PrivateCopy,
71-
SourceLocation ImplicitDSALoc)
72-
: DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72+
SourceLocation ImplicitDSALoc, unsigned Modifier)
73+
: DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
7374
PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
7475
};
7576
using OperatorOffsetTy =
@@ -80,6 +81,7 @@ class DSAStackTy {
8081
private:
8182
struct DSAInfo {
8283
OpenMPClauseKind Attributes = OMPC_unknown;
84+
unsigned Modifier = 0;
8385
/// Pointer to a reference expression and a flag which shows that the
8486
/// variable is marked as lastprivate(true) or not (false).
8587
llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
@@ -164,6 +166,8 @@ class DSAStackTy {
164166
/// List of globals marked as declare target link in this target region
165167
/// (isOpenMPTargetExecutionDirective(Directive) == true).
166168
llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
169+
/// List of decls used in inclusive/exclusive clauses of the scan directive.
170+
llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
167171
SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
168172
Scope *CurScope, SourceLocation Loc)
169173
: Directive(DKind), DirectiveName(Name), CurScope(CurScope),
@@ -469,9 +473,22 @@ class DSAStackTy {
469473
/// parent directive.
470474
const ValueDecl *getParentLoopControlVariable(unsigned I) const;
471475

476+
/// Marks the specified decl \p D as used in scan directive.
477+
void markDeclAsUsedInScanDirective(ValueDecl *D) {
478+
if (SharingMapTy *Stack = getSecondOnStackOrNull())
479+
Stack->UsedInScanDirective.insert(D);
480+
}
481+
482+
/// Checks if the specified declaration was used in the inner scan directive.
483+
bool isUsedInScanDirective(ValueDecl *D) const {
484+
if (const SharingMapTy *Stack = getTopOfStackOrNull())
485+
return Stack->UsedInScanDirective.count(D) > 0;
486+
return false;
487+
}
488+
472489
/// Adds explicit data sharing attribute to the specified declaration.
473490
void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
474-
DeclRefExpr *PrivateCopy = nullptr);
491+
DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0);
475492

476493
/// Adds additional information for the reduction items with the reduction id
477494
/// represented as an operator.
@@ -1079,6 +1096,7 @@ DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
10791096
DVar.PrivateCopy = Data.PrivateCopy;
10801097
DVar.CKind = Data.Attributes;
10811098
DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1099+
DVar.Modifier = Data.Modifier;
10821100
return DVar;
10831101
}
10841102

@@ -1226,19 +1244,21 @@ const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
12261244
}
12271245

12281246
void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1229-
DeclRefExpr *PrivateCopy) {
1247+
DeclRefExpr *PrivateCopy, unsigned Modifier) {
12301248
D = getCanonicalDecl(D);
12311249
if (A == OMPC_threadprivate) {
12321250
DSAInfo &Data = Threadprivates[D];
12331251
Data.Attributes = A;
12341252
Data.RefExpr.setPointer(E);
12351253
Data.PrivateCopy = nullptr;
1254+
Data.Modifier = Modifier;
12361255
} else {
12371256
DSAInfo &Data = getTopOfStack().SharingMap[D];
12381257
assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
12391258
(A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
12401259
(A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
12411260
(isLoopControlVariable(D).first && A == OMPC_private));
1261+
Data.Modifier = Modifier;
12421262
if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
12431263
Data.RefExpr.setInt(/*IntVal=*/true);
12441264
return;
@@ -1250,6 +1270,7 @@ void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
12501270
Data.PrivateCopy = PrivateCopy;
12511271
if (PrivateCopy) {
12521272
DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1273+
Data.Modifier = Modifier;
12531274
Data.Attributes = A;
12541275
Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
12551276
Data.PrivateCopy = nullptr;
@@ -1355,7 +1376,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
13551376
"set.");
13561377
TaskgroupDescriptor = I->TaskgroupReductionRef;
13571378
return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1358-
Data.PrivateCopy, I->DefaultAttrLoc);
1379+
Data.PrivateCopy, I->DefaultAttrLoc, /*Modifier=*/0);
13591380
}
13601381
return DSAVarData();
13611382
}
@@ -1380,7 +1401,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
13801401
"set.");
13811402
TaskgroupDescriptor = I->TaskgroupReductionRef;
13821403
return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1383-
Data.PrivateCopy, I->DefaultAttrLoc);
1404+
Data.PrivateCopy, I->DefaultAttrLoc, /*Modifier=*/0);
13841405
}
13851406
return DSAVarData();
13861407
}
@@ -1455,6 +1476,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
14551476
if (TI != Threadprivates.end()) {
14561477
DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
14571478
DVar.CKind = OMPC_threadprivate;
1479+
DVar.Modifier = TI->getSecond().Modifier;
14581480
return DVar;
14591481
}
14601482
if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
@@ -1538,15 +1560,18 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
15381560
const_iterator EndI = end();
15391561
if (FromParent && I != EndI)
15401562
++I;
1541-
auto It = I->SharingMap.find(D);
1542-
if (It != I->SharingMap.end()) {
1543-
const DSAInfo &Data = It->getSecond();
1544-
DVar.RefExpr = Data.RefExpr.getPointer();
1545-
DVar.PrivateCopy = Data.PrivateCopy;
1546-
DVar.CKind = Data.Attributes;
1547-
DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1548-
DVar.DKind = I->Directive;
1549-
return DVar;
1563+
if (I != EndI) {
1564+
auto It = I->SharingMap.find(D);
1565+
if (It != I->SharingMap.end()) {
1566+
const DSAInfo &Data = It->getSecond();
1567+
DVar.RefExpr = Data.RefExpr.getPointer();
1568+
DVar.PrivateCopy = Data.PrivateCopy;
1569+
DVar.CKind = Data.Attributes;
1570+
DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1571+
DVar.DKind = I->Directive;
1572+
DVar.Modifier = Data.Modifier;
1573+
return DVar;
1574+
}
15501575
}
15511576

15521577
DVar.CKind = OMPC_shared;
@@ -1584,6 +1609,8 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
15841609
const_iterator EndI = end();
15851610
if (FromParent && I != EndI)
15861611
++I;
1612+
if (I == EndI)
1613+
return DVar;
15871614
auto It = I->SharingMap.find(D);
15881615
if (It != I->SharingMap.end()) {
15891616
const DSAInfo &Data = It->getSecond();
@@ -1592,6 +1619,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
15921619
DVar.CKind = Data.Attributes;
15931620
DVar.ImplicitDSALoc = I->DefaultAttrLoc;
15941621
DVar.DKind = I->Directive;
1622+
DVar.Modifier = Data.Modifier;
15951623
}
15961624

15971625
return DVar;
@@ -2315,11 +2343,64 @@ void Sema::EndOpenMPClause() {
23152343
DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
23162344
}
23172345

2318-
static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2319-
ArrayRef<OMPClause *> Clauses);
23202346
static std::pair<ValueDecl *, bool>
23212347
getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
23222348
SourceRange &ERange, bool AllowArraySection = false);
2349+
2350+
/// Check consistency of the reduction clauses.
2351+
static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2352+
ArrayRef<OMPClause *> Clauses) {
2353+
bool InscanFound = false;
2354+
SourceLocation InscanLoc;
2355+
// OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2356+
// A reduction clause without the inscan reduction-modifier may not appear on
2357+
// a construct on which a reduction clause with the inscan reduction-modifier
2358+
// appears.
2359+
for (OMPClause *C : Clauses) {
2360+
if (C->getClauseKind() != OMPC_reduction)
2361+
continue;
2362+
auto *RC = cast<OMPReductionClause>(C);
2363+
if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2364+
InscanFound = true;
2365+
InscanLoc = RC->getModifierLoc();
2366+
break;
2367+
}
2368+
}
2369+
if (InscanFound) {
2370+
for (OMPClause *C : Clauses) {
2371+
if (C->getClauseKind() != OMPC_reduction)
2372+
continue;
2373+
auto *RC = cast<OMPReductionClause>(C);
2374+
if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2375+
S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown
2376+
? RC->getBeginLoc()
2377+
: RC->getModifierLoc(),
2378+
diag::err_omp_inscan_reduction_expected);
2379+
S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction);
2380+
continue;
2381+
}
2382+
for (Expr *Ref : RC->varlists()) {
2383+
assert(Ref && "NULL expr in OpenMP nontemporal clause.");
2384+
SourceLocation ELoc;
2385+
SourceRange ERange;
2386+
Expr *SimpleRefExpr = Ref;
2387+
auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
2388+
/*AllowArraySection=*/true);
2389+
ValueDecl *D = Res.first;
2390+
if (!D)
2391+
continue;
2392+
if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) {
2393+
S.Diag(Ref->getExprLoc(),
2394+
diag::err_omp_reduction_not_inclusive_exclusive)
2395+
<< Ref->getSourceRange();
2396+
}
2397+
}
2398+
}
2399+
}
2400+
}
2401+
2402+
static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2403+
ArrayRef<OMPClause *> Clauses);
23232404
static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
23242405
bool WithInit);
23252406

@@ -2396,6 +2477,7 @@ void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
23962477
// Check allocate clauses.
23972478
if (!CurContext->isDependentContext())
23982479
checkAllocateClauses(*this, DSAStack, D->clauses());
2480+
checkReductionClauses(*this, DSAStack, D->clauses());
23992481
}
24002482

24012483
DSAStack->pop();
@@ -14111,9 +14193,11 @@ struct ReductionData {
1411114193
SmallVector<Decl *, 4> ExprCaptures;
1411214194
/// List of postupdate expressions.
1411314195
SmallVector<Expr *, 4> ExprPostUpdates;
14196+
/// Reduction modifier.
14197+
unsigned RedModifier = 0;
1411414198
ReductionData() = delete;
1411514199
/// Reserves required memory for the reduction data.
14116-
ReductionData(unsigned Size) {
14200+
ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) {
1411714201
Vars.reserve(Size);
1411814202
Privates.reserve(Size);
1411914203
LHSs.reserve(Size);
@@ -14831,7 +14915,8 @@ static bool actOnOMPReductionKindClause(
1483114915
}
1483214916
// All reduction items are still marked as reduction (to do not increase
1483314917
// code base size).
14834-
Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
14918+
Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref,
14919+
RD.RedModifier);
1483514920
if (CurrDir == OMPD_taskgroup) {
1483614921
if (DeclareReductionRef.isUsable())
1483714922
Stack->addTaskgroupReductionData(D, ReductionIdRange,
@@ -14858,8 +14943,22 @@ OMPClause *Sema::ActOnOpenMPReductionClause(
1485814943
<< getOpenMPClauseName(OMPC_reduction);
1485914944
return nullptr;
1486014945
}
14946+
// OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
14947+
// A reduction clause with the inscan reduction-modifier may only appear on a
14948+
// worksharing-loop construct, a worksharing-loop SIMD construct, a simd
14949+
// construct, a parallel worksharing-loop construct or a parallel
14950+
// worksharing-loop SIMD construct.
14951+
if (Modifier == OMPC_REDUCTION_inscan &&
14952+
(DSAStack->getCurrentDirective() != OMPD_for &&
14953+
DSAStack->getCurrentDirective() != OMPD_for_simd &&
14954+
DSAStack->getCurrentDirective() != OMPD_simd &&
14955+
DSAStack->getCurrentDirective() != OMPD_parallel_for &&
14956+
DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
14957+
Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction);
14958+
return nullptr;
14959+
}
1486114960

14862-
ReductionData RD(VarList.size());
14961+
ReductionData RD(VarList.size(), Modifier);
1486314962
if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
1486414963
StartLoc, LParenLoc, ColonLoc, EndLoc,
1486514964
ReductionIdScopeSpec, ReductionId,
@@ -18161,6 +18260,19 @@ OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
1816118260
if (!D)
1816218261
continue;
1816318262

18263+
const DSAStackTy::DSAVarData DVar =
18264+
DSAStack->getTopDSA(D, /*FromParent=*/true);
18265+
// OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
18266+
// A list item that appears in the inclusive or exclusive clause must appear
18267+
// in a reduction clause with the inscan modifier on the enclosing
18268+
// worksharing-loop, worksharing-loop SIMD, or simd construct.
18269+
if (DVar.CKind != OMPC_reduction ||
18270+
DVar.Modifier != OMPC_REDUCTION_inscan)
18271+
Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
18272+
<< RefExpr->getSourceRange();
18273+
18274+
if (DSAStack->getParentDirective() != OMPD_unknown)
18275+
DSAStack->markDeclAsUsedInScanDirective(D);
1816418276
Vars.push_back(RefExpr);
1816518277
}
1816618278

@@ -18189,6 +18301,21 @@ OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
1818918301
if (!D)
1819018302
continue;
1819118303

18304+
OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
18305+
DSAStackTy::DSAVarData DVar;
18306+
if (ParentDirective != OMPD_unknown)
18307+
DVar = DSAStack->getTopDSA(D, /*FromParent=*/true);
18308+
// OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
18309+
// A list item that appears in the inclusive or exclusive clause must appear
18310+
// in a reduction clause with the inscan modifier on the enclosing
18311+
// worksharing-loop, worksharing-loop SIMD, or simd construct.
18312+
if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction ||
18313+
DVar.Modifier != OMPC_REDUCTION_inscan) {
18314+
Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
18315+
<< RefExpr->getSourceRange();
18316+
} else {
18317+
DSAStack->markDeclAsUsedInScanDirective(D);
18318+
}
1819218319
Vars.push_back(RefExpr);
1819318320
}
1819418321

clang/test/OpenMP/nesting_of_regions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2892,7 +2892,7 @@ void foo() {
28922892
}
28932893
#pragma omp parallel for simd
28942894
for (int i = 0; i < 10; ++i) {
2895-
#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}}
2895+
#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}}
28962896
bar();
28972897
}
28982898
#pragma omp parallel for simd

0 commit comments

Comments
 (0)