Skip to content

Commit 9cbdbe2

Browse files
jyu2-gitmemfrob
authored andcommitted
[OPENMP5.1]Initial support for novariants clause.
Added basic parsing/sema/serialization support for the 'novariants' clause.
1 parent bcfa193 commit 9cbdbe2

File tree

16 files changed

+232
-6
lines changed

16 files changed

+232
-6
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7649,6 +7649,77 @@ class OMPDestroyClause final : public OMPClause {
76497649
}
76507650
};
76517651

7652+
/// This represents 'novariants' clause in the '#pragma omp ...' directive.
7653+
///
7654+
/// \code
7655+
/// #pragma omp dispatch novariants(a > 5)
7656+
/// \endcode
7657+
/// In this example directive '#pragma omp dispatch' has simple 'novariants'
7658+
/// clause with condition 'a > 5'.
7659+
class OMPNovariantsClause final : public OMPClause,
7660+
public OMPClauseWithPreInit {
7661+
friend class OMPClauseReader;
7662+
7663+
/// Location of '('.
7664+
SourceLocation LParenLoc;
7665+
7666+
/// Condition of the 'if' clause.
7667+
Stmt *Condition = nullptr;
7668+
7669+
/// Set condition.
7670+
void setCondition(Expr *Cond) { Condition = Cond; }
7671+
7672+
/// Sets the location of '('.
7673+
void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
7674+
7675+
public:
7676+
/// Build 'novariants' clause with condition \a Cond.
7677+
///
7678+
/// \param Cond Condition of the clause.
7679+
/// \param HelperCond Helper condition for the construct.
7680+
/// \param CaptureRegion Innermost OpenMP region where expressions in this
7681+
/// clause must be captured.
7682+
/// \param StartLoc Starting location of the clause.
7683+
/// \param LParenLoc Location of '('.
7684+
/// \param EndLoc Ending location of the clause.
7685+
OMPNovariantsClause(Expr *Cond, Stmt *HelperCond,
7686+
OpenMPDirectiveKind CaptureRegion,
7687+
SourceLocation StartLoc, SourceLocation LParenLoc,
7688+
SourceLocation EndLoc)
7689+
: OMPClause(llvm::omp::OMPC_novariants, StartLoc, EndLoc),
7690+
OMPClauseWithPreInit(this), LParenLoc(LParenLoc), Condition(Cond) {
7691+
setPreInitStmt(HelperCond, CaptureRegion);
7692+
}
7693+
7694+
/// Build an empty clause.
7695+
OMPNovariantsClause()
7696+
: OMPClause(llvm::omp::OMPC_novariants, SourceLocation(),
7697+
SourceLocation()),
7698+
OMPClauseWithPreInit(this) {}
7699+
7700+
/// Returns the location of '('.
7701+
SourceLocation getLParenLoc() const { return LParenLoc; }
7702+
7703+
/// Returns condition.
7704+
Expr *getCondition() const { return cast_or_null<Expr>(Condition); }
7705+
7706+
child_range children() { return child_range(&Condition, &Condition + 1); }
7707+
7708+
const_child_range children() const {
7709+
return const_child_range(&Condition, &Condition + 1);
7710+
}
7711+
7712+
child_range used_children();
7713+
const_child_range used_children() const {
7714+
auto Children = const_cast<OMPNovariantsClause *>(this)->used_children();
7715+
return const_child_range(Children.begin(), Children.end());
7716+
}
7717+
7718+
static bool classof(const OMPClause *T) {
7719+
return T->getClauseKind() == llvm::omp::OMPC_novariants;
7720+
}
7721+
};
7722+
76527723
/// This represents 'detach' clause in the '#pragma omp task' directive.
76537724
///
76547725
/// \code

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3218,6 +3218,14 @@ bool RecursiveASTVisitor<Derived>::VisitOMPDestroyClause(OMPDestroyClause *C) {
32183218
return true;
32193219
}
32203220

3221+
template <typename Derived>
3222+
bool RecursiveASTVisitor<Derived>::VisitOMPNovariantsClause(
3223+
OMPNovariantsClause *C) {
3224+
TRY_TO(VisitOMPClauseWithPreInit(C));
3225+
TRY_TO(TraverseStmt(C->getCondition()));
3226+
return true;
3227+
}
3228+
32213229
template <typename Derived>
32223230
template <typename T>
32233231
bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) {

clang/include/clang/Sema/Sema.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11017,7 +11017,11 @@ class Sema final {
1101711017
SourceLocation LParenLoc,
1101811018
SourceLocation VarLoc,
1101911019
SourceLocation EndLoc);
11020-
11020+
/// Called on well-formed 'novariants' clause.
11021+
OMPClause *ActOnOpenMPNovariantsClause(Expr *Condition,
11022+
SourceLocation StartLoc,
11023+
SourceLocation LParenLoc,
11024+
SourceLocation EndLoc);
1102111025
/// Called on well-formed 'threads' clause.
1102211026
OMPClause *ActOnOpenMPThreadsClause(SourceLocation StartLoc,
1102311027
SourceLocation EndLoc);

clang/lib/AST/OpenMPClause.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
9696
return static_cast<const OMPFinalClause *>(C);
9797
case OMPC_priority:
9898
return static_cast<const OMPPriorityClause *>(C);
99+
case OMPC_novariants:
100+
return static_cast<const OMPNovariantsClause *>(C);
99101
case OMPC_default:
100102
case OMPC_proc_bind:
101103
case OMPC_safelen:
@@ -244,6 +246,7 @@ const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C)
244246
case OMPC_nontemporal:
245247
case OMPC_order:
246248
case OMPC_destroy:
249+
case OMPC_novariants:
247250
case OMPC_detach:
248251
case OMPC_inclusive:
249252
case OMPC_exclusive:
@@ -300,6 +303,12 @@ OMPClause::child_range OMPPriorityClause::used_children() {
300303
return child_range(&Priority, &Priority + 1);
301304
}
302305

306+
OMPClause::child_range OMPNovariantsClause::used_children() {
307+
if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
308+
return child_range(C, C + 1);
309+
return child_range(&Condition, &Condition + 1);
310+
}
311+
303312
OMPOrderedClause *OMPOrderedClause::Create(const ASTContext &C, Expr *Num,
304313
unsigned NumLoops,
305314
SourceLocation StartLoc,
@@ -1816,6 +1825,15 @@ void OMPClausePrinter::VisitOMPDestroyClause(OMPDestroyClause *Node) {
18161825
}
18171826
}
18181827

1828+
void OMPClausePrinter::VisitOMPNovariantsClause(OMPNovariantsClause *Node) {
1829+
OS << "novariants";
1830+
if (Expr *E = Node->getCondition()) {
1831+
OS << "(";
1832+
E->printPretty(OS, nullptr, Policy, 0);
1833+
OS << ")";
1834+
}
1835+
}
1836+
18191837
template<typename T>
18201838
void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
18211839
for (typename T::varlist_iterator I = Node->varlist_begin(),

clang/lib/AST/StmtProfile.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ void OMPClauseProfiler::VisitOMPDetachClause(const OMPDetachClause *C) {
483483
Profiler->VisitStmt(Evt);
484484
}
485485

486+
void OMPClauseProfiler::VisitOMPNovariantsClause(const OMPNovariantsClause *C) {
487+
VistOMPClauseWithPreInit(C);
488+
if (C->getCondition())
489+
Profiler->VisitStmt(C->getCondition());
490+
}
491+
486492
void OMPClauseProfiler::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
487493

488494
void OMPClauseProfiler::VisitOMPProcBindClause(const OMPProcBindClause *C) { }

clang/lib/Basic/OpenMPKinds.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
176176
case OMPC_match:
177177
case OMPC_nontemporal:
178178
case OMPC_destroy:
179+
case OMPC_novariants:
179180
case OMPC_detach:
180181
case OMPC_inclusive:
181182
case OMPC_exclusive:
@@ -418,6 +419,7 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
418419
case OMPC_nontemporal:
419420
case OMPC_destroy:
420421
case OMPC_detach:
422+
case OMPC_novariants:
421423
case OMPC_inclusive:
422424
case OMPC_exclusive:
423425
case OMPC_uses_allocators:

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,7 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
27762776
case OMPC_allocator:
27772777
case OMPC_depobj:
27782778
case OMPC_detach:
2779+
case OMPC_novariants:
27792780
// OpenMP [2.5, Restrictions]
27802781
// At most one num_threads clause can appear on the directive.
27812782
// OpenMP [2.8.1, simd construct, Restrictions]
@@ -2798,6 +2799,8 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
27982799
// At most one allocator clause can appear on the directive.
27992800
// OpenMP 5.0, 2.10.1 task Construct, Restrictions.
28002801
// At most one detach clause can appear on the directive.
2802+
// OpenMP 5.1, 2.3.6 dispatch Construct, Restrictions.
2803+
// At most one novariants clause can appear on a dispatch directive.
28012804
if (!FirstClause) {
28022805
Diag(Tok, diag::err_omp_more_one_clause)
28032806
<< getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6173,6 +6173,7 @@ StmtResult Sema::ActOnOpenMPExecutableDirective(
61736173
case OMPC_num_tasks:
61746174
case OMPC_final:
61756175
case OMPC_priority:
6176+
case OMPC_novariants:
61766177
// Do not analyze if no parent parallel directive.
61776178
if (isOpenMPParallelDirective(Kind))
61786179
break;
@@ -12785,6 +12786,9 @@ OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
1278512786
case OMPC_detach:
1278612787
Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc);
1278712788
break;
12789+
case OMPC_novariants:
12790+
Res = ActOnOpenMPNovariantsClause(Expr, StartLoc, LParenLoc, EndLoc);
12791+
break;
1278812792
case OMPC_device:
1278912793
case OMPC_if:
1279012794
case OMPC_default:
@@ -13557,6 +13561,15 @@ static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
1355713561
llvm_unreachable("Unknown OpenMP directive");
1355813562
}
1355913563
break;
13564+
case OMPC_novariants:
13565+
switch (DKind) {
13566+
case OMPD_dispatch:
13567+
CaptureRegion = OMPD_task;
13568+
break;
13569+
default:
13570+
llvm_unreachable("Unknown OpenMP directive");
13571+
}
13572+
break;
1356013573
case OMPC_firstprivate:
1356113574
case OMPC_lastprivate:
1356213575
case OMPC_reduction:
@@ -14061,6 +14074,7 @@ OMPClause *Sema::ActOnOpenMPSimpleClause(
1406114074
case OMPC_match:
1406214075
case OMPC_nontemporal:
1406314076
case OMPC_destroy:
14077+
case OMPC_novariants:
1406414078
case OMPC_detach:
1406514079
case OMPC_inclusive:
1406614080
case OMPC_exclusive:
@@ -14328,6 +14342,7 @@ OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1432814342
case OMPC_nontemporal:
1432914343
case OMPC_order:
1433014344
case OMPC_destroy:
14345+
case OMPC_novariants:
1433114346
case OMPC_detach:
1433214347
case OMPC_inclusive:
1433314348
case OMPC_exclusive:
@@ -14569,6 +14584,7 @@ OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
1456914584
case OMPC_match:
1457014585
case OMPC_nontemporal:
1457114586
case OMPC_order:
14587+
case OMPC_novariants:
1457214588
case OMPC_detach:
1457314589
case OMPC_inclusive:
1457414590
case OMPC_exclusive:
@@ -14859,6 +14875,36 @@ OMPClause *Sema::ActOnOpenMPDestroyClause(Expr *InteropVar,
1485914875
OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
1486014876
}
1486114877

14878+
OMPClause *Sema::ActOnOpenMPNovariantsClause(Expr *Condition,
14879+
SourceLocation StartLoc,
14880+
SourceLocation LParenLoc,
14881+
SourceLocation EndLoc) {
14882+
Expr *ValExpr = Condition;
14883+
Stmt *HelperValStmt = nullptr;
14884+
OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
14885+
if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
14886+
!Condition->isInstantiationDependent() &&
14887+
!Condition->containsUnexpandedParameterPack()) {
14888+
ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
14889+
if (Val.isInvalid())
14890+
return nullptr;
14891+
14892+
ValExpr = MakeFullExpr(Val.get()).get();
14893+
14894+
OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14895+
CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_novariants,
14896+
LangOpts.OpenMP);
14897+
if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14898+
ValExpr = MakeFullExpr(ValExpr).get();
14899+
llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14900+
HelperValStmt = buildPreInits(Context, Captures);
14901+
}
14902+
}
14903+
14904+
return new (Context) OMPNovariantsClause(
14905+
ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
14906+
}
14907+
1486214908
OMPClause *Sema::ActOnOpenMPVarListClause(
1486314909
OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr,
1486414910
const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
@@ -15029,6 +15075,7 @@ OMPClause *Sema::ActOnOpenMPVarListClause(
1502915075
case OMPC_match:
1503015076
case OMPC_order:
1503115077
case OMPC_destroy:
15078+
case OMPC_novariants:
1503215079
case OMPC_detach:
1503315080
case OMPC_uses_allocators:
1503415081
default:

clang/lib/Sema/TreeTransform.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,6 +2208,18 @@ class TreeTransform {
22082208
VarLoc, EndLoc);
22092209
}
22102210

2211+
/// Build a new OpenMP 'novariants' clause.
2212+
///
2213+
/// By default, performs semantic analysis to build the new OpenMP clause.
2214+
/// Subclasses may override this routine to provide different behavior.
2215+
OMPClause *RebuildOMPNovariantsClause(Expr *Condition,
2216+
SourceLocation StartLoc,
2217+
SourceLocation LParenLoc,
2218+
SourceLocation EndLoc) {
2219+
return getSema().ActOnOpenMPNovariantsClause(Condition, StartLoc, LParenLoc,
2220+
EndLoc);
2221+
}
2222+
22112223
/// Rebuild the operand to an Objective-C \@synchronized statement.
22122224
///
22132225
/// By default, performs semantic analysis to build the new statement.
@@ -9377,6 +9389,16 @@ TreeTransform<Derived>::TransformOMPDestroyClause(OMPDestroyClause *C) {
93779389
C->getEndLoc());
93789390
}
93799391

9392+
template <typename Derived>
9393+
OMPClause *
9394+
TreeTransform<Derived>::TransformOMPNovariantsClause(OMPNovariantsClause *C) {
9395+
ExprResult Cond = getDerived().TransformExpr(C->getCondition());
9396+
if (Cond.isInvalid())
9397+
return nullptr;
9398+
return getDerived().RebuildOMPNovariantsClause(
9399+
Cond.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc());
9400+
}
9401+
93809402
template <typename Derived>
93819403
OMPClause *TreeTransform<Derived>::TransformOMPUnifiedAddressClause(
93829404
OMPUnifiedAddressClause *C) {

clang/lib/Serialization/ASTReader.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11977,6 +11977,9 @@ OMPClause *OMPClauseReader::readClause() {
1197711977
case llvm::omp::OMPC_destroy:
1197811978
C = new (Context) OMPDestroyClause();
1197911979
break;
11980+
case llvm::omp::OMPC_novariants:
11981+
C = new (Context) OMPNovariantsClause();
11982+
break;
1198011983
case llvm::omp::OMPC_detach:
1198111984
C = new (Context) OMPDetachClause();
1198211985
break;
@@ -12162,6 +12165,12 @@ void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
1216212165
C->setVarLoc(Record.readSourceLocation());
1216312166
}
1216412167

12168+
void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12169+
VisitOMPClauseWithPreInit(C);
12170+
C->setCondition(Record.readSubExpr());
12171+
C->setLParenLoc(Record.readSourceLocation());
12172+
}
12173+
1216512174
void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
1216612175

1216712176
void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(

0 commit comments

Comments
 (0)