Skip to content

Commit cf392f0

Browse files
author
Chandra Ghale
committed
Inital support for privavate variable reduction
1 parent d34f7ea commit cf392f0

File tree

5 files changed

+101
-10
lines changed

5 files changed

+101
-10
lines changed

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 7 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,9 @@ OPENMP_REDUCTION_MODIFIER(default)
202205
OPENMP_REDUCTION_MODIFIER(inscan)
203206
OPENMP_REDUCTION_MODIFIER(task)
204207

208+
OPENMP_ORIGINAL_SHARING_MODIFIER(shared)
209+
OPENMP_ORIGINAL_SHARING_MODIFIER(private)
210+
205211
// Adjust-op kinds for the 'adjust_args' clause.
206212
OPENMP_ADJUST_ARGS_KIND(nothing)
207213
OPENMP_ADJUST_ARGS_KIND(need_device_ptr)
@@ -231,6 +237,7 @@ OPENMP_DOACROSS_MODIFIER(source_omp_cur_iteration)
231237
#undef OPENMP_BIND_KIND
232238
#undef OPENMP_ADJUST_ARGS_KIND
233239
#undef OPENMP_REDUCTION_MODIFIER
240+
#undef OPENMP_ORIGINAL_SHARING_MODIFIER
234241
#undef OPENMP_DEVICE_MODIFIER
235242
#undef OPENMP_ORDER_KIND
236243
#undef OPENMP_ORDER_MODIFIER

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 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_default,
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ class SemaOpenMP : public SemaBase {
11361136
DeclarationNameInfo ReductionOrMapperId;
11371137
int ExtraModifier = -1; ///< Additional modifier for linear, map, depend or
11381138
///< lastprivate clause.
1139+
int OriginalSharingModifier = 0; // Default is shared
11391140
SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
11401141
MapTypeModifiers;
11411142
SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>
@@ -1145,6 +1146,7 @@ class SemaOpenMP : public SemaBase {
11451146
SmallVector<SourceLocation, NumberOfOMPMotionModifiers> MotionModifiersLoc;
11461147
bool IsMapTypeImplicit = false;
11471148
SourceLocation ExtraModifierLoc;
1149+
SourceLocation OriginalSharingModifierLoc;
11481150
SourceLocation OmpAllMemoryLoc;
11491151
SourceLocation
11501152
StepModifierLoc; /// 'step' modifier location for linear clause
@@ -1197,7 +1199,8 @@ class SemaOpenMP : public SemaBase {
11971199
SourceLocation ModifierLoc, SourceLocation ColonLoc,
11981200
SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
11991201
const DeclarationNameInfo &ReductionId,
1200-
ArrayRef<Expr *> UnresolvedReductions = {});
1202+
ArrayRef<Expr *> UnresolvedReductions = {},
1203+
OpenMPOriginalSharingModifier OriginalShareModifier = OMPC_ORIGINAL_SHARING_default);
12011204
/// Called on well-formed 'task_reduction' clause.
12021205
OMPClause *ActOnOpenMPTaskReductionClause(
12031206
ArrayRef<Expr *> VarList, SourceLocation StartLoc,

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4594,6 +4594,33 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
45944594
assert(Tok.is(tok::comma) && "Expected comma.");
45954595
(void)ConsumeToken();
45964596
}
4597+
// Handle original(private / shared) Modifier
4598+
if (Kind == OMPC_reduction && getLangOpts().OpenMP >= 60 &&
4599+
Tok.is(tok::identifier) && PP.getSpelling(Tok) == "original" &&
4600+
NextToken().is(tok::l_paren)) {
4601+
// Parse original(private) modifier.
4602+
ConsumeToken();
4603+
BalancedDelimiterTracker ParenT(*this, tok::l_paren, tok::r_paren);
4604+
ParenT.consumeOpen();
4605+
if (Tok.is(tok::kw_private)) {
4606+
Data.OriginalSharingModifier = OMPC_ORIGINAL_SHARING_private;
4607+
Data.OriginalSharingModifierLoc = Tok.getLocation();
4608+
ConsumeToken();
4609+
}
4610+
else if (Tok.is(tok::identifier) && PP.getSpelling(Tok) == "shared") {
4611+
Data.OriginalSharingModifier = OMPC_ORIGINAL_SHARING_shared;
4612+
Data.OriginalSharingModifierLoc = Tok.getLocation();
4613+
ConsumeToken();
4614+
} else {
4615+
Diag(Tok.getLocation(), diag::err_expected) << "'private or shared'";
4616+
SkipUntil(tok::r_paren);
4617+
return false;
4618+
}
4619+
ParenT.consumeClose();
4620+
assert(Tok.is(tok::comma) && "Expected comma.");
4621+
(void)ConsumeToken();
4622+
}
4623+
45974624
ColonProtectionRAIIObject ColonRAII(*this);
45984625
if (getLangOpts().CPlusPlus)
45994626
ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17054,6 +17054,7 @@ OMPClause *SemaOpenMP::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
1705417054
SourceLocation EndLoc = Locs.EndLoc;
1705517055
OMPClause *Res = nullptr;
1705617056
int ExtraModifier = Data.ExtraModifier;
17057+
int OriginalSharingModifier = Data.OriginalSharingModifier;
1705717058
SourceLocation ExtraModifierLoc = Data.ExtraModifierLoc;
1705817059
SourceLocation ColonLoc = Data.ColonLoc;
1705917060
switch (Kind) {
@@ -17079,7 +17080,8 @@ OMPClause *SemaOpenMP::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
1707917080
Res = ActOnOpenMPReductionClause(
1708017081
VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier),
1708117082
StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc,
17082-
Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId);
17083+
Data.ReductionOrMapperIdScopeSpec, Data.ReductionOrMapperId, {},
17084+
static_cast<OpenMPOriginalSharingModifier>(OriginalSharingModifier));
1708317085
break;
1708417086
case OMPC_task_reduction:
1708517087
Res = ActOnOpenMPTaskReductionClause(
@@ -18220,6 +18222,8 @@ namespace {
1822018222
struct ReductionData {
1822118223
/// List of original reduction items.
1822218224
SmallVector<Expr *, 8> Vars;
18225+
/// OpenMP 6.0 List of internal shared private reduction items
18226+
SmallVector<Expr *, 8> VarsTmp;
1822318227
/// List of private copies of the reduction items.
1822418228
SmallVector<Expr *, 8> Privates;
1822518229
/// LHS expressions for the reduction_op expressions.
@@ -18243,9 +18247,12 @@ struct ReductionData {
1824318247
SmallVector<Expr *, 4> ExprPostUpdates;
1824418248
/// Reduction modifier.
1824518249
unsigned RedModifier = 0;
18250+
/// OpenMP 6.9 Original sharing modifier
18251+
unsigned OrigSharingModifier = 0;
1824618252
ReductionData() = delete;
1824718253
/// Reserves required memory for the reduction data.
18248-
ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) {
18254+
ReductionData(unsigned Size, unsigned Modifier = 0, unsigned OrgModifier = 0) : RedModifier(Modifier),
18255+
OrigSharingModifier(OrgModifier) {
1824918256
Vars.reserve(Size);
1825018257
Privates.reserve(Size);
1825118258
LHSs.reserve(Size);
@@ -18256,6 +18263,9 @@ struct ReductionData {
1825618263
InscanCopyArrayTemps.reserve(Size);
1825718264
InscanCopyArrayElems.reserve(Size);
1825818265
}
18266+
if( OrigSharingModifier == OMPC_ORIGINAL_SHARING_private){
18267+
VarsTmp.reserve(Size);
18268+
}
1825918269
TaskgroupDescriptors.reserve(Size);
1826018270
ExprCaptures.reserve(Size);
1826118271
ExprPostUpdates.reserve(Size);
@@ -18274,6 +18284,10 @@ struct ReductionData {
1827418284
InscanCopyArrayTemps.push_back(nullptr);
1827518285
InscanCopyArrayElems.push_back(nullptr);
1827618286
}
18287+
// To be analyze later
18288+
if( OrigSharingModifier == OMPC_ORIGINAL_SHARING_private){
18289+
VarsTmp.emplace_back(Item);
18290+
}
1827718291
}
1827818292
/// Stores reduction data.
1827918293
void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
@@ -18599,7 +18613,7 @@ static bool actOnOMPReductionKindClause(
1859918613
S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
1860018614
continue;
1860118615
}
18602-
if (DVar.CKind != OMPC_unknown) {
18616+
if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1860318617
S.Diag(ELoc, diag::err_omp_wrong_dsa)
1860418618
<< getOpenMPClauseName(DVar.CKind)
1860518619
<< getOpenMPClauseName(OMPC_reduction);
@@ -18611,7 +18625,7 @@ static bool actOnOMPReductionKindClause(
1861118625
// A list item that appears in a reduction clause of a worksharing
1861218626
// construct must be shared in the parallel regions to which any of the
1861318627
// worksharing regions arising from the worksharing construct bind.
18614-
if (isOpenMPWorksharingDirective(CurrDir) &&
18628+
if (RD.OrigSharingModifier !=OMPC_ORIGINAL_SHARING_private && isOpenMPWorksharingDirective(CurrDir) &&
1861518629
!isOpenMPParallelDirective(CurrDir) &&
1861618630
!isOpenMPTeamsDirective(CurrDir)) {
1861718631
DVar = Stack->getImplicitDSA(D, true);
@@ -19117,7 +19131,7 @@ OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
1911719131
SourceLocation StartLoc, SourceLocation LParenLoc,
1911819132
SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc,
1911919133
CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
19120-
ArrayRef<Expr *> UnresolvedReductions) {
19134+
ArrayRef<Expr *> UnresolvedReductions, OpenMPOriginalSharingModifier OriginalSharingMod ) {
1912119135
if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) {
1912219136
Diag(LParenLoc, diag::err_omp_unexpected_clause_value)
1912319137
<< getListOfPossibleValues(OMPC_reduction, /*First=*/0,
@@ -19130,7 +19144,8 @@ OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
1913019144
// worksharing-loop construct, a worksharing-loop SIMD construct, a simd
1913119145
// construct, a parallel worksharing-loop construct or a parallel
1913219146
// worksharing-loop SIMD construct.
19133-
if (Modifier == OMPC_REDUCTION_inscan &&
19147+
if ((Modifier == OMPC_REDUCTION_inscan || OriginalSharingMod == OMPC_ORIGINAL_SHARING_private
19148+
|| OriginalSharingMod == OMPC_ORIGINAL_SHARING_shared ) &&
1913419149
(DSAStack->getCurrentDirective() != OMPD_for &&
1913519150
DSAStack->getCurrentDirective() != OMPD_for_simd &&
1913619151
DSAStack->getCurrentDirective() != OMPD_simd &&
@@ -19139,9 +19154,41 @@ OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
1913919154
Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction);
1914019155
return nullptr;
1914119156
}
19142-
19143-
ReductionData RD(VarList.size(), Modifier);
19144-
if (actOnOMPReductionKindClause(SemaRef, DSAStack, OMPC_reduction, VarList,
19157+
19158+
SmallVector<Expr *, 4> ProcessedVarList;
19159+
for (Expr *E : VarList) {
19160+
DeclRefExpr *OrigDRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts());
19161+
if( !OrigDRE ){
19162+
ProcessedVarList.push_back(E);
19163+
continue;
19164+
}
19165+
ValueDecl *OrigDecl = OrigDRE->getDecl();
19166+
DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(OrigDecl, true);
19167+
if (DVar.CKind == OMPC_private || DVar.CKind == OMPC_firstprivate ||
19168+
OriginalSharingMod == OMPC_ORIGINAL_SHARING_private )
19169+
{
19170+
19171+
VarDecl *SharedVar = buildVarDecl(SemaRef, E->getExprLoc(), E->getType(),
19172+
".omp.reduction.shared.tmp", nullptr, nullptr);
19173+
SemaRef.getCurScope()->AddDecl(SharedVar);
19174+
SharedVar->setImplicit();
19175+
DeclRefExpr* SharedRef = buildDeclRefExpr(SemaRef, SharedVar,
19176+
SharedVar->getType(),
19177+
E->getExprLoc());
19178+
DSAStack->addDSA(SharedVar, nullptr, OMPC_shared);
19179+
ExprResult Init = SemaRef.BuildBinOp( SemaRef.getCurScope(), OrigDRE->getBeginLoc(), BO_Assign, static_cast<Expr*>(SharedRef), E);
19180+
if (!Init.isInvalid())
19181+
SemaRef.AddInitializerToDecl(SharedVar, Init.get(), false);
19182+
19183+
ProcessedVarList.push_back(static_cast<Expr*>(SharedRef));
19184+
}
19185+
else
19186+
{
19187+
ProcessedVarList.push_back(E);
19188+
}
19189+
}
19190+
ReductionData RD(ProcessedVarList.size(), Modifier, OriginalSharingMod );
19191+
if (actOnOMPReductionKindClause(SemaRef, DSAStack, OMPC_reduction, ProcessedVarList,
1914519192
StartLoc, LParenLoc, ColonLoc, EndLoc,
1914619193
ReductionIdScopeSpec, ReductionId,
1914719194
UnresolvedReductions, RD))

0 commit comments

Comments
 (0)