Skip to content

Commit ad38e24

Browse files
authored
[clang][OpenMP] Add 'align' modifier for 'allocate' clause (#121814)
The 'align' modifier is now accepted in the 'allocate' clause. Added LIT tests covering codegen, PCH, template handling, and serialization for 'align' modifier. Added support for align-modifier to release notes. Testing - New allocate modifier LIT tests. - OpenMP LIT tests. - check-all
1 parent 7ed451a commit ad38e24

18 files changed

+962
-451
lines changed

clang/docs/OpenMPSupport.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ implementation.
286286
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
287287
| memory management | 'allocator' modifier for allocate clause | :good:`done` | https://github.com/llvm/llvm-project/pull/114883 |
288288
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
289+
| memory management | 'align' modifier for allocate clause | :good:`done` | https://github.com/llvm/llvm-project/pull/121814 |
290+
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
289291
| memory management | new memory management routines | :none:`unclaimed` | |
290292
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
291293
| memory management | changes to omp_alloctrait_key enum | :none:`unclaimed` | |

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,7 @@ OpenMP Support
13701370
always build support for AMDGPU and NVPTX targets.
13711371
- Added support for combined masked constructs 'omp parallel masked taskloop',
13721372
'omp parallel masked taskloop simd','omp masked taskloop' and 'omp masked taskloop simd' directive.
1373+
- Added support for align-modifier in 'allocate' clause.
13731374

13741375
Improvements
13751376
^^^^^^^^^^^^

clang/include/clang/AST/OpenMPClause.h

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,51 @@ class OMPAllocateClause final
498498
/// Allocator specified in the clause, or 'nullptr' if the default one is
499499
/// used.
500500
Expr *Allocator = nullptr;
501+
/// Alignment specified in the clause, or 'nullptr' if the default one is
502+
/// used.
503+
Expr *Alignment = nullptr;
501504
/// Position of the ':' delimiter in the clause;
502505
SourceLocation ColonLoc;
503506
/// Modifier of 'allocate' clause.
504507
OpenMPAllocateClauseModifier AllocatorModifier = OMPC_ALLOCATE_unknown;
505508
/// Location of allocator modifier if any.
506509
SourceLocation AllocatorModifierLoc;
507510

511+
// ----------------------------------------------------------------------------
512+
513+
/// Modifiers for 'allocate' clause.
514+
enum { FIRST, SECOND, NUM_MODIFIERS };
515+
OpenMPAllocateClauseModifier Modifiers[NUM_MODIFIERS];
516+
517+
/// Locations of modifiers.
518+
SourceLocation ModifiersLoc[NUM_MODIFIERS];
519+
520+
/// Set the first allocate modifier.
521+
///
522+
/// \param M Allocate modifier.
523+
void setFirstAllocateModifier(OpenMPAllocateClauseModifier M) {
524+
Modifiers[FIRST] = M;
525+
}
526+
527+
/// Set the second allocate modifier.
528+
///
529+
/// \param M Allocate modifier.
530+
void setSecondAllocateModifier(OpenMPAllocateClauseModifier M) {
531+
Modifiers[SECOND] = M;
532+
}
533+
534+
/// Set location of the first allocate modifier.
535+
void setFirstAllocateModifierLoc(SourceLocation Loc) {
536+
ModifiersLoc[FIRST] = Loc;
537+
}
538+
539+
/// Set location of the second allocate modifier.
540+
void setSecondAllocateModifierLoc(SourceLocation Loc) {
541+
ModifiersLoc[SECOND] = Loc;
542+
}
543+
544+
// ----------------------------------------------------------------------------
545+
508546
/// Build clause with number of variables \a N.
509547
///
510548
/// \param StartLoc Starting location of the clause.
@@ -514,23 +552,31 @@ class OMPAllocateClause final
514552
/// \param EndLoc Ending location of the clause.
515553
/// \param N Number of the variables in the clause.
516554
OMPAllocateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
517-
Expr *Allocator, SourceLocation ColonLoc,
518-
OpenMPAllocateClauseModifier AllocatorModifier,
519-
SourceLocation AllocatorModifierLoc, SourceLocation EndLoc,
555+
Expr *Allocator, Expr *Alignment, SourceLocation ColonLoc,
556+
OpenMPAllocateClauseModifier Modifier1,
557+
SourceLocation Modifier1Loc,
558+
OpenMPAllocateClauseModifier Modifier2,
559+
SourceLocation Modifier2Loc, SourceLocation EndLoc,
520560
unsigned N)
521561
: OMPVarListClause<OMPAllocateClause>(llvm::omp::OMPC_allocate, StartLoc,
522562
LParenLoc, EndLoc, N),
523-
Allocator(Allocator), ColonLoc(ColonLoc),
524-
AllocatorModifier(AllocatorModifier),
525-
AllocatorModifierLoc(AllocatorModifierLoc) {}
563+
Allocator(Allocator), Alignment(Alignment), ColonLoc(ColonLoc) {
564+
Modifiers[FIRST] = Modifier1;
565+
Modifiers[SECOND] = Modifier2;
566+
ModifiersLoc[FIRST] = Modifier1Loc;
567+
ModifiersLoc[SECOND] = Modifier2Loc;
568+
}
526569

527570
/// Build an empty clause.
528571
///
529572
/// \param N Number of variables.
530573
explicit OMPAllocateClause(unsigned N)
531574
: OMPVarListClause<OMPAllocateClause>(llvm::omp::OMPC_allocate,
532575
SourceLocation(), SourceLocation(),
533-
SourceLocation(), N) {}
576+
SourceLocation(), N) {
577+
Modifiers[FIRST] = OMPC_ALLOCATE_unknown;
578+
Modifiers[SECOND] = OMPC_ALLOCATE_unknown;
579+
}
534580

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

543590
public:
544591
/// Creates clause with a list of variables \a VL.
@@ -554,19 +601,42 @@ class OMPAllocateClause final
554601
/// \param VL List of references to the variables.
555602
static OMPAllocateClause *
556603
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
557-
Expr *Allocator, SourceLocation ColonLoc,
558-
OpenMPAllocateClauseModifier AllocatorModifier,
559-
SourceLocation AllocatorModifierLoc, SourceLocation EndLoc,
560-
ArrayRef<Expr *> VL);
604+
Expr *Allocator, Expr *Alignment, SourceLocation ColonLoc,
605+
OpenMPAllocateClauseModifier Modifier1, SourceLocation Modifier1Loc,
606+
OpenMPAllocateClauseModifier Modifier2, SourceLocation Modifier2Loc,
607+
SourceLocation EndLoc, ArrayRef<Expr *> VL);
561608

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

612+
/// Returns the alignment expression or nullptr, if no alignment specified.
613+
Expr *getAlignment() const { return Alignment; }
614+
565615
/// Return 'allocate' modifier.
566616
OpenMPAllocateClauseModifier getAllocatorModifier() const {
567617
return AllocatorModifier;
568618
}
569619

620+
/// Get the first modifier of the clause.
621+
OpenMPAllocateClauseModifier getFirstAllocateModifier() const {
622+
return Modifiers[FIRST];
623+
}
624+
625+
/// Get location of first modifier of the clause.
626+
SourceLocation getFirstAllocateModifierLoc() const {
627+
return ModifiersLoc[FIRST];
628+
}
629+
630+
/// Get the second modifier of the clause.
631+
OpenMPAllocateClauseModifier getSecondAllocateModifier() const {
632+
return Modifiers[SECOND];
633+
}
634+
635+
/// Get location of second modifier of the clause.
636+
SourceLocation getSecondAllocateModifierLoc() const {
637+
return ModifiersLoc[SECOND];
638+
}
639+
570640
/// Returns the location of the ':' delimiter.
571641
SourceLocation getColonLoc() const { return ColonLoc; }
572642
/// Return the location of the modifier.

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,8 @@ def warn_omp_depend_in_ordered_deprecated : Warning<"'depend' clause for"
16581658
def warn_omp_invalid_attribute_for_ompx_attributes : Warning<"'ompx_attribute' clause only allows "
16591659
"'amdgpu_flat_work_group_size', 'amdgpu_waves_per_eu', and 'launch_bounds'; "
16601660
"%0 is ignored">, InGroup<OpenMPExtensions>;
1661+
def err_omp_duplicate_modifier : Error<"duplicate modifier '%0' in '%1' clause">;
1662+
def err_omp_expected_modifier : Error<"expected modifier in '%0' clause">;
16611663

16621664
// Pragma loop support.
16631665
def err_pragma_loop_missing_argument : Error<

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ OPENMP_NUMTASKS_MODIFIER(strict)
219219

220220
// Modifiers for 'allocate' clause.
221221
OPENMP_ALLOCATE_MODIFIER(allocator)
222+
OPENMP_ALLOCATE_MODIFIER(align)
222223

223224
// Modifiers for the 'doacross' clause.
224225
OPENMP_DOACROSS_MODIFIER(source)

clang/include/clang/Basic/OpenMPKinds.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ enum OpenMPAllocateClauseModifier {
230230
OMPC_ALLOCATE_unknown
231231
};
232232

233+
/// Number of allowed allocate-modifiers.
234+
static constexpr unsigned NumberOfOMPAllocateClauseModifiers =
235+
OMPC_ALLOCATE_unknown;
236+
233237
/// Contains 'interop' data for 'append_args' and 'init' clauses.
234238
class Expr;
235239
struct OMPInteropInfo final {

clang/include/clang/Sema/SemaOpenMP.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,12 @@ class SemaOpenMP : public SemaBase {
11481148
SourceLocation OmpAllMemoryLoc;
11491149
SourceLocation
11501150
StepModifierLoc; /// 'step' modifier location for linear clause
1151-
OpenMPAllocateClauseModifier AllocClauseModifier = OMPC_ALLOCATE_unknown;
1151+
SmallVector<OpenMPAllocateClauseModifier,
1152+
NumberOfOMPAllocateClauseModifiers>
1153+
AllocClauseModifiers;
1154+
SmallVector<SourceLocation, NumberOfOMPAllocateClauseModifiers>
1155+
AllocClauseModifiersLoc;
1156+
Expr *AllocateAlignment = nullptr;
11521157
};
11531158

11541159
OMPClause *ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
@@ -1166,10 +1171,15 @@ class SemaOpenMP : public SemaBase {
11661171
SourceLocation LParenLoc,
11671172
SourceLocation EndLoc);
11681173
/// Called on well-formed 'allocate' clause.
1169-
OMPClause *ActOnOpenMPAllocateClause(
1170-
Expr *Allocator, OpenMPAllocateClauseModifier ACModifier,
1171-
ArrayRef<Expr *> VarList, SourceLocation StartLoc,
1172-
SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc);
1174+
OMPClause *
1175+
ActOnOpenMPAllocateClause(Expr *Allocator, Expr *Alignment,
1176+
OpenMPAllocateClauseModifier FirstModifier,
1177+
SourceLocation FirstModifierLoc,
1178+
OpenMPAllocateClauseModifier SecondModifier,
1179+
SourceLocation SecondModifierLoc,
1180+
ArrayRef<Expr *> VarList, SourceLocation StartLoc,
1181+
SourceLocation ColonLoc, SourceLocation LParenLoc,
1182+
SourceLocation EndLoc);
11731183
/// Called on well-formed 'private' clause.
11741184
OMPClause *ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
11751185
SourceLocation StartLoc,

clang/lib/AST/OpenMPClause.cpp

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,19 +1019,18 @@ OMPPartialClause *OMPPartialClause::CreateEmpty(const ASTContext &C) {
10191019
return new (C) OMPPartialClause();
10201020
}
10211021

1022-
OMPAllocateClause *
1023-
OMPAllocateClause::Create(const ASTContext &C, SourceLocation StartLoc,
1024-
SourceLocation LParenLoc, Expr *Allocator,
1025-
SourceLocation ColonLoc,
1026-
OpenMPAllocateClauseModifier AllocatorModifier,
1027-
SourceLocation AllocatorModifierLoc,
1028-
SourceLocation EndLoc, ArrayRef<Expr *> VL) {
1022+
OMPAllocateClause *OMPAllocateClause::Create(
1023+
const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1024+
Expr *Allocator, Expr *Alignment, SourceLocation ColonLoc,
1025+
OpenMPAllocateClauseModifier Modifier1, SourceLocation Modifier1Loc,
1026+
OpenMPAllocateClauseModifier Modifier2, SourceLocation Modifier2Loc,
1027+
SourceLocation EndLoc, ArrayRef<Expr *> VL) {
10291028

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

10361035
Clause->setVarRefs(VL);
10371036
return Clause;
@@ -2245,21 +2244,48 @@ void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
22452244
void OMPClausePrinter::VisitOMPAllocateClause(OMPAllocateClause *Node) {
22462245
if (Node->varlist_empty())
22472246
return;
2247+
2248+
Expr *FirstModifier = nullptr;
2249+
Expr *SecondModifier = nullptr;
2250+
auto FirstAllocMod = Node->getFirstAllocateModifier();
2251+
auto SecondAllocMod = Node->getSecondAllocateModifier();
2252+
bool FirstUnknown = FirstAllocMod == OMPC_ALLOCATE_unknown;
2253+
bool SecondUnknown = SecondAllocMod == OMPC_ALLOCATE_unknown;
2254+
if (FirstAllocMod == OMPC_ALLOCATE_allocator ||
2255+
(FirstAllocMod == OMPC_ALLOCATE_unknown && Node->getAllocator())) {
2256+
FirstModifier = Node->getAllocator();
2257+
SecondModifier = Node->getAlignment();
2258+
} else {
2259+
FirstModifier = Node->getAlignment();
2260+
SecondModifier = Node->getAllocator();
2261+
}
2262+
22482263
OS << "allocate";
2249-
OpenMPAllocateClauseModifier Modifier = Node->getAllocatorModifier();
2250-
if (Expr *Allocator = Node->getAllocator()) {
2264+
// If we have any explicit modifiers.
2265+
if (FirstModifier) {
22512266
OS << "(";
2252-
if (Modifier == OMPC_ALLOCATE_allocator) {
2253-
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier);
2267+
if (!FirstUnknown) {
2268+
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), FirstAllocMod);
22542269
OS << "(";
2255-
Allocator->printPretty(OS, nullptr, Policy, 0);
2270+
}
2271+
FirstModifier->printPretty(OS, nullptr, Policy, 0);
2272+
if (!FirstUnknown)
22562273
OS << ")";
2257-
} else {
2258-
Allocator->printPretty(OS, nullptr, Policy, 0);
2274+
if (SecondModifier) {
2275+
OS << ", ";
2276+
if (!SecondUnknown) {
2277+
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
2278+
SecondAllocMod);
2279+
OS << "(";
2280+
}
2281+
SecondModifier->printPretty(OS, nullptr, Policy, 0);
2282+
if (!SecondUnknown)
2283+
OS << ")";
22592284
}
22602285
OS << ":";
22612286
VisitOMPClauseList(Node, ' ');
22622287
} else {
2288+
// No modifiers. Just print the variable list.
22632289
VisitOMPClauseList(Node, '(');
22642290
}
22652291
OS << ")";

0 commit comments

Comments
 (0)