Skip to content

Commit 6f9c251

Browse files
committed
[OpenMP] Initial parsing/sema for the 'omp loop' construct
Adds basic parsing/sema/serialization support for the #pragma omp loop directive. Differential Revision: https://reviews.llvm.org/D112499
1 parent 66e03db commit 6f9c251

27 files changed

+588
-8
lines changed

clang/include/clang-c/Index.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2596,7 +2596,11 @@ enum CXCursorKind {
25962596
*/
25972597
CXCursor_OMPMetaDirective = 294,
25982598

2599-
CXCursor_LastStmt = CXCursor_OMPMetaDirective,
2599+
/** OpenMP loop directive.
2600+
*/
2601+
CXCursor_OMPGenericLoopDirective = 295,
2602+
2603+
CXCursor_LastStmt = CXCursor_OMPGenericLoopDirective,
26002604

26012605
/**
26022606
* Cursor that represents the translation unit itself.

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,9 @@ DEF_TRAVERSE_STMT(OMPDispatchDirective,
30233023
DEF_TRAVERSE_STMT(OMPMaskedDirective,
30243024
{ TRY_TO(TraverseOMPExecutableDirective(S)); })
30253025

3026+
DEF_TRAVERSE_STMT(OMPGenericLoopDirective,
3027+
{ TRY_TO(TraverseOMPExecutableDirective(S)); })
3028+
30263029
// OpenMP clauses.
30273030
template <typename Derived>
30283031
bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {

clang/include/clang/AST/StmtOpenMP.h

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ class OMPLoopDirective : public OMPLoopBasedDirective {
11441144
if (isOpenMPLoopBoundSharingDirective(Kind))
11451145
return CombinedDistributeEnd;
11461146
if (isOpenMPWorksharingDirective(Kind) || isOpenMPTaskLoopDirective(Kind) ||
1147-
isOpenMPDistributeDirective(Kind))
1147+
isOpenMPGenericLoopDirective(Kind) || isOpenMPDistributeDirective(Kind))
11481148
return WorksharingEnd;
11491149
return DefaultEnd;
11501150
}
@@ -1176,55 +1176,63 @@ class OMPLoopDirective : public OMPLoopBasedDirective {
11761176
}
11771177
void setIsLastIterVariable(Expr *IL) {
11781178
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1179+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
11791180
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
11801181
isOpenMPDistributeDirective(getDirectiveKind())) &&
11811182
"expected worksharing loop directive");
11821183
Data->getChildren()[IsLastIterVariableOffset] = IL;
11831184
}
11841185
void setLowerBoundVariable(Expr *LB) {
11851186
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1187+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
11861188
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
11871189
isOpenMPDistributeDirective(getDirectiveKind())) &&
11881190
"expected worksharing loop directive");
11891191
Data->getChildren()[LowerBoundVariableOffset] = LB;
11901192
}
11911193
void setUpperBoundVariable(Expr *UB) {
11921194
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1195+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
11931196
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
11941197
isOpenMPDistributeDirective(getDirectiveKind())) &&
11951198
"expected worksharing loop directive");
11961199
Data->getChildren()[UpperBoundVariableOffset] = UB;
11971200
}
11981201
void setStrideVariable(Expr *ST) {
11991202
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1203+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
12001204
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
12011205
isOpenMPDistributeDirective(getDirectiveKind())) &&
12021206
"expected worksharing loop directive");
12031207
Data->getChildren()[StrideVariableOffset] = ST;
12041208
}
12051209
void setEnsureUpperBound(Expr *EUB) {
12061210
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1211+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
12071212
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
12081213
isOpenMPDistributeDirective(getDirectiveKind())) &&
12091214
"expected worksharing loop directive");
12101215
Data->getChildren()[EnsureUpperBoundOffset] = EUB;
12111216
}
12121217
void setNextLowerBound(Expr *NLB) {
12131218
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1219+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
12141220
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
12151221
isOpenMPDistributeDirective(getDirectiveKind())) &&
12161222
"expected worksharing loop directive");
12171223
Data->getChildren()[NextLowerBoundOffset] = NLB;
12181224
}
12191225
void setNextUpperBound(Expr *NUB) {
12201226
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1227+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
12211228
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
12221229
isOpenMPDistributeDirective(getDirectiveKind())) &&
12231230
"expected worksharing loop directive");
12241231
Data->getChildren()[NextUpperBoundOffset] = NUB;
12251232
}
12261233
void setNumIterations(Expr *NI) {
12271234
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1235+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
12281236
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
12291237
isOpenMPDistributeDirective(getDirectiveKind())) &&
12301238
"expected worksharing loop directive");
@@ -1327,55 +1335,63 @@ class OMPLoopDirective : public OMPLoopBasedDirective {
13271335
Stmt *getPreInits() { return Data->getChildren()[PreInitsOffset]; }
13281336
Expr *getIsLastIterVariable() const {
13291337
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1338+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
13301339
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
13311340
isOpenMPDistributeDirective(getDirectiveKind())) &&
13321341
"expected worksharing loop directive");
13331342
return cast<Expr>(Data->getChildren()[IsLastIterVariableOffset]);
13341343
}
13351344
Expr *getLowerBoundVariable() const {
13361345
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1346+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
13371347
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
13381348
isOpenMPDistributeDirective(getDirectiveKind())) &&
13391349
"expected worksharing loop directive");
13401350
return cast<Expr>(Data->getChildren()[LowerBoundVariableOffset]);
13411351
}
13421352
Expr *getUpperBoundVariable() const {
13431353
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1354+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
13441355
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
13451356
isOpenMPDistributeDirective(getDirectiveKind())) &&
13461357
"expected worksharing loop directive");
13471358
return cast<Expr>(Data->getChildren()[UpperBoundVariableOffset]);
13481359
}
13491360
Expr *getStrideVariable() const {
13501361
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1362+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
13511363
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
13521364
isOpenMPDistributeDirective(getDirectiveKind())) &&
13531365
"expected worksharing loop directive");
13541366
return cast<Expr>(Data->getChildren()[StrideVariableOffset]);
13551367
}
13561368
Expr *getEnsureUpperBound() const {
13571369
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1370+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
13581371
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
13591372
isOpenMPDistributeDirective(getDirectiveKind())) &&
13601373
"expected worksharing loop directive");
13611374
return cast<Expr>(Data->getChildren()[EnsureUpperBoundOffset]);
13621375
}
13631376
Expr *getNextLowerBound() const {
13641377
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1378+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
13651379
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
13661380
isOpenMPDistributeDirective(getDirectiveKind())) &&
13671381
"expected worksharing loop directive");
13681382
return cast<Expr>(Data->getChildren()[NextLowerBoundOffset]);
13691383
}
13701384
Expr *getNextUpperBound() const {
13711385
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1386+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
13721387
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
13731388
isOpenMPDistributeDirective(getDirectiveKind())) &&
13741389
"expected worksharing loop directive");
13751390
return cast<Expr>(Data->getChildren()[NextUpperBoundOffset]);
13761391
}
13771392
Expr *getNumIterations() const {
13781393
assert((isOpenMPWorksharingDirective(getDirectiveKind()) ||
1394+
isOpenMPGenericLoopDirective(getDirectiveKind()) ||
13791395
isOpenMPTaskLoopDirective(getDirectiveKind()) ||
13801396
isOpenMPDistributeDirective(getDirectiveKind())) &&
13811397
"expected worksharing loop directive");
@@ -1509,6 +1525,7 @@ class OMPLoopDirective : public OMPLoopBasedDirective {
15091525
T->getStmtClass() == OMPTaskLoopSimdDirectiveClass ||
15101526
T->getStmtClass() == OMPMasterTaskLoopDirectiveClass ||
15111527
T->getStmtClass() == OMPMasterTaskLoopSimdDirectiveClass ||
1528+
T->getStmtClass() == OMPGenericLoopDirectiveClass ||
15121529
T->getStmtClass() == OMPParallelMasterTaskLoopDirectiveClass ||
15131530
T->getStmtClass() == OMPParallelMasterTaskLoopSimdDirectiveClass ||
15141531
T->getStmtClass() == OMPDistributeDirectiveClass ||
@@ -5461,6 +5478,69 @@ class OMPMetaDirective final : public OMPExecutableDirective {
54615478
}
54625479
};
54635480

5481+
/// This represents '#pragma omp loop' directive.
5482+
///
5483+
/// \code
5484+
/// #pragma omp loop private(a,b) binding(parallel) order(concurrent)
5485+
/// \endcode
5486+
/// In this example directive '#pragma omp loop' has
5487+
/// clauses 'private' with the variables 'a' and 'b', 'binding' with
5488+
/// modifier 'parallel' and 'order(concurrent).
5489+
///
5490+
class OMPGenericLoopDirective final : public OMPLoopDirective {
5491+
friend class ASTStmtReader;
5492+
friend class OMPExecutableDirective;
5493+
/// Build directive with the given start and end location.
5494+
///
5495+
/// \param StartLoc Starting location of the directive kind.
5496+
/// \param EndLoc Ending location of the directive.
5497+
/// \param CollapsedNum Number of collapsed nested loops.
5498+
///
5499+
OMPGenericLoopDirective(SourceLocation StartLoc, SourceLocation EndLoc,
5500+
unsigned CollapsedNum)
5501+
: OMPLoopDirective(OMPGenericLoopDirectiveClass, llvm::omp::OMPD_loop,
5502+
StartLoc, EndLoc, CollapsedNum) {}
5503+
5504+
/// Build an empty directive.
5505+
///
5506+
/// \param CollapsedNum Number of collapsed nested loops.
5507+
///
5508+
explicit OMPGenericLoopDirective(unsigned CollapsedNum)
5509+
: OMPLoopDirective(OMPGenericLoopDirectiveClass, llvm::omp::OMPD_loop,
5510+
SourceLocation(), SourceLocation(), CollapsedNum) {}
5511+
5512+
public:
5513+
/// Creates directive with a list of \p Clauses.
5514+
///
5515+
/// \param C AST context.
5516+
/// \param StartLoc Starting location of the directive kind.
5517+
/// \param EndLoc Ending Location of the directive.
5518+
/// \param CollapsedNum Number of collapsed loops.
5519+
/// \param Clauses List of clauses.
5520+
/// \param AssociatedStmt Statement, associated with the directive.
5521+
/// \param Exprs Helper expressions for CodeGen.
5522+
///
5523+
static OMPGenericLoopDirective *
5524+
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
5525+
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses,
5526+
Stmt *AssociatedStmt, const HelperExprs &Exprs);
5527+
5528+
/// Creates an empty directive with a place for \a NumClauses clauses.
5529+
///
5530+
/// \param C AST context.
5531+
/// \param NumClauses Number of clauses.
5532+
/// \param CollapsedNum Number of collapsed nested loops.
5533+
///
5534+
static OMPGenericLoopDirective *CreateEmpty(const ASTContext &C,
5535+
unsigned NumClauses,
5536+
unsigned CollapsedNum,
5537+
EmptyShell);
5538+
5539+
static bool classof(const Stmt *T) {
5540+
return T->getStmtClass() == OMPGenericLoopDirectiveClass;
5541+
}
5542+
};
5543+
54645544
} // end namespace clang
54655545

54665546
#endif

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10803,6 +10803,9 @@ def note_omp_protected_structured_block
1080310803
: Note<"jump bypasses OpenMP structured block">;
1080410804
def note_omp_exits_structured_block
1080510805
: Note<"jump exits scope of OpenMP structured block">;
10806+
def err_omp_lastprivate_loop_var_non_loop_iteration : Error<
10807+
"only loop iteration variables are allowed in 'lastprivate' clause in "
10808+
"'omp loop' directives">;
1080610809
def err_omp_interop_variable_expected : Error<
1080710810
"expected%select{| non-const}0 variable of type 'omp_interop_t'">;
1080810811
def err_omp_interop_variable_wrong_type : Error<

clang/include/clang/Basic/OpenMPKinds.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ bool isOpenMPDistributeDirective(OpenMPDirectiveKind DKind);
253253
/// otherwise - false.
254254
bool isOpenMPNestingDistributeDirective(OpenMPDirectiveKind DKind);
255255

256+
/// Checks if the specified directive constitutes a 'loop' directive in the
257+
/// outermost nest. For example, 'omp teams loop' or 'omp loop'.
258+
/// \param DKind Specified directive.
259+
/// \return true - the directive has loop on the outermost nest.
260+
/// otherwise - false.
261+
bool isOpenMPGenericLoopDirective(OpenMPDirectiveKind DKind);
262+
256263
/// Checks if the specified clause is one of private clauses like
257264
/// 'private', 'firstprivate', 'reduction' etc..
258265
/// \param Kind Clause kind.

clang/include/clang/Basic/StmtNodes.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,4 @@ def OMPTargetTeamsDistributeSimdDirective : StmtNode<OMPLoopDirective>;
282282
def OMPInteropDirective : StmtNode<OMPExecutableDirective>;
283283
def OMPDispatchDirective : StmtNode<OMPExecutableDirective>;
284284
def OMPMaskedDirective : StmtNode<OMPExecutableDirective>;
285+
def OMPGenericLoopDirective : StmtNode<OMPLoopDirective>;

clang/include/clang/Sema/Sema.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10940,6 +10940,12 @@ class Sema final {
1094010940
Stmt *AStmt, SourceLocation StartLoc,
1094110941
SourceLocation EndLoc);
1094210942

10943+
/// Called on well-formed '\#pragma omp loop' after parsing of the
10944+
/// associated statement.
10945+
StmtResult ActOnOpenMPGenericLoopDirective(
10946+
ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10947+
SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA);
10948+
1094310949
/// Checks correctness of linear modifiers.
1094410950
bool CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
1094510951
SourceLocation LinLoc);

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,7 @@ enum StmtCode {
19571957
STMT_OMP_INTEROP_DIRECTIVE,
19581958
STMT_OMP_DISPATCH_DIRECTIVE,
19591959
STMT_OMP_MASKED_DIRECTIVE,
1960+
STMT_OMP_GENERIC_LOOP_DIRECTIVE,
19601961
EXPR_OMP_ARRAY_SECTION,
19611962
EXPR_OMP_ARRAY_SHAPING,
19621963
EXPR_OMP_ITERATOR,

clang/lib/AST/StmtOpenMP.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,3 +2086,45 @@ OMPMaskedDirective *OMPMaskedDirective::CreateEmpty(const ASTContext &C,
20862086
return createEmptyDirective<OMPMaskedDirective>(C, NumClauses,
20872087
/*HasAssociatedStmt=*/true);
20882088
}
2089+
2090+
OMPGenericLoopDirective *OMPGenericLoopDirective::Create(
2091+
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
2092+
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
2093+
const HelperExprs &Exprs) {
2094+
auto *Dir = createDirective<OMPGenericLoopDirective>(
2095+
C, Clauses, AssociatedStmt, numLoopChildren(CollapsedNum, OMPD_loop),
2096+
StartLoc, EndLoc, CollapsedNum);
2097+
Dir->setIterationVariable(Exprs.IterationVarRef);
2098+
Dir->setLastIteration(Exprs.LastIteration);
2099+
Dir->setCalcLastIteration(Exprs.CalcLastIteration);
2100+
Dir->setPreCond(Exprs.PreCond);
2101+
Dir->setCond(Exprs.Cond);
2102+
Dir->setInit(Exprs.Init);
2103+
Dir->setInc(Exprs.Inc);
2104+
Dir->setIsLastIterVariable(Exprs.IL);
2105+
Dir->setLowerBoundVariable(Exprs.LB);
2106+
Dir->setUpperBoundVariable(Exprs.UB);
2107+
Dir->setStrideVariable(Exprs.ST);
2108+
Dir->setEnsureUpperBound(Exprs.EUB);
2109+
Dir->setNextLowerBound(Exprs.NLB);
2110+
Dir->setNextUpperBound(Exprs.NUB);
2111+
Dir->setNumIterations(Exprs.NumIterations);
2112+
Dir->setCounters(Exprs.Counters);
2113+
Dir->setPrivateCounters(Exprs.PrivateCounters);
2114+
Dir->setInits(Exprs.Inits);
2115+
Dir->setUpdates(Exprs.Updates);
2116+
Dir->setFinals(Exprs.Finals);
2117+
Dir->setDependentCounters(Exprs.DependentCounters);
2118+
Dir->setDependentInits(Exprs.DependentInits);
2119+
Dir->setFinalsConditions(Exprs.FinalsConditions);
2120+
Dir->setPreInits(Exprs.PreInits);
2121+
return Dir;
2122+
}
2123+
2124+
OMPGenericLoopDirective *
2125+
OMPGenericLoopDirective::CreateEmpty(const ASTContext &C, unsigned NumClauses,
2126+
unsigned CollapsedNum, EmptyShell) {
2127+
return createEmptyDirective<OMPGenericLoopDirective>(
2128+
C, NumClauses, /*HasAssociatedStmt=*/true,
2129+
numLoopChildren(CollapsedNum, OMPD_loop), CollapsedNum);
2130+
}

clang/lib/AST/StmtPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,11 @@ void StmtPrinter::VisitOMPMaskedDirective(OMPMaskedDirective *Node) {
10001000
PrintOMPExecutableDirective(Node);
10011001
}
10021002

1003+
void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *Node) {
1004+
Indent() << "#pragma omp loop";
1005+
PrintOMPExecutableDirective(Node);
1006+
}
1007+
10031008
//===----------------------------------------------------------------------===//
10041009
// Expr printing methods.
10051010
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)