Skip to content

Commit ea22ec1

Browse files
author
Chandra Ghale
committed
Parse/Sema support for reduction over private variable with reduction clause
1 parent 0f0d3fb commit ea22ec1

File tree

13 files changed

+1086
-946
lines changed

13 files changed

+1086
-946
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3678,6 +3678,9 @@ class OMPReductionClause final
36783678
/// Name of custom operator.
36793679
DeclarationNameInfo NameInfo;
36803680

3681+
/// Private variable reduction flag
3682+
llvm::SmallVector<bool, 8> IsPrivateVarReductionFlags;
3683+
36813684
/// Build clause with number of variables \a N.
36823685
///
36833686
/// \param StartLoc Starting location of the clause.
@@ -3854,6 +3857,7 @@ class OMPReductionClause final
38543857
/// region with this clause.
38553858
/// \param PostUpdate Expression that must be executed after exit from the
38563859
/// OpenMP region with this clause.
3860+
/// \pram IsPrivateVarReduction array for private variable reduction flags
38573861
static OMPReductionClause *
38583862
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
38593863
SourceLocation ModifierLoc, SourceLocation ColonLoc,
@@ -3863,7 +3867,7 @@ class OMPReductionClause final
38633867
ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
38643868
ArrayRef<Expr *> ReductionOps, ArrayRef<Expr *> CopyOps,
38653869
ArrayRef<Expr *> CopyArrayTemps, ArrayRef<Expr *> CopyArrayElems,
3866-
Stmt *PreInit, Expr *PostUpdate);
3870+
Stmt *PreInit, Expr *PostUpdate, ArrayRef<bool> IsPrivateVarReduction);
38673871

38683872
/// Creates an empty clause with the place for \a N variables.
38693873
///
@@ -3889,6 +3893,16 @@ class OMPReductionClause final
38893893
/// Gets the nested name specifier.
38903894
NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
38913895

3896+
/// Get all private variable reduction flags
3897+
ArrayRef<bool> getPrivateVariableReductionFlags() const {
3898+
return IsPrivateVarReductionFlags;
3899+
}
3900+
3901+
//// Set private variable reduction flags
3902+
void setPrivateVariableReductionFlags(ArrayRef<bool> Flags) {
3903+
std::copy(Flags.begin(), Flags.end(), IsPrivateVarReductionFlags.begin());
3904+
}
3905+
38923906
using helper_expr_iterator = MutableArrayRef<Expr *>::iterator;
38933907
using helper_expr_const_iterator = ArrayRef<const Expr *>::iterator;
38943908
using helper_expr_range = llvm::iterator_range<helper_expr_iterator>;

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
#ifndef OPENMP_REDUCTION_MODIFIER
7272
#define OPENMP_REDUCTION_MODIFIER(Name)
7373
#endif
74+
#ifndef OPENMP_ORIGINAL_SHARING_MODIFIER
75+
#define OPENMP_ORIGINAL_SHARING_MODIFIER(Name)
76+
#endif
7477
#ifndef OPENMP_ADJUST_ARGS_KIND
7578
#define OPENMP_ADJUST_ARGS_KIND(Name)
7679
#endif
@@ -202,6 +205,11 @@ OPENMP_REDUCTION_MODIFIER(default)
202205
OPENMP_REDUCTION_MODIFIER(inscan)
203206
OPENMP_REDUCTION_MODIFIER(task)
204207

208+
// OpenMP 6.0 modifiers for 'reduction' clause.
209+
OPENMP_ORIGINAL_SHARING_MODIFIER(shared)
210+
OPENMP_ORIGINAL_SHARING_MODIFIER(private)
211+
OPENMP_ORIGINAL_SHARING_MODIFIER(default)
212+
205213
// Adjust-op kinds for the 'adjust_args' clause.
206214
OPENMP_ADJUST_ARGS_KIND(nothing)
207215
OPENMP_ADJUST_ARGS_KIND(need_device_ptr)
@@ -233,6 +241,7 @@ OPENMP_DOACROSS_MODIFIER(source_omp_cur_iteration)
233241
#undef OPENMP_ADJUST_ARGS_KIND
234242
#undef OPENMP_REDUCTION_MODIFIER
235243
#undef OPENMP_DEVICE_MODIFIER
244+
#undef OPENMP_ORIGINAL_SHARING_MODIFIER
236245
#undef OPENMP_ORDER_KIND
237246
#undef OPENMP_ORDER_MODIFIER
238247
#undef OPENMP_LASTPRIVATE_KIND

clang/include/clang/Basic/OpenMPKinds.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ enum OpenMPReductionClauseModifier {
190190
OMPC_REDUCTION_unknown,
191191
};
192192

193+
/// OpenMP 6.0 original sharing modifiers
194+
enum OpenMPOriginalSharingModifier {
195+
#define OPENMP_ORIGINAL_SHARING_MODIFIER(Name) OMPC_ORIGINAL_SHARING_##Name,
196+
#include "clang/Basic/OpenMPKinds.def"
197+
OMPC_ORIGINAL_SHARING_unknown,
198+
};
199+
193200
/// OpenMP adjust-op kinds for 'adjust_args' clause.
194201
enum OpenMPAdjustArgsOpKind {
195202
#define OPENMP_ADJUST_ARGS_KIND(Name) OMPC_ADJUST_ARGS_##Name,

clang/include/clang/Sema/SemaOpenMP.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,7 @@ class SemaOpenMP : public SemaBase {
11421142
DeclarationNameInfo ReductionOrMapperId;
11431143
int ExtraModifier = -1; ///< Additional modifier for linear, map, depend or
11441144
///< lastprivate clause.
1145+
int OriginalSharingModifier = 0; // Default is shared
11451146
SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
11461147
MapTypeModifiers;
11471148
SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>
@@ -1151,6 +1152,7 @@ class SemaOpenMP : public SemaBase {
11511152
SmallVector<SourceLocation, NumberOfOMPMotionModifiers> MotionModifiersLoc;
11521153
bool IsMapTypeImplicit = false;
11531154
SourceLocation ExtraModifierLoc;
1155+
SourceLocation OriginalSharingModifierLoc;
11541156
SourceLocation OmpAllMemoryLoc;
11551157
SourceLocation
11561158
StepModifierLoc; /// 'step' modifier location for linear clause
@@ -1213,7 +1215,9 @@ class SemaOpenMP : public SemaBase {
12131215
SourceLocation ModifierLoc, SourceLocation ColonLoc,
12141216
SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
12151217
const DeclarationNameInfo &ReductionId,
1216-
ArrayRef<Expr *> UnresolvedReductions = {});
1218+
ArrayRef<Expr *> UnresolvedReductions = {},
1219+
OpenMPOriginalSharingModifier OriginalShareModifier =
1220+
OMPC_ORIGINAL_SHARING_default);
12171221
/// Called on well-formed 'task_reduction' clause.
12181222
OMPClause *ActOnOpenMPTaskReductionClause(
12191223
ArrayRef<Expr *> VarList, SourceLocation StartLoc,

clang/lib/AST/OpenMPClause.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ OMPReductionClause *OMPReductionClause::Create(
797797
ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
798798
ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
799799
ArrayRef<Expr *> CopyOps, ArrayRef<Expr *> CopyArrayTemps,
800-
ArrayRef<Expr *> CopyArrayElems, Stmt *PreInit, Expr *PostUpdate) {
800+
ArrayRef<Expr *> CopyArrayElems, Stmt *PreInit, Expr *PostUpdate,ArrayRef<bool> IsPrivateVarReduction) {
801801
void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(
802802
(Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * VL.size()));
803803
auto *Clause = new (Mem)
@@ -810,6 +810,7 @@ OMPReductionClause *OMPReductionClause::Create(
810810
Clause->setReductionOps(ReductionOps);
811811
Clause->setPreInitStmt(PreInit);
812812
Clause->setPostUpdateExpr(PostUpdate);
813+
Clause->setPrivateVariableReductionFlags(IsPrivateVarReduction);
813814
if (Modifier == OMPC_REDUCTION_inscan) {
814815
Clause->setInscanCopyOps(CopyOps);
815816
Clause->setInscanCopyArrayTemps(CopyArrayTemps);

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4668,6 +4668,33 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
46684668
assert(Tok.is(tok::comma) && "Expected comma.");
46694669
(void)ConsumeToken();
46704670
}
4671+
// Handle original(private / shared) Modifier
4672+
if (Kind == OMPC_reduction && getLangOpts().OpenMP >= 60 &&
4673+
Tok.is(tok::identifier) && PP.getSpelling(Tok) == "original" &&
4674+
NextToken().is(tok::l_paren)) {
4675+
// Parse original(private) modifier.
4676+
ConsumeToken();
4677+
BalancedDelimiterTracker ParenT(*this, tok::l_paren, tok::r_paren);
4678+
ParenT.consumeOpen();
4679+
if (Tok.is(tok::kw_private)) {
4680+
Data.OriginalSharingModifier = OMPC_ORIGINAL_SHARING_private;
4681+
Data.OriginalSharingModifierLoc = Tok.getLocation();
4682+
ConsumeToken();
4683+
} else if (Tok.is(tok::identifier) && (PP.getSpelling(Tok) == "shared"
4684+
|| PP.getSpelling(Tok) == "default")) {
4685+
Data.OriginalSharingModifier = OMPC_ORIGINAL_SHARING_shared;
4686+
Data.OriginalSharingModifierLoc = Tok.getLocation();
4687+
ConsumeToken();
4688+
} else {
4689+
Diag(Tok.getLocation(), diag::err_expected)
4690+
<< "'private or shared or default'";
4691+
SkipUntil(tok::r_paren);
4692+
return false;
4693+
}
4694+
ParenT.consumeClose();
4695+
assert(Tok.is(tok::comma) && "Expected comma.");
4696+
(void)ConsumeToken();
4697+
}
46714698
ColonProtectionRAIIObject ColonRAII(*this);
46724699
if (getLangOpts().CPlusPlus)
46734700
ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17366,6 +17366,7 @@ OMPClause *SemaOpenMP::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
1736617366
SourceLocation EndLoc = Locs.EndLoc;
1736717367
OMPClause *Res = nullptr;
1736817368
int ExtraModifier = Data.ExtraModifier;
17369+
int OriginalSharingModifier = Data.OriginalSharingModifier;
1736917370
SourceLocation ExtraModifierLoc = Data.ExtraModifierLoc;
1737017371
SourceLocation ColonLoc = Data.ColonLoc;
1737117372
switch (Kind) {
@@ -17391,7 +17392,8 @@ OMPClause *SemaOpenMP::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
1739117392
Res = ActOnOpenMPReductionClause(
1739217393
VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier),
1739317394
StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc,
17394-
Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId);
17395+
Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, {},
17396+
static_cast<OpenMPOriginalSharingModifier>(OriginalSharingModifier));
1739517397
break;
1739617398
case OMPC_task_reduction:
1739717399
Res = ActOnOpenMPTaskReductionClause(
@@ -18570,14 +18572,20 @@ struct ReductionData {
1857018572
SmallVector<Expr *, 4> ExprPostUpdates;
1857118573
/// Reduction modifier.
1857218574
unsigned RedModifier = 0;
18575+
/// Original modifier.
18576+
unsigned OrigSharingModifier = 0;
18577+
/// Private Variable Reduction
18578+
SmallVector<bool, 8> IsPrivateVarReduction;
1857318579
ReductionData() = delete;
1857418580
/// Reserves required memory for the reduction data.
18575-
ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) {
18581+
ReductionData(unsigned Size, unsigned Modifier = 0, unsigned OrgModifier = 0)
18582+
: RedModifier(Modifier), OrigSharingModifier(OrgModifier) {
1857618583
Vars.reserve(Size);
1857718584
Privates.reserve(Size);
1857818585
LHSs.reserve(Size);
1857918586
RHSs.reserve(Size);
1858018587
ReductionOps.reserve(Size);
18588+
IsPrivateVarReduction.reserve(Size);
1858118589
if (RedModifier == OMPC_REDUCTION_inscan) {
1858218590
InscanCopyOps.reserve(Size);
1858318591
InscanCopyArrayTemps.reserve(Size);
@@ -18595,6 +18603,7 @@ struct ReductionData {
1859518603
LHSs.emplace_back(nullptr);
1859618604
RHSs.emplace_back(nullptr);
1859718605
ReductionOps.emplace_back(ReductionOp);
18606+
IsPrivateVarReduction.emplace_back(false);
1859818607
TaskgroupDescriptors.emplace_back(nullptr);
1859918608
if (RedModifier == OMPC_REDUCTION_inscan) {
1860018609
InscanCopyOps.push_back(nullptr);
@@ -18605,7 +18614,7 @@ struct ReductionData {
1860518614
/// Stores reduction data.
1860618615
void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
1860718616
Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp,
18608-
Expr *CopyArrayElem) {
18617+
Expr *CopyArrayElem, bool IsPrivate) {
1860918618
Vars.emplace_back(Item);
1861018619
Privates.emplace_back(Private);
1861118620
LHSs.emplace_back(LHS);
@@ -18621,6 +18630,7 @@ struct ReductionData {
1862118630
CopyArrayElem == nullptr &&
1862218631
"Copy operation must be used for inscan reductions only.");
1862318632
}
18633+
IsPrivateVarReduction.emplace_back(IsPrivate);
1862418634
}
1862518635
};
1862618636
} // namespace
@@ -18834,6 +18844,7 @@ static bool actOnOMPReductionKindClause(
1883418844
FirstIter = false;
1883518845
SourceLocation ELoc;
1883618846
SourceRange ERange;
18847+
bool IsPrivate = false;
1883718848
Expr *SimpleRefExpr = RefExpr;
1883818849
auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
1883918850
/*AllowArraySection=*/true);
@@ -18933,12 +18944,35 @@ static bool actOnOMPReductionKindClause(
1893318944
reportOriginalDsa(S, Stack, D, DVar);
1893418945
continue;
1893518946
}
18947+
// OpenMP 6.0 [ 7.6.10 ]
18948+
// Support Reduction over private variables with reduction clause.
18949+
// A list item in a reduction clause can now be private in the enclosing
18950+
// context. For orphaned constructs it is assumed to be shared unless the
18951+
// original(private) modifier appears in the clause.
18952+
DVar = Stack->getImplicitDSA(D, true);
18953+
bool IsOrphaned = false;
18954+
OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
18955+
OpenMPDirectiveKind ParentDir = Stack->getParentDirective();
18956+
// Check if the construct is orphaned (has no enclosing OpenMP context)
18957+
IsOrphaned = (ParentDir == OMPD_unknown);
18958+
IsPrivate =
18959+
((isOpenMPPrivate(DVar.CKind) && DVar.CKind != OMPC_reduction &&
18960+
isOpenMPWorksharingDirective(CurrDir) &&
18961+
!isOpenMPParallelDirective(CurrDir) &&
18962+
!isOpenMPTeamsDirective(CurrDir) &&
18963+
!isOpenMPSimdDirective(ParentDir)) ||
18964+
(IsOrphaned && DVar.CKind == OMPC_unknown) ||
18965+
RD.OrigSharingModifier != OMPC_ORIGINAL_SHARING_shared);
18966+
// Disable private handling for OpenMP versions <= 5.2
18967+
if (S.getLangOpts().OpenMP <= 52)
18968+
IsPrivate = false;
1893618969

1893718970
// OpenMP [2.14.3.6, Restrictions, p.1]
1893818971
// A list item that appears in a reduction clause of a worksharing
1893918972
// construct must be shared in the parallel regions to which any of the
1894018973
// worksharing regions arising from the worksharing construct bind.
18941-
if (isOpenMPWorksharingDirective(CurrDir) &&
18974+
18975+
if (!IsPrivate && isOpenMPWorksharingDirective(CurrDir) &&
1894218976
!isOpenMPParallelDirective(CurrDir) &&
1894318977
!isOpenMPTeamsDirective(CurrDir)) {
1894418978
DVar = Stack->getImplicitDSA(D, true);
@@ -19434,7 +19468,7 @@ static bool actOnOMPReductionKindClause(
1943419468
}
1943519469
RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
1943619470
TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(),
19437-
TempArrayElem.get());
19471+
TempArrayElem.get(), IsPrivate);
1943819472
}
1943919473
return RD.Vars.empty();
1944019474
}
@@ -19444,7 +19478,8 @@ OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
1944419478
SourceLocation StartLoc, SourceLocation LParenLoc,
1944519479
SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc,
1944619480
CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
19447-
ArrayRef<Expr *> UnresolvedReductions) {
19481+
ArrayRef<Expr *> UnresolvedReductions,
19482+
OpenMPOriginalSharingModifier OriginalSharingMod) {
1944819483
if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) {
1944919484
Diag(LParenLoc, diag::err_omp_unexpected_clause_value)
1945019485
<< getListOfPossibleValues(OMPC_reduction, /*First=*/0,
@@ -19466,8 +19501,7 @@ OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
1946619501
Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction);
1946719502
return nullptr;
1946819503
}
19469-
19470-
ReductionData RD(VarList.size(), Modifier);
19504+
ReductionData RD(VarList.size(), Modifier, OriginalSharingMod);
1947119505
if (actOnOMPReductionKindClause(SemaRef, DSAStack, OMPC_reduction, VarList,
1947219506
StartLoc, LParenLoc, ColonLoc, EndLoc,
1947319507
ReductionIdScopeSpec, ReductionId,
@@ -19481,7 +19515,7 @@ OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
1948119515
RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps,
1948219516
RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems,
1948319517
buildPreInits(getASTContext(), RD.ExprCaptures),
19484-
buildPostUpdate(SemaRef, RD.ExprPostUpdates));
19518+
buildPostUpdate(SemaRef, RD.ExprPostUpdates), RD.IsPrivateVarReduction);
1948519519
}
1948619520

1948719521
OMPClause *SemaOpenMP::ActOnOpenMPTaskReductionClause(

0 commit comments

Comments
 (0)