Skip to content

[OpenMP 6.0] Parse/Sema support for reduction over private variable with reduction clause. #129938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/docs/OpenMPSupport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ implementation.
| Extensions to depobj construct | :none:`unclaimed` | :none:`unclaimed` | |
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
| Extensions to atomic construct | :none:`unclaimed` | :none:`unclaimed` | |
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
| Private reductions | :none:`unclaimed` | :none:`unclaimed` | |
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
| Private reductions | :part:`partial` | :none:`unclaimed` | Parse/Sema:https://github.com/llvm/llvm-project/pull/129938 |
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
| Self maps | :none:`unclaimed` | :none:`unclaimed` | |
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
Expand Down
66 changes: 62 additions & 4 deletions clang/include/clang/AST/OpenMPClause.h
Original file line number Diff line number Diff line change
Expand Up @@ -3658,14 +3658,18 @@ class OMPSharedClause final
class OMPReductionClause final
: public OMPVarListClause<OMPReductionClause>,
public OMPClauseWithPostUpdate,
private llvm::TrailingObjects<OMPReductionClause, Expr *> {
private llvm::TrailingObjects<OMPReductionClause, Expr *, bool> {
friend class OMPClauseReader;
friend OMPVarListClause;
friend TrailingObjects;

/// Reduction modifier.
OpenMPReductionClauseModifier Modifier = OMPC_REDUCTION_unknown;

/// Original Sharing modifier.
OpenMPOriginalSharingModifier OriginalSharingModifier =
OMPC_ORIGINAL_SHARING_default;

/// Reduction modifier location.
SourceLocation ModifierLoc;

Expand All @@ -3691,12 +3695,14 @@ class OMPReductionClause final
OMPReductionClause(SourceLocation StartLoc, SourceLocation LParenLoc,
SourceLocation ModifierLoc, SourceLocation ColonLoc,
SourceLocation EndLoc,
OpenMPReductionClauseModifier Modifier, unsigned N,
NestedNameSpecifierLoc QualifierLoc,
OpenMPReductionClauseModifier Modifier,
OpenMPOriginalSharingModifier OriginalSharingModifier,
unsigned N, NestedNameSpecifierLoc QualifierLoc,
const DeclarationNameInfo &NameInfo)
: OMPVarListClause<OMPReductionClause>(llvm::omp::OMPC_reduction,
StartLoc, LParenLoc, EndLoc, N),
OMPClauseWithPostUpdate(this), Modifier(Modifier),
OriginalSharingModifier(OriginalSharingModifier),
ModifierLoc(ModifierLoc), ColonLoc(ColonLoc),
QualifierLoc(QualifierLoc), NameInfo(NameInfo) {}

Expand All @@ -3712,6 +3718,11 @@ class OMPReductionClause final
/// Sets reduction modifier.
void setModifier(OpenMPReductionClauseModifier M) { Modifier = M; }

/// Sets Original Sharing modifier.
void setOriginalSharingModifier(OpenMPOriginalSharingModifier M) {
OriginalSharingModifier = M;
}

/// Sets location of the modifier.
void setModifierLoc(SourceLocation Loc) { ModifierLoc = Loc; }

Expand Down Expand Up @@ -3757,6 +3768,31 @@ class OMPReductionClause final
/// reduction copies.
void setRHSExprs(ArrayRef<Expr *> RHSExprs);

/// Set the list private reduction flags
void setPrivateVariableReductionFlags(ArrayRef<bool> Flags) {
assert(Flags.size() == varlist_size() &&
"Number of private flags does not match vars");
llvm::copy(Flags, getTrailingObjects<bool>());
}

/// Get the list of help private variable reduction flags
MutableArrayRef<bool> getPrivateVariableReductionFlags() {
return MutableArrayRef(getTrailingObjects<bool>(), varlist_size());
}
ArrayRef<bool> getPrivateVariableReductionFlags() const {
return ArrayRef(getTrailingObjects<bool>(), varlist_size());
}

/// Returns the number of Expr* objects in trailing storage
size_t numTrailingObjects(OverloadToken<Expr *>) const {
return varlist_size() * (Modifier == OMPC_REDUCTION_inscan ? 8 : 5);
}

/// Returns the number of bool flags in trailing storage
size_t numTrailingObjects(OverloadToken<bool>) const {
return varlist_size();
}

/// Get the list of helper destination expressions.
MutableArrayRef<Expr *> getRHSExprs() {
return MutableArrayRef<Expr *>(getLHSExprs().end(), varlist_size());
Expand Down Expand Up @@ -3854,6 +3890,7 @@ class OMPReductionClause final
/// region with this clause.
/// \param PostUpdate Expression that must be executed after exit from the
/// OpenMP region with this clause.
/// \param IsPrivateVarReduction array for private variable reduction flags
static OMPReductionClause *
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
SourceLocation ModifierLoc, SourceLocation ColonLoc,
Expand All @@ -3863,7 +3900,8 @@ class OMPReductionClause final
ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
ArrayRef<Expr *> ReductionOps, ArrayRef<Expr *> CopyOps,
ArrayRef<Expr *> CopyArrayTemps, ArrayRef<Expr *> CopyArrayElems,
Stmt *PreInit, Expr *PostUpdate);
Stmt *PreInit, Expr *PostUpdate, ArrayRef<bool> IsPrivateVarReduction,
OpenMPOriginalSharingModifier OriginalSharingModifier);

/// Creates an empty clause with the place for \a N variables.
///
Expand All @@ -3877,6 +3915,11 @@ class OMPReductionClause final
/// Returns modifier.
OpenMPReductionClauseModifier getModifier() const { return Modifier; }

/// Returns Original Sharing Modifier.
OpenMPOriginalSharingModifier getOriginalSharingModifier() const {
return OriginalSharingModifier;
}

/// Returns modifier location.
SourceLocation getModifierLoc() const { return ModifierLoc; }

Expand All @@ -3894,6 +3937,11 @@ class OMPReductionClause final
using helper_expr_range = llvm::iterator_range<helper_expr_iterator>;
using helper_expr_const_range =
llvm::iterator_range<helper_expr_const_iterator>;
using helper_flag_iterator = MutableArrayRef<bool>::iterator;
using helper_flag_const_iterator = ArrayRef<bool>::iterator;
using helper_flag_range = llvm::iterator_range<helper_flag_iterator>;
using helper_flag_const_range =
llvm::iterator_range<helper_flag_const_iterator>;

helper_expr_const_range privates() const {
return helper_expr_const_range(getPrivates().begin(), getPrivates().end());
Expand All @@ -3919,6 +3967,16 @@ class OMPReductionClause final
return helper_expr_range(getRHSExprs().begin(), getRHSExprs().end());
}

helper_flag_const_range private_var_reduction_flags() const {
return helper_flag_const_range(getPrivateVariableReductionFlags().begin(),
getPrivateVariableReductionFlags().end());
}

helper_flag_range private_var_reduction_flags() {
return helper_flag_range(getPrivateVariableReductionFlags().begin(),
getPrivateVariableReductionFlags().end());
}

helper_expr_const_range reduction_ops() const {
return helper_expr_const_range(getReductionOps().begin(),
getReductionOps().end());
Expand Down
9 changes: 9 additions & 0 deletions clang/include/clang/Basic/OpenMPKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
#ifndef OPENMP_REDUCTION_MODIFIER
#define OPENMP_REDUCTION_MODIFIER(Name)
#endif
#ifndef OPENMP_ORIGINAL_SHARING_MODIFIER
#define OPENMP_ORIGINAL_SHARING_MODIFIER(Name)
#endif
#ifndef OPENMP_ADJUST_ARGS_KIND
#define OPENMP_ADJUST_ARGS_KIND(Name)
#endif
Expand Down Expand Up @@ -202,6 +205,11 @@ OPENMP_REDUCTION_MODIFIER(default)
OPENMP_REDUCTION_MODIFIER(inscan)
OPENMP_REDUCTION_MODIFIER(task)

// OpenMP 6.0 modifiers for 'reduction' clause.
OPENMP_ORIGINAL_SHARING_MODIFIER(shared)
OPENMP_ORIGINAL_SHARING_MODIFIER(private)
OPENMP_ORIGINAL_SHARING_MODIFIER(default)

// Adjust-op kinds for the 'adjust_args' clause.
OPENMP_ADJUST_ARGS_KIND(nothing)
OPENMP_ADJUST_ARGS_KIND(need_device_ptr)
Expand Down Expand Up @@ -233,6 +241,7 @@ OPENMP_DOACROSS_MODIFIER(source_omp_cur_iteration)
#undef OPENMP_ADJUST_ARGS_KIND
#undef OPENMP_REDUCTION_MODIFIER
#undef OPENMP_DEVICE_MODIFIER
#undef OPENMP_ORIGINAL_SHARING_MODIFIER
#undef OPENMP_ORDER_KIND
#undef OPENMP_ORDER_MODIFIER
#undef OPENMP_LASTPRIVATE_KIND
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/OpenMPKinds.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ enum OpenMPReductionClauseModifier {
OMPC_REDUCTION_unknown,
};

/// OpenMP 6.0 original sharing modifiers
enum OpenMPOriginalSharingModifier {
#define OPENMP_ORIGINAL_SHARING_MODIFIER(Name) OMPC_ORIGINAL_SHARING_##Name,
#include "clang/Basic/OpenMPKinds.def"
OMPC_ORIGINAL_SHARING_unknown,
};

/// OpenMP adjust-op kinds for 'adjust_args' clause.
enum OpenMPAdjustArgsOpKind {
#define OPENMP_ADJUST_ARGS_KIND(Name) OMPC_ADJUST_ARGS_##Name,
Expand Down
11 changes: 10 additions & 1 deletion clang/include/clang/Sema/SemaOpenMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ class SemaOpenMP : public SemaBase {
DeclarationNameInfo ReductionOrMapperId;
int ExtraModifier = -1; ///< Additional modifier for linear, map, depend or
///< lastprivate clause.
int OriginalSharingModifier = 0; // Default is shared
SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
MapTypeModifiers;
SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>
Expand All @@ -1151,6 +1152,7 @@ class SemaOpenMP : public SemaBase {
SmallVector<SourceLocation, NumberOfOMPMotionModifiers> MotionModifiersLoc;
bool IsMapTypeImplicit = false;
SourceLocation ExtraModifierLoc;
SourceLocation OriginalSharingModifierLoc;
SourceLocation OmpAllMemoryLoc;
SourceLocation
StepModifierLoc; /// 'step' modifier location for linear clause
Expand All @@ -1160,6 +1162,12 @@ class SemaOpenMP : public SemaBase {
SmallVector<SourceLocation, NumberOfOMPAllocateClauseModifiers>
AllocClauseModifiersLoc;
Expr *AllocateAlignment = nullptr;
struct OpenMPReductionClauseModifiers {
int ExtraModifier;
int OriginalSharingModifier;
OpenMPReductionClauseModifiers(int Extra, int Original)
: ExtraModifier(Extra), OriginalSharingModifier(Original) {}
};
};

OMPClause *ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
Expand Down Expand Up @@ -1208,7 +1216,8 @@ class SemaOpenMP : public SemaBase {
SourceLocation EndLoc);
/// Called on well-formed 'reduction' clause.
OMPClause *ActOnOpenMPReductionClause(
ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier,
ArrayRef<Expr *> VarList,
OpenMPVarListDataTy::OpenMPReductionClauseModifiers Modifiers,
SourceLocation StartLoc, SourceLocation LParenLoc,
SourceLocation ModifierLoc, SourceLocation ColonLoc,
SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Expand Down
19 changes: 11 additions & 8 deletions clang/lib/AST/OpenMPClause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,19 +797,22 @@ OMPReductionClause *OMPReductionClause::Create(
ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
ArrayRef<Expr *> CopyOps, ArrayRef<Expr *> CopyArrayTemps,
ArrayRef<Expr *> CopyArrayElems, Stmt *PreInit, Expr *PostUpdate) {
void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(
(Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * VL.size()));
auto *Clause = new (Mem)
OMPReductionClause(StartLoc, LParenLoc, ModifierLoc, EndLoc, ColonLoc,
Modifier, VL.size(), QualifierLoc, NameInfo);
ArrayRef<Expr *> CopyArrayElems, Stmt *PreInit, Expr *PostUpdate,
ArrayRef<bool> IsPrivateVarReduction,
OpenMPOriginalSharingModifier OrignalSharingModifier) {
void *Mem = C.Allocate(totalSizeToAlloc<Expr *, bool>(
(Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * VL.size(), VL.size()));
auto *Clause = new (Mem) OMPReductionClause(
StartLoc, LParenLoc, ModifierLoc, EndLoc, ColonLoc, Modifier,
OrignalSharingModifier, VL.size(), QualifierLoc, NameInfo);
Clause->setVarRefs(VL);
Clause->setPrivates(Privates);
Clause->setLHSExprs(LHSExprs);
Clause->setRHSExprs(RHSExprs);
Clause->setReductionOps(ReductionOps);
Clause->setPreInitStmt(PreInit);
Clause->setPostUpdateExpr(PostUpdate);
Clause->setPrivateVariableReductionFlags(IsPrivateVarReduction);
if (Modifier == OMPC_REDUCTION_inscan) {
Clause->setInscanCopyOps(CopyOps);
Clause->setInscanCopyArrayTemps(CopyArrayTemps);
Expand All @@ -828,8 +831,8 @@ OMPReductionClause *OMPReductionClause::Create(
OMPReductionClause *
OMPReductionClause::CreateEmpty(const ASTContext &C, unsigned N,
OpenMPReductionClauseModifier Modifier) {
void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(
(Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * N));
void *Mem = C.Allocate(totalSizeToAlloc<Expr *, bool>(
(Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * N, N));
auto *Clause = new (Mem) OMPReductionClause(N);
Clause->setModifier(Modifier);
return Clause;
Expand Down
31 changes: 31 additions & 0 deletions clang/lib/Parse/ParseOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4668,6 +4668,37 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
assert(Tok.is(tok::comma) && "Expected comma.");
(void)ConsumeToken();
}
// Handle original(private / shared) Modifier
if (Kind == OMPC_reduction && getLangOpts().OpenMP >= 60 &&
Tok.is(tok::identifier) && PP.getSpelling(Tok) == "original" &&
NextToken().is(tok::l_paren)) {
// Parse original(private) modifier.
ConsumeToken();
BalancedDelimiterTracker ParenT(*this, tok::l_paren, tok::r_paren);
ParenT.consumeOpen();
if (Tok.is(tok::kw_private)) {
Data.OriginalSharingModifier = OMPC_ORIGINAL_SHARING_private;
Data.OriginalSharingModifierLoc = Tok.getLocation();
ConsumeToken();
} else if (Tok.is(tok::identifier) &&
(PP.getSpelling(Tok) == "shared" ||
PP.getSpelling(Tok) == "default")) {
Data.OriginalSharingModifier = OMPC_ORIGINAL_SHARING_shared;
Data.OriginalSharingModifierLoc = Tok.getLocation();
ConsumeToken();
} else {
Diag(Tok.getLocation(), diag::err_expected)
<< "'private or shared or default'";
SkipUntil(tok::r_paren);
return false;
}
ParenT.consumeClose();
if (!Tok.is(tok::comma)) {
Diag(Tok.getLocation(), diag::err_expected) << "',' (comma)";
return false;
}
(void)ConsumeToken();
}
ColonProtectionRAIIObject ColonRAII(*this);
if (getLangOpts().CPlusPlus)
ParseOptionalCXXScopeSpecifier(Data.ReductionOrMapperIdScopeSpec,
Expand Down
Loading