@@ -62,14 +62,15 @@ class DSAStackTy {
62
62
struct DSAVarData {
63
63
OpenMPDirectiveKind DKind = OMPD_unknown;
64
64
OpenMPClauseKind CKind = OMPC_unknown;
65
+ unsigned Modifier = 0;
65
66
const Expr *RefExpr = nullptr;
66
67
DeclRefExpr *PrivateCopy = nullptr;
67
68
SourceLocation ImplicitDSALoc;
68
69
DSAVarData() = default;
69
70
DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
70
71
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),
73
74
PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
74
75
};
75
76
using OperatorOffsetTy =
@@ -80,6 +81,7 @@ class DSAStackTy {
80
81
private:
81
82
struct DSAInfo {
82
83
OpenMPClauseKind Attributes = OMPC_unknown;
84
+ unsigned Modifier = 0;
83
85
/// Pointer to a reference expression and a flag which shows that the
84
86
/// variable is marked as lastprivate(true) or not (false).
85
87
llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
@@ -164,6 +166,8 @@ class DSAStackTy {
164
166
/// List of globals marked as declare target link in this target region
165
167
/// (isOpenMPTargetExecutionDirective(Directive) == true).
166
168
llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
169
+ /// List of decls used in inclusive/exclusive clauses of the scan directive.
170
+ llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
167
171
SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
168
172
Scope *CurScope, SourceLocation Loc)
169
173
: Directive(DKind), DirectiveName(Name), CurScope(CurScope),
@@ -469,9 +473,20 @@ class DSAStackTy {
469
473
/// parent directive.
470
474
const ValueDecl *getParentLoopControlVariable(unsigned I) const;
471
475
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
+ return getTopOfStack().UsedInScanDirective.count(D) > 0;
485
+ }
486
+
472
487
/// Adds explicit data sharing attribute to the specified declaration.
473
488
void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
474
- DeclRefExpr *PrivateCopy = nullptr);
489
+ DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0 );
475
490
476
491
/// Adds additional information for the reduction items with the reduction id
477
492
/// represented as an operator.
@@ -1079,6 +1094,7 @@ DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1079
1094
DVar.PrivateCopy = Data.PrivateCopy;
1080
1095
DVar.CKind = Data.Attributes;
1081
1096
DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1097
+ DVar.Modifier = Data.Modifier;
1082
1098
return DVar;
1083
1099
}
1084
1100
@@ -1226,19 +1242,21 @@ const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1226
1242
}
1227
1243
1228
1244
void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1229
- DeclRefExpr *PrivateCopy) {
1245
+ DeclRefExpr *PrivateCopy, unsigned Modifier ) {
1230
1246
D = getCanonicalDecl(D);
1231
1247
if (A == OMPC_threadprivate) {
1232
1248
DSAInfo &Data = Threadprivates[D];
1233
1249
Data.Attributes = A;
1234
1250
Data.RefExpr.setPointer(E);
1235
1251
Data.PrivateCopy = nullptr;
1252
+ Data.Modifier = Modifier;
1236
1253
} else {
1237
1254
DSAInfo &Data = getTopOfStack().SharingMap[D];
1238
1255
assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1239
1256
(A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1240
1257
(A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1241
1258
(isLoopControlVariable(D).first && A == OMPC_private));
1259
+ Data.Modifier = Modifier;
1242
1260
if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1243
1261
Data.RefExpr.setInt(/*IntVal=*/true);
1244
1262
return;
@@ -1250,6 +1268,7 @@ void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1250
1268
Data.PrivateCopy = PrivateCopy;
1251
1269
if (PrivateCopy) {
1252
1270
DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1271
+ Data.Modifier = Modifier;
1253
1272
Data.Attributes = A;
1254
1273
Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1255
1274
Data.PrivateCopy = nullptr;
@@ -1355,7 +1374,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1355
1374
"set.");
1356
1375
TaskgroupDescriptor = I->TaskgroupReductionRef;
1357
1376
return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1358
- Data.PrivateCopy, I->DefaultAttrLoc);
1377
+ Data.PrivateCopy, I->DefaultAttrLoc, /*Modifier=*/0 );
1359
1378
}
1360
1379
return DSAVarData();
1361
1380
}
@@ -1380,7 +1399,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1380
1399
"set.");
1381
1400
TaskgroupDescriptor = I->TaskgroupReductionRef;
1382
1401
return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1383
- Data.PrivateCopy, I->DefaultAttrLoc);
1402
+ Data.PrivateCopy, I->DefaultAttrLoc, /*Modifier=*/0 );
1384
1403
}
1385
1404
return DSAVarData();
1386
1405
}
@@ -1455,6 +1474,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1455
1474
if (TI != Threadprivates.end()) {
1456
1475
DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1457
1476
DVar.CKind = OMPC_threadprivate;
1477
+ DVar.Modifier = TI->getSecond().Modifier;
1458
1478
return DVar;
1459
1479
}
1460
1480
if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
@@ -1546,6 +1566,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1546
1566
DVar.CKind = Data.Attributes;
1547
1567
DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1548
1568
DVar.DKind = I->Directive;
1569
+ DVar.Modifier = Data.Modifier;
1549
1570
return DVar;
1550
1571
}
1551
1572
@@ -1592,6 +1613,7 @@ const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1592
1613
DVar.CKind = Data.Attributes;
1593
1614
DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1594
1615
DVar.DKind = I->Directive;
1616
+ DVar.Modifier = Data.Modifier;
1595
1617
}
1596
1618
1597
1619
return DVar;
@@ -2315,11 +2337,64 @@ void Sema::EndOpenMPClause() {
2315
2337
DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2316
2338
}
2317
2339
2318
- static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2319
- ArrayRef<OMPClause *> Clauses);
2320
2340
static std::pair<ValueDecl *, bool>
2321
2341
getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2322
2342
SourceRange &ERange, bool AllowArraySection = false);
2343
+
2344
+ /// Check consistency of the reduction clauses.
2345
+ static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2346
+ ArrayRef<OMPClause *> Clauses) {
2347
+ bool InscanFound = false;
2348
+ SourceLocation InscanLoc;
2349
+ // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2350
+ // A reduction clause without the inscan reduction-modifier may not appear on
2351
+ // a construct on which a reduction clause with the inscan reduction-modifier
2352
+ // appears.
2353
+ for (OMPClause *C : Clauses) {
2354
+ if (C->getClauseKind() != OMPC_reduction)
2355
+ continue;
2356
+ auto *RC = cast<OMPReductionClause>(C);
2357
+ if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2358
+ InscanFound = true;
2359
+ InscanLoc = RC->getModifierLoc();
2360
+ break;
2361
+ }
2362
+ }
2363
+ if (InscanFound) {
2364
+ for (OMPClause *C : Clauses) {
2365
+ if (C->getClauseKind() != OMPC_reduction)
2366
+ continue;
2367
+ auto *RC = cast<OMPReductionClause>(C);
2368
+ if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2369
+ S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown
2370
+ ? RC->getBeginLoc()
2371
+ : RC->getModifierLoc(),
2372
+ diag::err_omp_inscan_reduction_expected);
2373
+ S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction);
2374
+ continue;
2375
+ }
2376
+ for (Expr *Ref : RC->varlists()) {
2377
+ assert(Ref && "NULL expr in OpenMP nontemporal clause.");
2378
+ SourceLocation ELoc;
2379
+ SourceRange ERange;
2380
+ Expr *SimpleRefExpr = Ref;
2381
+ auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
2382
+ /*AllowArraySection=*/true);
2383
+ ValueDecl *D = Res.first;
2384
+ if (!D)
2385
+ continue;
2386
+ if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) {
2387
+ S.Diag(Ref->getExprLoc(),
2388
+ diag::err_omp_reduction_not_inclusive_exclusive)
2389
+ << Ref->getSourceRange();
2390
+ }
2391
+ }
2392
+ }
2393
+ }
2394
+ }
2395
+
2396
+ static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2397
+ ArrayRef<OMPClause *> Clauses);
2323
2398
static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2324
2399
bool WithInit);
2325
2400
@@ -2396,6 +2471,7 @@ void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
2396
2471
// Check allocate clauses.
2397
2472
if (!CurContext->isDependentContext())
2398
2473
checkAllocateClauses(*this, DSAStack, D->clauses());
2474
+ checkReductionClauses(*this, DSAStack, D->clauses());
2399
2475
}
2400
2476
2401
2477
DSAStack->pop();
@@ -14111,9 +14187,11 @@ struct ReductionData {
14111
14187
SmallVector<Decl *, 4> ExprCaptures;
14112
14188
/// List of postupdate expressions.
14113
14189
SmallVector<Expr *, 4> ExprPostUpdates;
14190
+ /// Reduction modifier.
14191
+ unsigned RedModifier = 0;
14114
14192
ReductionData() = delete;
14115
14193
/// Reserves required memory for the reduction data.
14116
- ReductionData(unsigned Size) {
14194
+ ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier ) {
14117
14195
Vars.reserve(Size);
14118
14196
Privates.reserve(Size);
14119
14197
LHSs.reserve(Size);
@@ -14831,7 +14909,8 @@ static bool actOnOMPReductionKindClause(
14831
14909
}
14832
14910
// All reduction items are still marked as reduction (to do not increase
14833
14911
// code base size).
14834
- Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
14912
+ Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref,
14913
+ RD.RedModifier);
14835
14914
if (CurrDir == OMPD_taskgroup) {
14836
14915
if (DeclareReductionRef.isUsable())
14837
14916
Stack->addTaskgroupReductionData(D, ReductionIdRange,
@@ -14858,8 +14937,22 @@ OMPClause *Sema::ActOnOpenMPReductionClause(
14858
14937
<< getOpenMPClauseName(OMPC_reduction);
14859
14938
return nullptr;
14860
14939
}
14940
+ // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
14941
+ // A reduction clause with the inscan reduction-modifier may only appear on a
14942
+ // worksharing-loop construct, a worksharing-loop SIMD construct, a simd
14943
+ // construct, a parallel worksharing-loop construct or a parallel
14944
+ // worksharing-loop SIMD construct.
14945
+ if (Modifier == OMPC_REDUCTION_inscan &&
14946
+ (DSAStack->getCurrentDirective() != OMPD_for &&
14947
+ DSAStack->getCurrentDirective() != OMPD_for_simd &&
14948
+ DSAStack->getCurrentDirective() != OMPD_simd &&
14949
+ DSAStack->getCurrentDirective() != OMPD_parallel_for &&
14950
+ DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
14951
+ Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction);
14952
+ return nullptr;
14953
+ }
14861
14954
14862
- ReductionData RD(VarList.size());
14955
+ ReductionData RD(VarList.size(), Modifier );
14863
14956
if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
14864
14957
StartLoc, LParenLoc, ColonLoc, EndLoc,
14865
14958
ReductionIdScopeSpec, ReductionId,
@@ -18161,6 +18254,19 @@ OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
18161
18254
if (!D)
18162
18255
continue;
18163
18256
18257
+ const DSAStackTy::DSAVarData DVar =
18258
+ DSAStack->getTopDSA(D, /*FromParent=*/true);
18259
+ // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
18260
+ // A list item that appears in the inclusive or exclusive clause must appear
18261
+ // in a reduction clause with the inscan modifier on the enclosing
18262
+ // worksharing-loop, worksharing-loop SIMD, or simd construct.
18263
+ if (DVar.CKind != OMPC_reduction ||
18264
+ DVar.Modifier != OMPC_REDUCTION_inscan)
18265
+ Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
18266
+ << RefExpr->getSourceRange();
18267
+
18268
+ if (DSAStack->getParentDirective() != OMPD_unknown)
18269
+ DSAStack->markDeclAsUsedInScanDirective(D);
18164
18270
Vars.push_back(RefExpr);
18165
18271
}
18166
18272
@@ -18189,6 +18295,19 @@ OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
18189
18295
if (!D)
18190
18296
continue;
18191
18297
18298
+ const DSAStackTy::DSAVarData DVar =
18299
+ DSAStack->getTopDSA(D, /*FromParent=*/true);
18300
+ // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
18301
+ // A list item that appears in the inclusive or exclusive clause must appear
18302
+ // in a reduction clause with the inscan modifier on the enclosing
18303
+ // worksharing-loop, worksharing-loop SIMD, or simd construct.
18304
+ if (DVar.CKind != OMPC_reduction ||
18305
+ DVar.Modifier != OMPC_REDUCTION_inscan)
18306
+ Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
18307
+ << RefExpr->getSourceRange();
18308
+
18309
+ if (DSAStack->getParentDirective() != OMPD_unknown)
18310
+ DSAStack->markDeclAsUsedInScanDirective(D);
18192
18311
Vars.push_back(RefExpr);
18193
18312
}
18194
18313
0 commit comments