Skip to content

Commit 62c87d2

Browse files
committedMar 21, 2014
[OPENMP] parsing of clause 'safelen' (for directive 'omp simd')
llvm-svn: 204428
1 parent db55b02 commit 62c87d2

17 files changed

+363
-6
lines changed
 

‎clang/include/clang/AST/DataRecursiveASTVisitor.h

+7
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,13 @@ bool DataRecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(
23672367
return true;
23682368
}
23692369

2370+
template<typename Derived>
2371+
bool DataRecursiveASTVisitor<Derived>::VisitOMPSafelenClause(
2372+
OMPSafelenClause *C) {
2373+
TraverseStmt(C->getSafelen());
2374+
return true;
2375+
}
2376+
23702377
template<typename Derived>
23712378
bool DataRecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *C) {
23722379
return true;

‎clang/include/clang/AST/OpenMPClause.h

+56
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,62 @@ class OMPNumThreadsClause : public OMPClause {
250250
StmtRange children() { return StmtRange(&NumThreads, &NumThreads + 1); }
251251
};
252252

253+
/// \brief This represents 'safelen' clause in the '#pragma omp ...'
254+
/// directive.
255+
///
256+
/// \code
257+
/// #pragma omp simd safelen(4)
258+
/// \endcode
259+
/// In this example directive '#pragma omp simd' has clause 'safelen'
260+
/// with single expression '4'.
261+
/// If the safelen clause is used then no two iterations executed
262+
/// concurrently with SIMD instructions can have a greater distance
263+
/// in the logical iteration space than its value. The parameter of
264+
/// the safelen clause must be a constant positive integer expression.
265+
///
266+
class OMPSafelenClause : public OMPClause {
267+
friend class OMPClauseReader;
268+
/// \brief Location of '('.
269+
SourceLocation LParenLoc;
270+
/// \brief Safe iteration space distance.
271+
Stmt *Safelen;
272+
273+
/// \brief Set safelen.
274+
void setSafelen(Expr *Len) { Safelen = Len; }
275+
276+
public:
277+
/// \brief Build 'safelen' clause.
278+
///
279+
/// \param Len Expression associated with this clause.
280+
/// \param StartLoc Starting location of the clause.
281+
/// \param EndLoc Ending location of the clause.
282+
///
283+
OMPSafelenClause(Expr *Len, SourceLocation StartLoc, SourceLocation LParenLoc,
284+
SourceLocation EndLoc)
285+
: OMPClause(OMPC_safelen, StartLoc, EndLoc), LParenLoc(LParenLoc),
286+
Safelen(Len) {}
287+
288+
/// \brief Build an empty clause.
289+
///
290+
explicit OMPSafelenClause()
291+
: OMPClause(OMPC_safelen, SourceLocation(), SourceLocation()),
292+
LParenLoc(SourceLocation()), Safelen(0) {}
293+
294+
/// \brief Sets the location of '('.
295+
void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
296+
/// \brief Returns the location of '('.
297+
SourceLocation getLParenLoc() const { return LParenLoc; }
298+
299+
/// \brief Return safe iteration space distance.
300+
Expr *getSafelen() const { return cast_or_null<Expr>(Safelen); }
301+
302+
static bool classof(const OMPClause *T) {
303+
return T->getClauseKind() == OMPC_safelen;
304+
}
305+
306+
StmtRange children() { return StmtRange(&Safelen, &Safelen + 1); }
307+
};
308+
253309
/// \brief This represents 'default' clause in the '#pragma omp ...' directive.
254310
///
255311
/// \code

‎clang/include/clang/AST/RecursiveASTVisitor.h

+6
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,12 @@ bool RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(
24052405
return true;
24062406
}
24072407

2408+
template <typename Derived>
2409+
bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) {
2410+
TraverseStmt(C->getSafelen());
2411+
return true;
2412+
}
2413+
24082414
template<typename Derived>
24092415
bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *C) {
24102416
return true;

‎clang/include/clang/Basic/OpenMPKinds.def

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ OPENMP_DIRECTIVE(simd)
3737
// OpenMP clauses.
3838
OPENMP_CLAUSE(if, OMPIfClause)
3939
OPENMP_CLAUSE(num_threads, OMPNumThreadsClause)
40+
OPENMP_CLAUSE(safelen, OMPSafelenClause)
4041
OPENMP_CLAUSE(default, OMPDefaultClause)
4142
OPENMP_CLAUSE(private, OMPPrivateClause)
4243
OPENMP_CLAUSE(firstprivate, OMPFirstprivateClause)
4344
OPENMP_CLAUSE(shared, OMPSharedClause)
4445

45-
// Clauses allowed for OpenMP directives.
46+
// Clauses allowed for OpenMP directive 'parallel'.
4647
OPENMP_PARALLEL_CLAUSE(if)
4748
OPENMP_PARALLEL_CLAUSE(num_threads)
4849
OPENMP_PARALLEL_CLAUSE(default)
@@ -52,6 +53,7 @@ OPENMP_PARALLEL_CLAUSE(shared)
5253

5354
// FIXME: more clauses allowed for directive 'omp simd'.
5455
OPENMP_SIMD_CLAUSE(private)
56+
OPENMP_SIMD_CLAUSE(safelen)
5557

5658
// Static attributes for 'default' clause.
5759
OPENMP_DEFAULT_KIND(none)

‎clang/include/clang/Sema/Sema.h

+7
Original file line numberDiff line numberDiff line change
@@ -7114,6 +7114,8 @@ class Sema {
71147114
void InitDataSharingAttributesStack();
71157115
void DestroyDataSharingAttributesStack();
71167116
ExprResult PerformImplicitIntegerConversion(SourceLocation OpLoc, Expr *Op);
7117+
ExprResult VerifyPositiveIntegerConstantInClause(Expr *Op,
7118+
OpenMPClauseKind CKind);
71177119
public:
71187120
/// \brief Called on start of new data sharing attribute block.
71197121
void StartOpenMPDSABlock(OpenMPDirectiveKind K,
@@ -7169,6 +7171,11 @@ class Sema {
71697171
SourceLocation StartLoc,
71707172
SourceLocation LParenLoc,
71717173
SourceLocation EndLoc);
7174+
/// \brief Called on well-formed 'safelen' clause.
7175+
OMPClause *ActOnOpenMPSafelenClause(Expr *Length,
7176+
SourceLocation StartLoc,
7177+
SourceLocation LParenLoc,
7178+
SourceLocation EndLoc);
71727179

71737180
OMPClause *ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
71747181
unsigned Argument,

‎clang/lib/AST/StmtPrinter.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,12 @@ void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
613613
OS << ")";
614614
}
615615

616+
void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
617+
OS << "safelen(";
618+
Node->getSafelen()->printPretty(OS, 0, Policy, 0);
619+
OS << ")";
620+
}
621+
616622
void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
617623
OS << "default("
618624
<< getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())

‎clang/lib/AST/StmtProfile.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ void OMPClauseProfiler::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
274274
Profiler->VisitStmt(C->getNumThreads());
275275
}
276276

277+
void OMPClauseProfiler::VisitOMPSafelenClause(const OMPSafelenClause *C) {
278+
if (C->getSafelen())
279+
Profiler->VisitStmt(C->getSafelen());
280+
}
281+
277282
void OMPClauseProfiler::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
278283

279284
template<typename T>

‎clang/lib/Basic/OpenMPKinds.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind,
7979
case OMPC_threadprivate:
8080
case OMPC_if:
8181
case OMPC_num_threads:
82+
case OMPC_safelen:
8283
case OMPC_private:
8384
case OMPC_firstprivate:
8485
case OMPC_shared:
@@ -104,6 +105,7 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
104105
case OMPC_threadprivate:
105106
case OMPC_if:
106107
case OMPC_num_threads:
108+
case OMPC_safelen:
107109
case OMPC_private:
108110
case OMPC_firstprivate:
109111
case OMPC_shared:

‎clang/lib/Parse/ParseOpenMP.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
257257
/// \brief Parsing of OpenMP clauses.
258258
///
259259
/// clause:
260-
/// default-clause|private-clause|firstprivate-clause|shared-clause
260+
/// if-clause | num_threads-clause | safelen-clause | default-clause |
261+
/// private-clause | firstprivate-clause | shared-clause
261262
///
262263
OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
263264
OpenMPClauseKind CKind, bool FirstClause) {
@@ -273,9 +274,12 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
273274
switch (CKind) {
274275
case OMPC_if:
275276
case OMPC_num_threads:
277+
case OMPC_safelen:
276278
// OpenMP [2.5, Restrictions]
277279
// At most one if clause can appear on the directive.
278280
// At most one num_threads clause can appear on the directive.
281+
// OpenMP [2.8.1, simd construct, Restrictions]
282+
// Only one safelen clause can appear on a simd directive.
279283
if (!FirstClause) {
280284
Diag(Tok, diag::err_omp_more_one_clause)
281285
<< getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
@@ -321,6 +325,12 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
321325
/// if-clause:
322326
/// 'if' '(' expression ')'
323327
///
328+
/// num_threads-clause:
329+
/// 'num_threads' '(' expression ')'
330+
///
331+
/// safelen-clause:
332+
/// 'safelen' '(' expression ')'
333+
///
324334
OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
325335
SourceLocation Loc = ConsumeToken();
326336

‎clang/lib/Sema/SemaOpenMP.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,9 @@ OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
761761
case OMPC_num_threads:
762762
Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
763763
break;
764+
case OMPC_safelen:
765+
Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
766+
break;
764767
case OMPC_default:
765768
case OMPC_private:
766769
case OMPC_firstprivate:
@@ -868,6 +871,38 @@ OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
868871
EndLoc);
869872
}
870873

874+
ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
875+
OpenMPClauseKind CKind) {
876+
if (!E)
877+
return ExprError();
878+
if (E->isValueDependent() || E->isTypeDependent() ||
879+
E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
880+
return Owned(E);
881+
llvm::APSInt Result;
882+
ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
883+
if (ICE.isInvalid())
884+
return ExprError();
885+
if (!Result.isStrictlyPositive()) {
886+
Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
887+
<< getOpenMPClauseName(CKind) << E->getSourceRange();
888+
return ExprError();
889+
}
890+
return ICE;
891+
}
892+
893+
OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
894+
SourceLocation LParenLoc,
895+
SourceLocation EndLoc) {
896+
// OpenMP [2.8.1, simd construct, Description]
897+
// The parameter of the safelen clause must be a constant
898+
// positive integer expression.
899+
ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
900+
if (Safelen.isInvalid())
901+
return 0;
902+
return new (Context)
903+
OMPSafelenClause(Safelen.take(), StartLoc, LParenLoc, EndLoc);
904+
}
905+
871906
OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
872907
unsigned Argument,
873908
SourceLocation ArgumentLoc,
@@ -883,6 +918,7 @@ OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
883918
break;
884919
case OMPC_if:
885920
case OMPC_num_threads:
921+
case OMPC_safelen:
886922
case OMPC_private:
887923
case OMPC_firstprivate:
888924
case OMPC_shared:
@@ -956,6 +992,7 @@ OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
956992
break;
957993
case OMPC_if:
958994
case OMPC_num_threads:
995+
case OMPC_safelen:
959996
case OMPC_default:
960997
case OMPC_threadprivate:
961998
case OMPC_unknown:

‎clang/lib/Sema/TreeTransform.h

+20
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,16 @@ class TreeTransform {
13231323
LParenLoc, EndLoc);
13241324
}
13251325

1326+
/// \brief Build a new OpenMP 'safelen' clause.
1327+
///
1328+
/// By default, performs semantic analysis to build the new statement.
1329+
/// Subclasses may override this routine to provide different behavior.
1330+
OMPClause *RebuildOMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1331+
SourceLocation LParenLoc,
1332+
SourceLocation EndLoc) {
1333+
return getSema().ActOnOpenMPSafelenClause(Len, StartLoc, LParenLoc, EndLoc);
1334+
}
1335+
13261336
/// \brief Build a new OpenMP 'default' clause.
13271337
///
13281338
/// By default, performs semantic analysis to build the new statement.
@@ -6340,6 +6350,16 @@ TreeTransform<Derived>::TransformOMPNumThreadsClause(OMPNumThreadsClause *C) {
63406350
C->getLocEnd());
63416351
}
63426352

6353+
template <typename Derived>
6354+
OMPClause *
6355+
TreeTransform<Derived>::TransformOMPSafelenClause(OMPSafelenClause *C) {
6356+
ExprResult E = getDerived().TransformExpr(C->getSafelen());
6357+
if (E.isInvalid())
6358+
return 0;
6359+
return getDerived().RebuildOMPSafelenClause(
6360+
E.take(), C->getLocStart(), C->getLParenLoc(), C->getLocEnd());
6361+
}
6362+
63436363
template<typename Derived>
63446364
OMPClause *
63456365
TreeTransform<Derived>::TransformOMPDefaultClause(OMPDefaultClause *C) {

‎clang/lib/Serialization/ASTReaderStmt.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,9 @@ OMPClause *OMPClauseReader::readClause() {
16771677
case OMPC_num_threads:
16781678
C = new (Context) OMPNumThreadsClause();
16791679
break;
1680+
case OMPC_safelen:
1681+
C = new (Context) OMPSafelenClause();
1682+
break;
16801683
case OMPC_default:
16811684
C = new (Context) OMPDefaultClause();
16821685
break;
@@ -1707,6 +1710,11 @@ void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
17071710
C->setLParenLoc(Reader->ReadSourceLocation(Record, Idx));
17081711
}
17091712

1713+
void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
1714+
C->setSafelen(Reader->Reader.ReadSubExpr());
1715+
C->setLParenLoc(Reader->ReadSourceLocation(Record, Idx));
1716+
}
1717+
17101718
void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
17111719
C->setDefaultKind(
17121720
static_cast<OpenMPDefaultClauseKind>(Record[Idx++]));

‎clang/lib/Serialization/ASTWriterStmt.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,11 @@ void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
16841684
Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
16851685
}
16861686

1687+
void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
1688+
Writer->Writer.AddStmt(C->getSafelen());
1689+
Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
1690+
}
1691+
16871692
void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
16881693
Record.push_back(C->getDefaultKind());
16891694
Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);

0 commit comments

Comments
 (0)
Please sign in to comment.