Skip to content

Commit 56c1660

Browse files
mdfazlaymikerice1969
authored andcommitted
[OpenMP] Initial parsing/sema for 'strict' modifier with 'num_tasks' clause
This patch gives basic parsing and semantic analysis support for 'strict' modifier with 'num_tasks' clause of 'taskloop' construct introduced in OpenMP 5.1 (section 2.12.2) Differential Revision: https://reviews.llvm.org/D138328
1 parent 01a8c49 commit 56c1660

File tree

14 files changed

+344
-29
lines changed

14 files changed

+344
-29
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6486,26 +6486,43 @@ class OMPNumTasksClause : public OMPClause, public OMPClauseWithPreInit {
64866486
/// Location of '('.
64876487
SourceLocation LParenLoc;
64886488

6489+
/// Modifiers for 'num_tasks' clause.
6490+
OpenMPNumTasksClauseModifier Modifier = OMPC_NUMTASKS_unknown;
6491+
6492+
/// Location of the modifier.
6493+
SourceLocation ModifierLoc;
6494+
64896495
/// Safe iteration space distance.
64906496
Stmt *NumTasks = nullptr;
64916497

64926498
/// Set safelen.
64936499
void setNumTasks(Expr *Size) { NumTasks = Size; }
64946500

6501+
/// Sets modifier.
6502+
void setModifier(OpenMPNumTasksClauseModifier M) { Modifier = M; }
6503+
6504+
/// Sets modifier location.
6505+
void setModifierLoc(SourceLocation Loc) { ModifierLoc = Loc; }
6506+
64956507
public:
64966508
/// Build 'num_tasks' clause.
64976509
///
6510+
/// \param Modifier Clause modifier.
64986511
/// \param Size Expression associated with this clause.
64996512
/// \param HelperSize Helper grainsize for the construct.
65006513
/// \param CaptureRegion Innermost OpenMP region where expressions in this
65016514
/// clause must be captured.
65026515
/// \param StartLoc Starting location of the clause.
65036516
/// \param EndLoc Ending location of the clause.
6504-
OMPNumTasksClause(Expr *Size, Stmt *HelperSize,
6505-
OpenMPDirectiveKind CaptureRegion, SourceLocation StartLoc,
6506-
SourceLocation LParenLoc, SourceLocation EndLoc)
6517+
/// \param ModifierLoc Modifier location.
6518+
/// \param LParenLoc Location of '('.
6519+
OMPNumTasksClause(OpenMPNumTasksClauseModifier Modifier, Expr *Size,
6520+
Stmt *HelperSize, OpenMPDirectiveKind CaptureRegion,
6521+
SourceLocation StartLoc, SourceLocation LParenLoc,
6522+
SourceLocation ModifierLoc, SourceLocation EndLoc)
65076523
: OMPClause(llvm::omp::OMPC_num_tasks, StartLoc, EndLoc),
6508-
OMPClauseWithPreInit(this), LParenLoc(LParenLoc), NumTasks(Size) {
6524+
OMPClauseWithPreInit(this), LParenLoc(LParenLoc), Modifier(Modifier),
6525+
ModifierLoc(ModifierLoc), NumTasks(Size) {
65096526
setPreInitStmt(HelperSize, CaptureRegion);
65106527
}
65116528

@@ -6524,6 +6541,12 @@ class OMPNumTasksClause : public OMPClause, public OMPClauseWithPreInit {
65246541
/// Return safe iteration space distance.
65256542
Expr *getNumTasks() const { return cast_or_null<Expr>(NumTasks); }
65266543

6544+
/// Gets modifier.
6545+
OpenMPNumTasksClauseModifier getModifier() const { return Modifier; }
6546+
6547+
/// Gets modifier location.
6548+
SourceLocation getModifierLoc() const { return ModifierLoc; }
6549+
65276550
child_range children() { return child_range(&NumTasks, &NumTasks + 1); }
65286551

65296552
const_child_range children() const {

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
#ifndef OPENMP_GRAINSIZE_MODIFIER
7575
#define OPENMP_GRAINSIZE_MODIFIER(Name)
7676
#endif
77+
#ifndef OPENMP_NUMTASKS_MODIFIER
78+
#define OPENMP_NUMTASKS_MODIFIER(Name)
79+
#endif
7780

7881
// Static attributes for 'schedule' clause.
7982
OPENMP_SCHEDULE_KIND(static)
@@ -187,6 +190,10 @@ OPENMP_BIND_KIND(thread)
187190
// Modifiers for the 'grainsize' clause.
188191
OPENMP_GRAINSIZE_MODIFIER(strict)
189192

193+
// Modifiers for the 'num_tasks' clause.
194+
OPENMP_NUMTASKS_MODIFIER(strict)
195+
196+
#undef OPENMP_NUMTASKS_MODIFIER
190197
#undef OPENMP_GRAINSIZE_MODIFIER
191198
#undef OPENMP_BIND_KIND
192199
#undef OPENMP_ADJUST_ARGS_KIND

clang/include/clang/Basic/OpenMPKinds.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ enum OpenMPGrainsizeClauseModifier {
201201
OMPC_GRAINSIZE_unknown
202202
};
203203

204+
enum OpenMPNumTasksClauseModifier {
205+
#define OPENMP_NUMTASKS_MODIFIER(Name) OMPC_NUMTASKS_##Name,
206+
#include "clang/Basic/OpenMPKinds.def"
207+
OMPC_NUMTASKS_unknown
208+
};
209+
204210
/// Contains 'interop' data for 'append_args' and 'init' clauses.
205211
class Expr;
206212
struct OMPInteropInfo final {

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11685,8 +11685,10 @@ class Sema final {
1168511685
SourceLocation ModifierLoc,
1168611686
SourceLocation EndLoc);
1168711687
/// Called on well-formed 'num_tasks' clause.
11688-
OMPClause *ActOnOpenMPNumTasksClause(Expr *NumTasks, SourceLocation StartLoc,
11688+
OMPClause *ActOnOpenMPNumTasksClause(OpenMPNumTasksClauseModifier Modifier,
11689+
Expr *NumTasks, SourceLocation StartLoc,
1168911690
SourceLocation LParenLoc,
11691+
SourceLocation ModifierLoc,
1169011692
SourceLocation EndLoc);
1169111693
/// Called on well-formed 'hint' clause.
1169211694
OMPClause *ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,

clang/lib/AST/OpenMPClause.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,11 @@ void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
19311931

19321932
void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
19331933
OS << "num_tasks(";
1934+
OpenMPNumTasksClauseModifier Modifier = Node->getModifier();
1935+
if (Modifier != OMPC_NUMTASKS_unknown) {
1936+
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
1937+
<< ": ";
1938+
}
19341939
Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
19351940
OS << ")";
19361941
}

clang/lib/Basic/OpenMPKinds.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
158158
return OMPC_GRAINSIZE_unknown;
159159
return Type;
160160
}
161+
case OMPC_num_tasks: {
162+
unsigned Type = llvm::StringSwitch<unsigned>(Str)
163+
#define OPENMP_NUMTASKS_MODIFIER(Name) .Case(#Name, OMPC_NUMTASKS_##Name)
164+
#include "clang/Basic/OpenMPKinds.def"
165+
.Default(OMPC_NUMTASKS_unknown);
166+
if (LangOpts.OpenMP < 51)
167+
return OMPC_NUMTASKS_unknown;
168+
return Type;
169+
}
161170
case OMPC_unknown:
162171
case OMPC_threadprivate:
163172
case OMPC_if:
@@ -198,7 +207,6 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
198207
case OMPC_thread_limit:
199208
case OMPC_priority:
200209
case OMPC_nogroup:
201-
case OMPC_num_tasks:
202210
case OMPC_hint:
203211
case OMPC_uniform:
204212
case OMPC_use_device_ptr:
@@ -454,6 +462,16 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
454462
#include "clang/Basic/OpenMPKinds.def"
455463
}
456464
llvm_unreachable("Invalid OpenMP 'grainsize' clause modifier");
465+
case OMPC_num_tasks:
466+
switch (Type) {
467+
case OMPC_NUMTASKS_unknown:
468+
return "unknown";
469+
#define OPENMP_NUMTASKS_MODIFIER(Name) \
470+
case OMPC_NUMTASKS_##Name: \
471+
return #Name;
472+
#include "clang/Basic/OpenMPKinds.def"
473+
}
474+
llvm_unreachable("Invalid OpenMP 'num_tasks' clause modifier");
457475
case OMPC_unknown:
458476
case OMPC_threadprivate:
459477
case OMPC_if:
@@ -494,7 +512,6 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
494512
case OMPC_thread_limit:
495513
case OMPC_priority:
496514
case OMPC_nogroup:
497-
case OMPC_num_tasks:
498515
case OMPC_hint:
499516
case OMPC_uniform:
500517
case OMPC_use_device_ptr:

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,7 +3226,7 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
32263226
if ((CKind == OMPC_ordered || CKind == OMPC_partial) &&
32273227
PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
32283228
Clause = ParseOpenMPClause(CKind, WrongDirective);
3229-
else if (CKind == OMPC_grainsize)
3229+
else if (CKind == OMPC_grainsize || CKind == OMPC_num_tasks)
32303230
Clause = ParseOpenMPSingleExprWithArgClause(DKind, CKind, WrongDirective);
32313231
else
32323232
Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
@@ -3900,6 +3900,33 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
39003900
Arg.push_back(OMPC_GRAINSIZE_unknown);
39013901
KLoc.emplace_back();
39023902
}
3903+
} else if (Kind == OMPC_num_tasks) {
3904+
// Parse optional <num_tasks modifier> ':'
3905+
OpenMPNumTasksClauseModifier Modifier =
3906+
static_cast<OpenMPNumTasksClauseModifier>(getOpenMPSimpleClauseType(
3907+
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok),
3908+
getLangOpts()));
3909+
if (getLangOpts().OpenMP >= 51) {
3910+
if (NextToken().is(tok::colon)) {
3911+
Arg.push_back(Modifier);
3912+
KLoc.push_back(Tok.getLocation());
3913+
// Parse modifier
3914+
ConsumeAnyToken();
3915+
// Parse ':'
3916+
ConsumeAnyToken();
3917+
} else {
3918+
if (Modifier == OMPC_NUMTASKS_strict) {
3919+
Diag(Tok, diag::err_modifier_expected_colon) << "strict";
3920+
// Parse modifier
3921+
ConsumeAnyToken();
3922+
}
3923+
Arg.push_back(OMPC_NUMTASKS_unknown);
3924+
KLoc.emplace_back();
3925+
}
3926+
} else {
3927+
Arg.push_back(OMPC_NUMTASKS_unknown);
3928+
KLoc.emplace_back();
3929+
}
39033930
} else {
39043931
assert(Kind == OMPC_if);
39053932
KLoc.push_back(Tok.getLocation());
@@ -3923,7 +3950,7 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
39233950
bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
39243951
(Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
39253952
Kind == OMPC_if || Kind == OMPC_device ||
3926-
Kind == OMPC_grainsize;
3953+
Kind == OMPC_grainsize || Kind == OMPC_num_tasks;
39273954
if (NeedAnExpression) {
39283955
SourceLocation ELoc = Tok.getLocation();
39293956
ExprResult LHS(ParseCastExpression(AnyCastExpr, false, NotTypeCast));

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15110,9 +15110,6 @@ OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
1511015110
case OMPC_priority:
1511115111
Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
1511215112
break;
15113-
case OMPC_num_tasks:
15114-
Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
15115-
break;
1511615113
case OMPC_hint:
1511715114
Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
1511815115
break;
@@ -15138,6 +15135,7 @@ OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
1513815135
Res = ActOnOpenMPAlignClause(Expr, StartLoc, LParenLoc, EndLoc);
1513915136
break;
1514015137
case OMPC_grainsize:
15138+
case OMPC_num_tasks:
1514115139
case OMPC_device:
1514215140
case OMPC_if:
1514315141
case OMPC_default:
@@ -16884,6 +16882,13 @@ OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1688416882
static_cast<OpenMPGrainsizeClauseModifier>(Argument.back()), Expr,
1688516883
StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc);
1688616884
break;
16885+
case OMPC_num_tasks:
16886+
assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
16887+
"Modifier for num_tasks clause and its location are expected.");
16888+
Res = ActOnOpenMPNumTasksClause(
16889+
static_cast<OpenMPNumTasksClauseModifier>(Argument.back()), Expr,
16890+
StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc);
16891+
break;
1688716892
case OMPC_final:
1688816893
case OMPC_num_threads:
1688916894
case OMPC_safelen:
@@ -16930,7 +16935,6 @@ OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1693016935
case OMPC_thread_limit:
1693116936
case OMPC_priority:
1693216937
case OMPC_nogroup:
16933-
case OMPC_num_tasks:
1693416938
case OMPC_hint:
1693516939
case OMPC_unknown:
1693616940
case OMPC_uniform:
@@ -22393,10 +22397,21 @@ OMPClause *Sema::ActOnOpenMPGrainsizeClause(
2239322397
StartLoc, LParenLoc, ModifierLoc, EndLoc);
2239422398
}
2239522399

22396-
OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
22397-
SourceLocation StartLoc,
22398-
SourceLocation LParenLoc,
22399-
SourceLocation EndLoc) {
22400+
OMPClause *Sema::ActOnOpenMPNumTasksClause(
22401+
OpenMPNumTasksClauseModifier Modifier, Expr *NumTasks,
22402+
SourceLocation StartLoc, SourceLocation LParenLoc,
22403+
SourceLocation ModifierLoc, SourceLocation EndLoc) {
22404+
assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 51) &&
22405+
"Unexpected num_tasks modifier in OpenMP < 51.");
22406+
22407+
if (ModifierLoc.isValid() && Modifier == OMPC_NUMTASKS_unknown) {
22408+
std::string Values = getListOfPossibleValues(OMPC_num_tasks, /*First=*/0,
22409+
OMPC_NUMTASKS_unknown);
22410+
Diag(ModifierLoc, diag::err_omp_unexpected_clause_value)
22411+
<< Values << getOpenMPClauseName(OMPC_num_tasks);
22412+
return nullptr;
22413+
}
22414+
2240022415
Expr *ValExpr = NumTasks;
2240122416
Stmt *HelperValStmt = nullptr;
2240222417
OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
@@ -22410,8 +22425,9 @@ OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
2241022425
DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
2241122426
return nullptr;
2241222427

22413-
return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion,
22414-
StartLoc, LParenLoc, EndLoc);
22428+
return new (Context)
22429+
OMPNumTasksClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
22430+
StartLoc, LParenLoc, ModifierLoc, EndLoc);
2241522431
}
2241622432

2241722433
OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,

clang/lib/Sema/TreeTransform.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,11 +2064,13 @@ class TreeTransform {
20642064
///
20652065
/// By default, performs semantic analysis to build the new statement.
20662066
/// Subclasses may override this routine to provide different behavior.
2067-
OMPClause *RebuildOMPNumTasksClause(Expr *NumTasks, SourceLocation StartLoc,
2067+
OMPClause *RebuildOMPNumTasksClause(OpenMPNumTasksClauseModifier Modifier,
2068+
Expr *NumTasks, SourceLocation StartLoc,
20682069
SourceLocation LParenLoc,
2070+
SourceLocation ModifierLoc,
20692071
SourceLocation EndLoc) {
2070-
return getSema().ActOnOpenMPNumTasksClause(NumTasks, StartLoc, LParenLoc,
2071-
EndLoc);
2072+
return getSema().ActOnOpenMPNumTasksClause(Modifier, NumTasks, StartLoc,
2073+
LParenLoc, ModifierLoc, EndLoc);
20722074
}
20732075

20742076
/// Build a new OpenMP 'hint' clause.
@@ -10363,7 +10365,8 @@ TreeTransform<Derived>::TransformOMPNumTasksClause(OMPNumTasksClause *C) {
1036310365
if (E.isInvalid())
1036410366
return nullptr;
1036510367
return getDerived().RebuildOMPNumTasksClause(
10366-
E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc());
10368+
C->getModifier(), E.get(), C->getBeginLoc(), C->getLParenLoc(),
10369+
C->getModifierLoc(), C->getEndLoc());
1036710370
}
1036810371

1036910372
template <typename Derived>

clang/lib/Serialization/ASTReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10792,7 +10792,9 @@ void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
1079210792

1079310793
void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
1079410794
VisitOMPClauseWithPreInit(C);
10795+
C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
1079510796
C->setNumTasks(Record.readSubExpr());
10797+
C->setModifierLoc(Record.readSourceLocation());
1079610798
C->setLParenLoc(Record.readSourceLocation());
1079710799
}
1079810800

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6798,7 +6798,9 @@ void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
67986798

67996799
void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
68006800
VisitOMPClauseWithPreInit(C);
6801+
Record.writeEnum(C->getModifier());
68016802
Record.AddStmt(C->getNumTasks());
6803+
Record.AddSourceLocation(C->getModifierLoc());
68026804
Record.AddSourceLocation(C->getLParenLoc());
68036805
}
68046806

0 commit comments

Comments
 (0)