Skip to content

[clang][OpenMP] Add 'align' modifier for 'allocate' clause #121814

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
Jan 13, 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
2 changes: 2 additions & 0 deletions clang/docs/OpenMPSupport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ implementation.
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
| memory management | 'allocator' modifier for allocate clause | :good:`done` | https://github.com/llvm/llvm-project/pull/114883 |
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
| memory management | 'align' modifier for allocate clause | :good:`done` | https://github.com/llvm/llvm-project/pull/121814 |
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
| memory management | new memory management routines | :none:`unclaimed` | |
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
| memory management | changes to omp_alloctrait_key enum | :none:`unclaimed` | |
Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,7 @@ OpenMP Support
always build support for AMDGPU and NVPTX targets.
- Added support for combined masked constructs 'omp parallel masked taskloop',
'omp parallel masked taskloop simd','omp masked taskloop' and 'omp masked taskloop simd' directive.
- Added support for align-modifier in 'allocate' clause.

Improvements
^^^^^^^^^^^^
Expand Down
92 changes: 81 additions & 11 deletions clang/include/clang/AST/OpenMPClause.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,51 @@ class OMPAllocateClause final
/// Allocator specified in the clause, or 'nullptr' if the default one is
/// used.
Expr *Allocator = nullptr;
/// Alignment specified in the clause, or 'nullptr' if the default one is
/// used.
Expr *Alignment = nullptr;
/// Position of the ':' delimiter in the clause;
SourceLocation ColonLoc;
/// Modifier of 'allocate' clause.
OpenMPAllocateClauseModifier AllocatorModifier = OMPC_ALLOCATE_unknown;
/// Location of allocator modifier if any.
SourceLocation AllocatorModifierLoc;

// ----------------------------------------------------------------------------

/// Modifiers for 'allocate' clause.
enum { FIRST, SECOND, NUM_MODIFIERS };
OpenMPAllocateClauseModifier Modifiers[NUM_MODIFIERS];

/// Locations of modifiers.
SourceLocation ModifiersLoc[NUM_MODIFIERS];

/// Set the first allocate modifier.
///
/// \param M Allocate modifier.
void setFirstAllocateModifier(OpenMPAllocateClauseModifier M) {
Modifiers[FIRST] = M;
}

/// Set the second allocate modifier.
///
/// \param M Allocate modifier.
void setSecondAllocateModifier(OpenMPAllocateClauseModifier M) {
Modifiers[SECOND] = M;
}

/// Set location of the first allocate modifier.
void setFirstAllocateModifierLoc(SourceLocation Loc) {
ModifiersLoc[FIRST] = Loc;
}

/// Set location of the second allocate modifier.
void setSecondAllocateModifierLoc(SourceLocation Loc) {
ModifiersLoc[SECOND] = Loc;
}

// ----------------------------------------------------------------------------

/// Build clause with number of variables \a N.
///
/// \param StartLoc Starting location of the clause.
Expand All @@ -514,23 +552,31 @@ class OMPAllocateClause final
/// \param EndLoc Ending location of the clause.
/// \param N Number of the variables in the clause.
OMPAllocateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
Expr *Allocator, SourceLocation ColonLoc,
OpenMPAllocateClauseModifier AllocatorModifier,
SourceLocation AllocatorModifierLoc, SourceLocation EndLoc,
Expr *Allocator, Expr *Alignment, SourceLocation ColonLoc,
OpenMPAllocateClauseModifier Modifier1,
SourceLocation Modifier1Loc,
OpenMPAllocateClauseModifier Modifier2,
SourceLocation Modifier2Loc, SourceLocation EndLoc,
unsigned N)
: OMPVarListClause<OMPAllocateClause>(llvm::omp::OMPC_allocate, StartLoc,
LParenLoc, EndLoc, N),
Allocator(Allocator), ColonLoc(ColonLoc),
AllocatorModifier(AllocatorModifier),
AllocatorModifierLoc(AllocatorModifierLoc) {}
Allocator(Allocator), Alignment(Alignment), ColonLoc(ColonLoc) {
Modifiers[FIRST] = Modifier1;
Modifiers[SECOND] = Modifier2;
ModifiersLoc[FIRST] = Modifier1Loc;
ModifiersLoc[SECOND] = Modifier2Loc;
}

/// Build an empty clause.
///
/// \param N Number of variables.
explicit OMPAllocateClause(unsigned N)
: OMPVarListClause<OMPAllocateClause>(llvm::omp::OMPC_allocate,
SourceLocation(), SourceLocation(),
SourceLocation(), N) {}
SourceLocation(), N) {
Modifiers[FIRST] = OMPC_ALLOCATE_unknown;
Modifiers[SECOND] = OMPC_ALLOCATE_unknown;
}

/// Sets location of ':' symbol in clause.
void setColonLoc(SourceLocation CL) { ColonLoc = CL; }
Expand All @@ -539,6 +585,7 @@ class OMPAllocateClause final
void setAllocatorModifier(OpenMPAllocateClauseModifier AM) {
AllocatorModifier = AM;
}
void setAlignment(Expr *A) { Alignment = A; }

public:
/// Creates clause with a list of variables \a VL.
Expand All @@ -554,19 +601,42 @@ class OMPAllocateClause final
/// \param VL List of references to the variables.
static OMPAllocateClause *
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
Expr *Allocator, SourceLocation ColonLoc,
OpenMPAllocateClauseModifier AllocatorModifier,
SourceLocation AllocatorModifierLoc, SourceLocation EndLoc,
ArrayRef<Expr *> VL);
Expr *Allocator, Expr *Alignment, SourceLocation ColonLoc,
OpenMPAllocateClauseModifier Modifier1, SourceLocation Modifier1Loc,
OpenMPAllocateClauseModifier Modifier2, SourceLocation Modifier2Loc,
SourceLocation EndLoc, ArrayRef<Expr *> VL);

/// Returns the allocator expression or nullptr, if no allocator is specified.
Expr *getAllocator() const { return Allocator; }

/// Returns the alignment expression or nullptr, if no alignment specified.
Expr *getAlignment() const { return Alignment; }

/// Return 'allocate' modifier.
OpenMPAllocateClauseModifier getAllocatorModifier() const {
return AllocatorModifier;
}

/// Get the first modifier of the clause.
OpenMPAllocateClauseModifier getFirstAllocateModifier() const {
return Modifiers[FIRST];
}

/// Get location of first modifier of the clause.
SourceLocation getFirstAllocateModifierLoc() const {
return ModifiersLoc[FIRST];
}

/// Get the second modifier of the clause.
OpenMPAllocateClauseModifier getSecondAllocateModifier() const {
return Modifiers[SECOND];
}

/// Get location of second modifier of the clause.
SourceLocation getSecondAllocateModifierLoc() const {
return ModifiersLoc[SECOND];
}

/// Returns the location of the ':' delimiter.
SourceLocation getColonLoc() const { return ColonLoc; }
/// Return the location of the modifier.
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,8 @@ def warn_omp_depend_in_ordered_deprecated : Warning<"'depend' clause for"
def warn_omp_invalid_attribute_for_ompx_attributes : Warning<"'ompx_attribute' clause only allows "
"'amdgpu_flat_work_group_size', 'amdgpu_waves_per_eu', and 'launch_bounds'; "
"%0 is ignored">, InGroup<OpenMPExtensions>;
def err_omp_duplicate_modifier : Error<"duplicate modifier '%0' in '%1' clause">;
def err_omp_expected_modifier : Error<"expected modifier in '%0' clause">;

// Pragma loop support.
def err_pragma_loop_missing_argument : Error<
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/OpenMPKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ OPENMP_NUMTASKS_MODIFIER(strict)

// Modifiers for 'allocate' clause.
OPENMP_ALLOCATE_MODIFIER(allocator)
OPENMP_ALLOCATE_MODIFIER(align)

// Modifiers for the 'doacross' clause.
OPENMP_DOACROSS_MODIFIER(source)
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/OpenMPKinds.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ enum OpenMPAllocateClauseModifier {
OMPC_ALLOCATE_unknown
};

/// Number of allowed allocate-modifiers.
static constexpr unsigned NumberOfOMPAllocateClauseModifiers =
OMPC_ALLOCATE_unknown;

/// Contains 'interop' data for 'append_args' and 'init' clauses.
class Expr;
struct OMPInteropInfo final {
Expand Down
20 changes: 15 additions & 5 deletions clang/include/clang/Sema/SemaOpenMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,12 @@ class SemaOpenMP : public SemaBase {
SourceLocation OmpAllMemoryLoc;
SourceLocation
StepModifierLoc; /// 'step' modifier location for linear clause
OpenMPAllocateClauseModifier AllocClauseModifier = OMPC_ALLOCATE_unknown;
SmallVector<OpenMPAllocateClauseModifier,
NumberOfOMPAllocateClauseModifiers>
AllocClauseModifiers;
SmallVector<SourceLocation, NumberOfOMPAllocateClauseModifiers>
AllocClauseModifiersLoc;
Expr *AllocateAlignment = nullptr;
};

OMPClause *ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
Expand All @@ -1166,10 +1171,15 @@ class SemaOpenMP : public SemaBase {
SourceLocation LParenLoc,
SourceLocation EndLoc);
/// Called on well-formed 'allocate' clause.
OMPClause *ActOnOpenMPAllocateClause(
Expr *Allocator, OpenMPAllocateClauseModifier ACModifier,
ArrayRef<Expr *> VarList, SourceLocation StartLoc,
SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc);
OMPClause *
ActOnOpenMPAllocateClause(Expr *Allocator, Expr *Alignment,
OpenMPAllocateClauseModifier FirstModifier,
SourceLocation FirstModifierLoc,
OpenMPAllocateClauseModifier SecondModifier,
SourceLocation SecondModifierLoc,
ArrayRef<Expr *> VarList, SourceLocation StartLoc,
SourceLocation ColonLoc, SourceLocation LParenLoc,
SourceLocation EndLoc);
/// Called on well-formed 'private' clause.
OMPClause *ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
SourceLocation StartLoc,
Expand Down
58 changes: 42 additions & 16 deletions clang/lib/AST/OpenMPClause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,19 +1019,18 @@ OMPPartialClause *OMPPartialClause::CreateEmpty(const ASTContext &C) {
return new (C) OMPPartialClause();
}

OMPAllocateClause *
OMPAllocateClause::Create(const ASTContext &C, SourceLocation StartLoc,
SourceLocation LParenLoc, Expr *Allocator,
SourceLocation ColonLoc,
OpenMPAllocateClauseModifier AllocatorModifier,
SourceLocation AllocatorModifierLoc,
SourceLocation EndLoc, ArrayRef<Expr *> VL) {
OMPAllocateClause *OMPAllocateClause::Create(
const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
Expr *Allocator, Expr *Alignment, SourceLocation ColonLoc,
OpenMPAllocateClauseModifier Modifier1, SourceLocation Modifier1Loc,
OpenMPAllocateClauseModifier Modifier2, SourceLocation Modifier2Loc,
SourceLocation EndLoc, ArrayRef<Expr *> VL) {

// Allocate space for private variables and initializer expressions.
void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
auto *Clause = new (Mem) OMPAllocateClause(
StartLoc, LParenLoc, Allocator, ColonLoc, AllocatorModifier,
AllocatorModifierLoc, EndLoc, VL.size());
StartLoc, LParenLoc, Allocator, Alignment, ColonLoc, Modifier1,
Modifier1Loc, Modifier2, Modifier2Loc, EndLoc, VL.size());

Clause->setVarRefs(VL);
return Clause;
Expand Down Expand Up @@ -2245,21 +2244,48 @@ void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
void OMPClausePrinter::VisitOMPAllocateClause(OMPAllocateClause *Node) {
if (Node->varlist_empty())
return;

Expr *FirstModifier = nullptr;
Expr *SecondModifier = nullptr;
auto FirstAllocMod = Node->getFirstAllocateModifier();
auto SecondAllocMod = Node->getSecondAllocateModifier();
bool FirstUnknown = FirstAllocMod == OMPC_ALLOCATE_unknown;
bool SecondUnknown = SecondAllocMod == OMPC_ALLOCATE_unknown;
if (FirstAllocMod == OMPC_ALLOCATE_allocator ||
(FirstAllocMod == OMPC_ALLOCATE_unknown && Node->getAllocator())) {
FirstModifier = Node->getAllocator();
SecondModifier = Node->getAlignment();
} else {
FirstModifier = Node->getAlignment();
SecondModifier = Node->getAllocator();
}

OS << "allocate";
OpenMPAllocateClauseModifier Modifier = Node->getAllocatorModifier();
if (Expr *Allocator = Node->getAllocator()) {
// If we have any explicit modifiers.
if (FirstModifier) {
OS << "(";
if (Modifier == OMPC_ALLOCATE_allocator) {
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier);
if (!FirstUnknown) {
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), FirstAllocMod);
OS << "(";
Allocator->printPretty(OS, nullptr, Policy, 0);
}
FirstModifier->printPretty(OS, nullptr, Policy, 0);
if (!FirstUnknown)
OS << ")";
} else {
Allocator->printPretty(OS, nullptr, Policy, 0);
if (SecondModifier) {
OS << ", ";
if (!SecondUnknown) {
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
SecondAllocMod);
OS << "(";
}
SecondModifier->printPretty(OS, nullptr, Policy, 0);
if (!SecondUnknown)
OS << ")";
}
OS << ":";
VisitOMPClauseList(Node, ' ');
} else {
// No modifiers. Just print the variable list.
VisitOMPClauseList(Node, '(');
}
OS << ")";
Expand Down
Loading
Loading