Skip to content

Commit 59e97a7

Browse files
ShashwathiNavadaShashwathi N
authored andcommitted
Adding support for iterator in motion clauses. (llvm#159112)
As described in section 2.14.6 of openmp spec, the patch implements support for iterator in motion clauses. --------- Co-authored-by: Shashwathi N <[email protected]>
1 parent 61821f2 commit 59e97a7

File tree

14 files changed

+238
-71
lines changed

14 files changed

+238
-71
lines changed

clang/docs/OpenMPSupport.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ implementation.
266266
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
267267
| device | has_device_addr clause on target construct | :none:`unclaimed` | |
268268
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
269-
| device | iterators in map clause or motion clauses | :none:`unclaimed` | |
269+
| device | iterators in map clause or motion clauses | :none:`done` | https://github.com/llvm/llvm-project/pull/159112 |
270270
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+
271271
| device | indirect clause on declare target directive | :part:`In Progress` | |
272272
+------------------------------+--------------------------------------------------------------+--------------------------+-----------------------------------------------------------------------+

clang/include/clang/AST/OpenMPClause.h

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7582,7 +7582,8 @@ class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
75827582

75837583
/// Motion-modifiers for the 'to' clause.
75847584
OpenMPMotionModifierKind MotionModifiers[NumberOfOMPMotionModifiers] = {
7585-
OMPC_MOTION_MODIFIER_unknown, OMPC_MOTION_MODIFIER_unknown};
7585+
OMPC_MOTION_MODIFIER_unknown, OMPC_MOTION_MODIFIER_unknown,
7586+
OMPC_MOTION_MODIFIER_unknown};
75867587

75877588
/// Location of motion-modifiers for the 'to' clause.
75887589
SourceLocation MotionModifiersLoc[NumberOfOMPMotionModifiers];
@@ -7654,6 +7655,9 @@ class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
76547655
MotionModifiersLoc[I] = TLoc;
76557656
}
76567657

7658+
void setIteratorModifier(Expr *IteratorModifier) {
7659+
getTrailingObjects<Expr *>()[2 * varlist_size()] = IteratorModifier;
7660+
}
76577661
/// Set colon location.
76587662
void setColonLoc(SourceLocation Loc) { ColonLoc = Loc; }
76597663

@@ -7662,7 +7666,7 @@ class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
76627666
size_t numTrailingObjects(OverloadToken<Expr *>) const {
76637667
// There are varlist_size() of expressions, and varlist_size() of
76647668
// user-defined mappers.
7665-
return 2 * varlist_size();
7669+
return 2 * varlist_size() + 1;
76667670
}
76677671
size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
76687672
return getUniqueDeclarationsNum();
@@ -7688,15 +7692,14 @@ class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
76887692
/// \param UDMQualifierLoc C++ nested name specifier for the associated
76897693
/// user-defined mapper.
76907694
/// \param MapperId The identifier of associated user-defined mapper.
7691-
static OMPToClause *Create(const ASTContext &C, const OMPVarListLocTy &Locs,
7692-
ArrayRef<Expr *> Vars,
7693-
ArrayRef<ValueDecl *> Declarations,
7694-
MappableExprComponentListsRef ComponentLists,
7695-
ArrayRef<Expr *> UDMapperRefs,
7696-
ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
7697-
ArrayRef<SourceLocation> MotionModifiersLoc,
7698-
NestedNameSpecifierLoc UDMQualifierLoc,
7699-
DeclarationNameInfo MapperId);
7695+
static OMPToClause *
7696+
Create(const ASTContext &C, const OMPVarListLocTy &Locs,
7697+
ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
7698+
MappableExprComponentListsRef ComponentLists,
7699+
ArrayRef<Expr *> UDMapperRefs, Expr *IteratorModifier,
7700+
ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
7701+
ArrayRef<SourceLocation> MotionModifiersLoc,
7702+
NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId);
77007703

77017704
/// Creates an empty clause with the place for \a NumVars variables.
77027705
///
@@ -7717,7 +7720,9 @@ class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
77177720
"Requested modifier exceeds the total number of modifiers.");
77187721
return MotionModifiers[Cnt];
77197722
}
7720-
7723+
Expr *getIteratorModifier() const {
7724+
return getTrailingObjects<Expr *>()[2 * varlist_size()];
7725+
}
77217726
/// Fetches the motion-modifier location at 'Cnt' index of array of modifiers'
77227727
/// locations.
77237728
///
@@ -7782,7 +7787,8 @@ class OMPFromClause final
77827787

77837788
/// Motion-modifiers for the 'from' clause.
77847789
OpenMPMotionModifierKind MotionModifiers[NumberOfOMPMotionModifiers] = {
7785-
OMPC_MOTION_MODIFIER_unknown, OMPC_MOTION_MODIFIER_unknown};
7790+
OMPC_MOTION_MODIFIER_unknown, OMPC_MOTION_MODIFIER_unknown,
7791+
OMPC_MOTION_MODIFIER_unknown};
77867792

77877793
/// Location of motion-modifiers for the 'from' clause.
77887794
SourceLocation MotionModifiersLoc[NumberOfOMPMotionModifiers];
@@ -7843,7 +7849,9 @@ class OMPFromClause final
78437849
"Unexpected index to store motion modifier, exceeds array size.");
78447850
MotionModifiers[I] = T;
78457851
}
7846-
7852+
void setIteratorModifier(Expr *IteratorModifier) {
7853+
getTrailingObjects<Expr *>()[2 * varlist_size()] = IteratorModifier;
7854+
}
78477855
/// Set location for the motion-modifier.
78487856
///
78497857
/// \param I index for motion-modifier location.
@@ -7862,7 +7870,7 @@ class OMPFromClause final
78627870
size_t numTrailingObjects(OverloadToken<Expr *>) const {
78637871
// There are varlist_size() of expressions, and varlist_size() of
78647872
// user-defined mappers.
7865-
return 2 * varlist_size();
7873+
return 2 * varlist_size() + 1;
78667874
}
78677875
size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
78687876
return getUniqueDeclarationsNum();
@@ -7892,7 +7900,7 @@ class OMPFromClause final
78927900
Create(const ASTContext &C, const OMPVarListLocTy &Locs,
78937901
ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
78947902
MappableExprComponentListsRef ComponentLists,
7895-
ArrayRef<Expr *> UDMapperRefs,
7903+
ArrayRef<Expr *> UDMapperRefs, Expr *IteratorExpr,
78967904
ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
78977905
ArrayRef<SourceLocation> MotionModifiersLoc,
78987906
NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId);
@@ -7916,7 +7924,9 @@ class OMPFromClause final
79167924
"Requested modifier exceeds the total number of modifiers.");
79177925
return MotionModifiers[Cnt];
79187926
}
7919-
7927+
Expr *getIteratorModifier() const {
7928+
return getTrailingObjects<Expr *>()[2 * varlist_size()];
7929+
}
79207930
/// Fetches the motion-modifier location at 'Cnt' index of array of modifiers'
79217931
/// locations.
79227932
///

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ OPENMP_MAP_MODIFIER_KIND(ompx_hold)
207207

208208
// Modifiers for 'to' or 'from' clause.
209209
OPENMP_MOTION_MODIFIER_KIND(mapper)
210+
OPENMP_MOTION_MODIFIER_KIND(iterator)
210211
OPENMP_MOTION_MODIFIER_KIND(present)
211212

212213
// Static attributes for 'dist_schedule' clause.

clang/include/clang/Sema/SemaOpenMP.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,15 +1351,15 @@ class SemaOpenMP : public SemaBase {
13511351
OMPClause *
13521352
ActOnOpenMPToClause(ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
13531353
ArrayRef<SourceLocation> MotionModifiersLoc,
1354-
CXXScopeSpec &MapperIdScopeSpec,
1354+
Expr *IteratorModifier, CXXScopeSpec &MapperIdScopeSpec,
13551355
DeclarationNameInfo &MapperId, SourceLocation ColonLoc,
13561356
ArrayRef<Expr *> VarList, const OMPVarListLocTy &Locs,
13571357
ArrayRef<Expr *> UnresolvedMappers = {});
13581358
/// Called on well-formed 'from' clause.
13591359
OMPClause *
13601360
ActOnOpenMPFromClause(ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
13611361
ArrayRef<SourceLocation> MotionModifiersLoc,
1362-
CXXScopeSpec &MapperIdScopeSpec,
1362+
Expr *IteratorModifier, CXXScopeSpec &MapperIdScopeSpec,
13631363
DeclarationNameInfo &MapperId, SourceLocation ColonLoc,
13641364
ArrayRef<Expr *> VarList, const OMPVarListLocTy &Locs,
13651365
ArrayRef<Expr *> UnresolvedMappers = {});

clang/lib/AST/OpenMPClause.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ OMPToClause *OMPToClause::Create(
13211321
const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
13221322
ArrayRef<ValueDecl *> Declarations,
13231323
MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1324-
ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
1324+
Expr *IteratorModifier, ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
13251325
ArrayRef<SourceLocation> MotionModifiersLoc,
13261326
NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
13271327
OMPMappableExprListSizeTy Sizes;
@@ -1343,7 +1343,7 @@ OMPToClause *OMPToClause::Create(
13431343
void *Mem = C.Allocate(
13441344
totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
13451345
OMPClauseMappableExprCommon::MappableComponent>(
1346-
2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1346+
2 * Sizes.NumVars + 1, Sizes.NumUniqueDeclarations,
13471347
Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
13481348
Sizes.NumComponents));
13491349

@@ -1353,6 +1353,7 @@ OMPToClause *OMPToClause::Create(
13531353
Clause->setVarRefs(Vars);
13541354
Clause->setUDMapperRefs(UDMapperRefs);
13551355
Clause->setClauseInfo(Declarations, ComponentLists);
1356+
Clause->setIteratorModifier(IteratorModifier);
13561357
return Clause;
13571358
}
13581359

@@ -1361,17 +1362,19 @@ OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C,
13611362
void *Mem = C.Allocate(
13621363
totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
13631364
OMPClauseMappableExprCommon::MappableComponent>(
1364-
2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1365+
2 * Sizes.NumVars + 1, Sizes.NumUniqueDeclarations,
13651366
Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
13661367
Sizes.NumComponents));
1367-
return new (Mem) OMPToClause(Sizes);
1368+
OMPToClause *Clause = new (Mem) OMPToClause(Sizes);
1369+
Clause->setIteratorModifier(nullptr);
1370+
return Clause;
13681371
}
13691372

13701373
OMPFromClause *OMPFromClause::Create(
13711374
const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
13721375
ArrayRef<ValueDecl *> Declarations,
13731376
MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1374-
ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
1377+
Expr *IteratorModifier, ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
13751378
ArrayRef<SourceLocation> MotionModifiersLoc,
13761379
NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
13771380
OMPMappableExprListSizeTy Sizes;
@@ -1393,7 +1396,7 @@ OMPFromClause *OMPFromClause::Create(
13931396
void *Mem = C.Allocate(
13941397
totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
13951398
OMPClauseMappableExprCommon::MappableComponent>(
1396-
2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1399+
2 * Sizes.NumVars + 1, Sizes.NumUniqueDeclarations,
13971400
Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
13981401
Sizes.NumComponents));
13991402

@@ -1404,6 +1407,7 @@ OMPFromClause *OMPFromClause::Create(
14041407
Clause->setVarRefs(Vars);
14051408
Clause->setUDMapperRefs(UDMapperRefs);
14061409
Clause->setClauseInfo(Declarations, ComponentLists);
1410+
Clause->setIteratorModifier(IteratorModifier);
14071411
return Clause;
14081412
}
14091413

@@ -1413,10 +1417,12 @@ OMPFromClause::CreateEmpty(const ASTContext &C,
14131417
void *Mem = C.Allocate(
14141418
totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
14151419
OMPClauseMappableExprCommon::MappableComponent>(
1416-
2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1420+
2 * Sizes.NumVars + 1, Sizes.NumUniqueDeclarations,
14171421
Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
14181422
Sizes.NumComponents));
1419-
return new (Mem) OMPFromClause(Sizes);
1423+
OMPFromClause *Clause = new (Mem) OMPFromClause(Sizes);
1424+
Clause->setIteratorModifier(nullptr);
1425+
return Clause;
14201426
}
14211427

14221428
void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
@@ -2694,12 +2700,16 @@ template <typename T> void OMPClausePrinter::VisitOMPMotionClause(T *Node) {
26942700
OS << '(';
26952701
for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
26962702
if (Node->getMotionModifier(I) != OMPC_MOTION_MODIFIER_unknown) {
2697-
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
2698-
Node->getMotionModifier(I));
2699-
if (Node->getMotionModifier(I) == OMPC_MOTION_MODIFIER_mapper)
2700-
PrintMapper(OS, Node, Policy);
2701-
if (I < ModifierCount - 1)
2702-
OS << ", ";
2703+
if (Node->getMotionModifier(I) == OMPC_MOTION_MODIFIER_iterator) {
2704+
PrintIterator(OS, Node, Policy);
2705+
} else {
2706+
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
2707+
Node->getMotionModifier(I));
2708+
if (Node->getMotionModifier(I) == OMPC_MOTION_MODIFIER_mapper)
2709+
PrintMapper(OS, Node, Policy);
2710+
if (I < ModifierCount - 1)
2711+
OS << ", ";
2712+
}
27032713
}
27042714
}
27052715
OS << ':';

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8634,6 +8634,15 @@ class MappableExprsHandler {
86348634
if (llvm::is_contained(C->getMotionModifiers(),
86358635
OMPC_MOTION_MODIFIER_present))
86368636
Kind = Present;
8637+
if (llvm::is_contained(C->getMotionModifiers(),
8638+
OMPC_MOTION_MODIFIER_iterator)) {
8639+
if (auto *IteratorExpr = dyn_cast<OMPIteratorExpr>(
8640+
C->getIteratorModifier()->IgnoreParenImpCasts())) {
8641+
const auto *VD = cast<VarDecl>(IteratorExpr->getIteratorDecl(0));
8642+
CGF.EmitVarDecl(*VD);
8643+
}
8644+
}
8645+
86378646
const auto *EI = C->getVarRefs().begin();
86388647
for (const auto L : C->component_lists()) {
86398648
InfoGen(std::get<0>(L), Kind, std::get<1>(L), OMPC_MAP_to, {},
@@ -8650,6 +8659,15 @@ class MappableExprsHandler {
86508659
if (llvm::is_contained(C->getMotionModifiers(),
86518660
OMPC_MOTION_MODIFIER_present))
86528661
Kind = Present;
8662+
if (llvm::is_contained(C->getMotionModifiers(),
8663+
OMPC_MOTION_MODIFIER_iterator)) {
8664+
if (auto *IteratorExpr = dyn_cast<OMPIteratorExpr>(
8665+
C->getIteratorModifier()->IgnoreParenImpCasts())) {
8666+
const auto *VD = cast<VarDecl>(IteratorExpr->getIteratorDecl(0));
8667+
CGF.EmitVarDecl(*VD);
8668+
}
8669+
}
8670+
86538671
const auto *EI = C->getVarRefs().begin();
86548672
for (const auto L : C->component_lists()) {
86558673
InfoGen(std::get<0>(L), Kind, std::get<1>(L), OMPC_MAP_from, {},

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4925,19 +4925,28 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
49254925
break;
49264926
Data.MotionModifiers.push_back(Modifier);
49274927
Data.MotionModifiersLoc.push_back(Tok.getLocation());
4928-
ConsumeToken();
4929-
if (Modifier == OMPC_MOTION_MODIFIER_mapper) {
4930-
IsInvalidMapperModifier = parseMapperModifier(Data);
4931-
if (IsInvalidMapperModifier)
4928+
if (PP.getSpelling(Tok) == "iterator" && getLangOpts().OpenMP >= 51) {
4929+
ExprResult Tail;
4930+
Tail = ParseOpenMPIteratorsExpr();
4931+
Tail = Actions.ActOnFinishFullExpr(Tail.get(), T.getOpenLocation(),
4932+
/*DiscardedValue=*/false);
4933+
if (Tail.isUsable())
4934+
Data.IteratorExpr = Tail.get();
4935+
} else {
4936+
ConsumeToken();
4937+
if (Modifier == OMPC_MOTION_MODIFIER_mapper) {
4938+
IsInvalidMapperModifier = parseMapperModifier(Data);
4939+
if (IsInvalidMapperModifier)
4940+
break;
4941+
}
4942+
// OpenMP < 5.1 doesn't permit a ',' or additional modifiers.
4943+
if (getLangOpts().OpenMP < 51)
49324944
break;
4945+
// OpenMP 5.1 accepts an optional ',' even if the next character is ':'.
4946+
// TODO: Is that intentional?
4947+
if (Tok.is(tok::comma))
4948+
ConsumeToken();
49334949
}
4934-
// OpenMP < 5.1 doesn't permit a ',' or additional modifiers.
4935-
if (getLangOpts().OpenMP < 51)
4936-
break;
4937-
// OpenMP 5.1 accepts an optional ',' even if the next character is ':'.
4938-
// TODO: Is that intentional?
4939-
if (Tok.is(tok::comma))
4940-
ConsumeToken();
49414950
}
49424951
if (!Data.MotionModifiers.empty() && Tok.isNot(tok::colon)) {
49434952
if (!IsInvalidMapperModifier) {

0 commit comments

Comments
 (0)