Repository navigation
[Clang][OpenMP] Add parsing/sema for dims modifier in num_threads - #208353
Conversation
|
@llvm/pr-subscribers-clang-codegen @llvm/pr-subscribers-clang Author: Kevin Sala Penades (kevinsala) ChangesAdd parsing for constexpr int N = 2;
#pragma omp parallel num_threads(dims(N),strict: a, b)
{ ... }Patch is 83.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/208353.diff 31 Files Affected:
diff --git a/clang/include/clang/AST/OpenMPClause.h b/clang/include/clang/AST/OpenMPClause.h
index 641c03bf8ff5c..a3048709ccd8c 100644
--- a/clang/include/clang/AST/OpenMPClause.h
+++ b/clang/include/clang/AST/OpenMPClause.h
@@ -804,67 +804,175 @@ class OMPFinalClause final
return const_cast<OMPFinalClause *>(this)->used_children();
}
};
+
/// This represents 'num_threads' clause in the '#pragma omp ...'
/// directive.
///
/// \code
-/// #pragma omp parallel num_threads(6)
+/// #pragma omp parallel num_threads(n)
+/// \endcode
+/// In this example directive '#pragma omp parallel' has 'num_threads' clause
+/// requesting 'n' threads.
+///
+/// \code
+/// #pragma omp parallel num_threads(strict: n)
/// \endcode
-/// In this example directive '#pragma omp parallel' has simple 'num_threads'
-/// clause with number of threads '6'.
+/// In this example directive '#pragma omp parallel' has 'num_threads' clause
+/// requesting 'n' threads strictly with the 'strict' modifier.
+///
+/// \code
+/// #pragma omp parallel num_threads(dims(2): x, y)
+/// \endcode
+/// In this example directive '#pragma omp parallel' has clause 'num_threads'
+/// with the 'dims' modifier specifying two dimensions. The list specifies the
+/// number of threads in each dimension.
class OMPNumThreadsClause final
- : public OMPOneStmtClause<llvm::omp::OMPC_num_threads, OMPClause>,
- public OMPClauseWithPreInit {
+ : public OMPVarListClause<OMPNumThreadsClause>,
+ public OMPClauseWithPreInit,
+ private llvm::TrailingObjects<OMPNumThreadsClause, Expr *> {
+ friend class OMPVarListClause<OMPNumThreadsClause>;
+ friend TrailingObjects;
friend class OMPClauseReader;
- /// Modifiers for 'num_threads' clause.
- OpenMPNumThreadsClauseModifier Modifier = OMPC_NUMTHREADS_unknown;
+private:
+ /// Categories of modifiers for the 'num_threads' clause.
+ enum { PRESCRIPTIVENESS, DIMS, NUM_MODIFIERS };
- /// Location of the modifier.
- SourceLocation ModifierLoc;
+ /// Modifiers for the clause, indexed by category.
+ OpenMPNumThreadsClauseModifier Modifiers[NUM_MODIFIERS] = {
+ OMPC_NUMTHREADS_unknown, OMPC_NUMTHREADS_unknown};
- /// Sets modifier.
- void setModifier(OpenMPNumThreadsClauseModifier M) { Modifier = M; }
+ /// Locations of the modifiers, indexed by category.
+ SourceLocation ModifiersLoc[NUM_MODIFIERS];
- /// Sets modifier location.
- void setModifierLoc(SourceLocation Loc) { ModifierLoc = Loc; }
+ /// Build clause with number of expressions \a N.
+ ///
+ /// \param C AST context.
+ /// \param StartLoc Starting location of the clause.
+ /// \param LParenLoc Location of '('.
+ /// \param EndLoc Ending location of the clause.
+ /// \param N Number of expressions.
+ OMPNumThreadsClause(const ASTContext &C, SourceLocation StartLoc,
+ SourceLocation LParenLoc, SourceLocation EndLoc,
+ unsigned N)
+ : OMPVarListClause<OMPNumThreadsClause>(llvm::omp::OMPC_num_threads,
+ StartLoc, LParenLoc, EndLoc, N),
+ OMPClauseWithPreInit(this) {}
- /// Set condition.
- void setNumThreads(Expr *NThreads) { setStmt(NThreads); }
+ /// Build an empty clause.
+ ///
+ /// \param N Number of expressions.
+ explicit OMPNumThreadsClause(unsigned N)
+ : OMPVarListClause<OMPNumThreadsClause>(
+ llvm::omp::OMPC_num_threads, SourceLocation(), SourceLocation(),
+ SourceLocation(), N),
+ OMPClauseWithPreInit(this) {}
+
+ /// Sets the dims modifier.
+ void setDimsModifier(OpenMPNumThreadsClauseModifier M) {
+ Modifiers[DIMS] = M;
+ }
+
+ /// Sets the prescriptiveness modifier.
+ void setPrescriptivenessModifier(OpenMPNumThreadsClauseModifier M) {
+ Modifiers[PRESCRIPTIVENESS] = M;
+ }
+
+ /// Sets the location of the prescriptiveness modifier.
+ void setPrescriptivenessModifierLoc(SourceLocation Loc) {
+ ModifiersLoc[PRESCRIPTIVENESS] = Loc;
+ }
+
+ /// Sets the location of the dims modifier.
+ void setDimsModifierLoc(SourceLocation Loc) { ModifiersLoc[DIMS] = Loc; }
+
+ /// Sets the dims modifier expression.
+ void setDimsModifierExpr(Expr *E) { *varlist_end() = E; }
public:
- /// Build 'num_threads' clause with condition \a NumThreads.
+ /// Creates clause with a list of variables/expressions.
///
- /// \param Modifier Clause modifier.
- /// \param NumThreads Number of threads for the construct.
- /// \param HelperNumThreads Helper Number of threads for the construct.
- /// \param CaptureRegion Innermost OpenMP region where expressions in this
- /// clause must be captured.
+ /// \param C AST context.
+ /// \param CaptureRegion The captured region for the pre-init statements.
/// \param StartLoc Starting location of the clause.
/// \param LParenLoc Location of '('.
- /// \param ModifierLoc Modifier location.
/// \param EndLoc Ending location of the clause.
- OMPNumThreadsClause(OpenMPNumThreadsClauseModifier Modifier, Expr *NumThreads,
- Stmt *HelperNumThreads, OpenMPDirectiveKind CaptureRegion,
- SourceLocation StartLoc, SourceLocation LParenLoc,
- SourceLocation ModifierLoc, SourceLocation EndLoc)
- : OMPOneStmtClause(NumThreads, StartLoc, LParenLoc, EndLoc),
- OMPClauseWithPreInit(this), Modifier(Modifier),
- ModifierLoc(ModifierLoc) {
- setPreInitStmt(HelperNumThreads, CaptureRegion);
+ /// \param VL List of references to the expressions.
+ /// \param PrescriptivenessModifier The prescriptiveness modifier.
+ /// \param DimsModifier The dims modifier.
+ /// \param PrescriptivenessModifierLoc Location of the prescriptiveness modifier.
+ /// \param DimsModifierLoc Location of the dims modifier.
+ /// \param DimsModifierExpr The expression of the number of dimensions.
+ /// \param PreInit
+ static OMPNumThreadsClause *
+ Create(const ASTContext &C, OpenMPDirectiveKind CaptureRegion,
+ SourceLocation StartLoc, SourceLocation LParenLoc,
+ SourceLocation EndLoc, ArrayRef<Expr *> VL,
+ OpenMPNumThreadsClauseModifier PrescriptivenessModifier,
+ OpenMPNumThreadsClauseModifier DimsModifier,
+ SourceLocation PrescriptivenessModifierLoc,
+ SourceLocation DimsModifierLoc, Expr *DimsModifierExpr, Stmt *PreInit);
+
+ /// Creates an empty clause with the place for \a N expressions.
+ ///
+ /// \param C AST context.
+ /// \param N The number of expressions.
+ static OMPNumThreadsClause *CreateEmpty(const ASTContext &C, unsigned N);
+
+ /// Return NumThreads expressions.
+ ArrayRef<Expr *> getNumThreads() { return getVarRefs(); }
+ ArrayRef<Expr *> getNumThreads() const {
+ return const_cast<OMPNumThreadsClause *>(this)->getNumThreads();
}
- /// Build an empty clause.
- OMPNumThreadsClause() : OMPOneStmtClause(), OMPClauseWithPreInit(this) {}
+ /// Returns the prescriptiveness modifier.
+ OpenMPNumThreadsClauseModifier getPrescriptivenessModifier() const {
+ return Modifiers[PRESCRIPTIVENESS];
+ }
+ /// Returns the location of the prescriptiveness modifier.
+ SourceLocation getPrescriptivenessModifierLoc() const {
+ return ModifiersLoc[PRESCRIPTIVENESS];
+ }
- /// Gets modifier.
- OpenMPNumThreadsClauseModifier getModifier() const { return Modifier; }
+ /// Returns the dims modifier.
+ OpenMPNumThreadsClauseModifier getDimsModifier() const {
+ return Modifiers[DIMS];
+ }
+ /// Returns the location of the dims modifier.
+ SourceLocation getDimsModifierLoc() const { return ModifiersLoc[DIMS]; }
- /// Gets modifier location.
- SourceLocation getModifierLoc() const { return ModifierLoc; }
+ /// Checks if the clause has the dims modifier.
+ bool hasDimsModifier() const {
+ return Modifiers[DIMS] == OMPC_NUMTHREADS_dims;
+ }
- /// Returns number of threads.
- Expr *getNumThreads() const { return getStmtAs<Expr>(); }
+ /// Returns the dims modifier expression if present.
+ Expr *getDimsModifierExpr() {
+ return hasDimsModifier() ? *varlist_end() : nullptr;
+ }
+ /// Returns the dims modifier expression if present.
+ const Expr *getDimsModifierExpr() const {
+ return const_cast<OMPNumThreadsClause *>(this)->getDimsModifierExpr();
+ }
+
+ child_range children() {
+ return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
+ reinterpret_cast<Stmt **>(varlist_end()) + 1);
+ }
+ const_child_range children() const {
+ return const_cast<OMPNumThreadsClause *>(this)->children();
+ }
+
+ child_range used_children() {
+ return child_range(child_iterator(), child_iterator());
+ }
+ const_child_range used_children() const {
+ return const_child_range(const_child_iterator(), const_child_iterator());
+ }
+
+ static bool classof(const OMPClause *T) {
+ return T->getClauseKind() == llvm::omp::OMPC_num_threads;
+ }
};
/// This represents 'safelen' clause in the '#pragma omp ...'
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 84aaac71746ed..1c0c182241064 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -3501,8 +3501,10 @@ bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
template <typename Derived>
bool
RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
+ if (auto *E = C->getDimsModifierExpr())
+ TRY_TO(VisitStmt(E));
+ TRY_TO(VisitOMPClauseList(C));
TRY_TO(VisitOMPClauseWithPreInit(C));
- TRY_TO(TraverseStmt(C->getNumThreads()));
return true;
}
diff --git a/clang/include/clang/Basic/OpenMPKinds.def b/clang/include/clang/Basic/OpenMPKinds.def
index 5c396a5d2d4b3..5b5222a2ab446 100644
--- a/clang/include/clang/Basic/OpenMPKinds.def
+++ b/clang/include/clang/Basic/OpenMPKinds.def
@@ -278,8 +278,9 @@ OPENMP_NUMTASKS_MODIFIER(strict)
OPENMP_NUMTEAMS_MODIFIER(lower_bound)
OPENMP_NUMTEAMS_MODIFIER(dims)
-// Modifiers for the 'num_tasks' clause.
+// Modifiers for the 'num_threads' clause.
OPENMP_NUMTHREADS_MODIFIER(strict)
+OPENMP_NUMTHREADS_MODIFIER(dims)
// Modifiers for the 'thread_limit' clause.
OPENMP_THREADLIMIT_MODIFIER(dims)
diff --git a/clang/include/clang/Sema/SemaOpenMP.h b/clang/include/clang/Sema/SemaOpenMP.h
index 9ef388a744db8..93f8bd7f85958 100644
--- a/clang/include/clang/Sema/SemaOpenMP.h
+++ b/clang/include/clang/Sema/SemaOpenMP.h
@@ -898,9 +898,12 @@ class SemaOpenMP : public SemaBase {
SourceLocation EndLoc);
/// Called on well-formed 'num_threads' clause.
OMPClause *ActOnOpenMPNumThreadsClause(
- OpenMPNumThreadsClauseModifier Modifier, Expr *NumThreads,
- SourceLocation StartLoc, SourceLocation LParenLoc,
- SourceLocation ModifierLoc, SourceLocation EndLoc);
+ ArrayRef<Expr *> VarList,
+ OpenMPNumThreadsClauseModifier PrescriptivenessModifier,
+ SourceLocation PrescriptivenessModifierLoc,
+ OpenMPNumThreadsClauseModifier DimsModifier, Expr *DimsModifierExpr,
+ SourceLocation DimsModifierLoc, SourceLocation StartLoc,
+ SourceLocation LParenLoc, SourceLocation EndLoc);
/// Called on well-formed 'align' clause.
OMPClause *ActOnOpenMPAlignClause(Expr *Alignment, SourceLocation StartLoc,
SourceLocation LParenLoc,
diff --git a/clang/lib/AST/OpenMPClause.cpp b/clang/lib/AST/OpenMPClause.cpp
index d451255bf5845..2860a96d4e3e4 100644
--- a/clang/lib/AST/OpenMPClause.cpp
+++ b/clang/lib/AST/OpenMPClause.cpp
@@ -1999,6 +1999,35 @@ OMPThreadLimitClause *OMPThreadLimitClause::CreateEmpty(const ASTContext &C,
return new (Mem) OMPThreadLimitClause(N);
}
+OMPNumThreadsClause *OMPNumThreadsClause::Create(
+ const ASTContext &C, OpenMPDirectiveKind CaptureRegion,
+ SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc,
+ ArrayRef<Expr *> VL,
+ OpenMPNumThreadsClauseModifier PrescriptivenessModifier,
+ OpenMPNumThreadsClauseModifier DimsModifier,
+ SourceLocation PrescriptivenessModifierLoc, SourceLocation DimsModifierLoc,
+ Expr *DimsModifierExpr, Stmt *PreInit) {
+ // Reserve space for an extra modifier expression.
+ void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
+ OMPNumThreadsClause *Clause =
+ new (Mem) OMPNumThreadsClause(C, StartLoc, LParenLoc, EndLoc, VL.size());
+ Clause->setVarRefs(VL);
+ Clause->setPrescriptivenessModifier(PrescriptivenessModifier);
+ Clause->setPrescriptivenessModifierLoc(PrescriptivenessModifierLoc);
+ Clause->setDimsModifier(DimsModifier);
+ Clause->setDimsModifierExpr(DimsModifierExpr);
+ Clause->setDimsModifierLoc(DimsModifierLoc);
+ Clause->setPreInitStmt(PreInit, CaptureRegion);
+ return Clause;
+}
+
+OMPNumThreadsClause *OMPNumThreadsClause::CreateEmpty(const ASTContext &C,
+ unsigned N) {
+ // Reserve space for an extra modifier expression.
+ void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
+ return new (Mem) OMPNumThreadsClause(N);
+}
+
//===----------------------------------------------------------------------===//
// OpenMP clauses printing methods
//===----------------------------------------------------------------------===//
@@ -2018,14 +2047,30 @@ void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
}
void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
- OS << "num_threads(";
- OpenMPNumThreadsClauseModifier Modifier = Node->getModifier();
- if (Modifier != OMPC_NUMTHREADS_unknown) {
- OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
- << ": ";
+ if (!Node->varlist_empty()) {
+ OS << "num_threads";
+ bool HasPrescriptiveness =
+ Node->getPrescriptivenessModifier() != OMPC_NUMTHREADS_unknown;
+ bool HasDims = Node->getDimsModifier() != OMPC_NUMTHREADS_unknown;
+ if (HasPrescriptiveness || HasDims) {
+ OS << "(";
+ if (HasPrescriptiveness)
+ OS << getOpenMPSimpleClauseTypeName(
+ Node->getClauseKind(), Node->getPrescriptivenessModifier());
+ if (HasPrescriptiveness && HasDims)
+ OS << ",";
+ if (HasDims) {
+ OS << "dims(";
+ Node->getDimsModifierExpr()->printPretty(OS, nullptr, Policy, 0);
+ OS << ")";
+ }
+ OS << ":";
+ VisitOMPClauseList(Node, ' ');
+ } else {
+ VisitOMPClauseList(Node, '(');
+ }
+ OS << ")";
}
- Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
- OS << ")";
}
void OMPClausePrinter::VisitOMPAlignClause(OMPAlignClause *Node) {
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index 291c72385518e..0751783483bf6 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -475,9 +475,12 @@ void OMPClauseProfiler::VisitOMPFinalClause(const OMPFinalClause *C) {
}
void OMPClauseProfiler::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
+ Profiler->VisitInteger(C->getPrescriptivenessModifier());
+ Profiler->VisitInteger(C->getDimsModifier());
+ if (const Expr *Modifier = C->getDimsModifierExpr())
+ Profiler->VisitStmt(Modifier);
+ VisitOMPClauseList(C);
VisitOMPClauseWithPreInit(C);
- if (C->getNumThreads())
- Profiler->VisitStmt(C->getNumThreads());
}
void OMPClauseProfiler::VisitOMPAlignClause(const OMPAlignClause *C) {
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 18ed6570730f4..f957eb4d859c9 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -6201,6 +6201,7 @@ Value *ScalarExprEmitter::VisitAtomicExpr(AtomicExpr *E) {
/// Emit the computation of the specified expression of scalar type, ignoring
/// the result.
Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
+ assert(E && "Invalid expression to emit");
assert(E && hasScalarEvaluationKind(E->getType()) &&
"Invalid scalar expression to emit");
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index eb2f92cdbf972..67d575d9d5e0d 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6701,7 +6701,7 @@ static void getNumThreads(CodeGenFunction &CGF, const CapturedStmt *CS,
CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
const auto *NumThreadsClause =
Dir->getSingleClause<OMPNumThreadsClause>();
- const Expr *NTExpr = NumThreadsClause->getNumThreads();
+ const Expr *NTExpr = NumThreadsClause->getNumThreads().front();
if (NTExpr->isIntegerConstantExpr(CGF.getContext()))
if (auto Constant = NTExpr->getIntegerConstantExpr(CGF.getContext()))
UpperBound =
@@ -6887,8 +6887,8 @@ const Expr *CGOpenMPRuntime::getNumThreadsExprForTargetDirective(
if (D.hasClausesOfKind<OMPNumThreadsClause>()) {
CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
const auto *NumThreadsClause = D.getSingleClause<OMPNumThreadsClause>();
- CheckForConstExpr(NumThreadsClause->getNumThreads(), nullptr);
- return NumThreadsClause->getNumThreads();
+ CheckForConstExpr(NumThreadsClause->getNumThreads().front(), nullptr);
+ return NumThreadsClause->getNumThreads().front();
}
return NT;
}
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 88f698f38cce0..9174f6c63a751 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -1913,9 +1913,9 @@ static void emitCommonOMPParallelDirective(
if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) {
CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
- NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
+ NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads().front(),
/*IgnoreResultAssign=*/true);
- Modifier = NumThreadsClause->getModifier();
+ Modifier = NumThreadsClause->getPrescriptivenessModifier();
if (const auto *MessageClause = S.getSingleClause<OMPMessageClause>()) {
Message = MessageClause->getMessageString();
MessageLoc = MessageClause->getBeginLoc();
@@ -2113,7 +2113,7 @@ void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
llvm::Value *NumThreads = nullptr;
if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>())
- NumThreads = EmitScalarExpr(NumThreadsClause->getNumThreads(),
+ NumThreads = EmitScalarExpr(NumThreadsClause->getNumThreads().front(),
/*IgnoreResultAssign=*/true);
ProcBindKind ProcBind = OMP_PROC_BIND_default;
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 7ef1e4211a1ef..4e19082f8f814 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -3231,7 +3231,6 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
switch (CKind) {
case OMPC_final:
- case OMPC_num_threads:
case OMPC_safelen:
case OMPC_simdlen:
case OMPC_collapse:
@@ -3297,7 +3296,7 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Clause = ParseOpenMPClause(CKind, WrongDirective);
else if (CKind == OMPC_grainsize || CKind == OMPC_num_tasks ||
- CKind == OMPC_num_threads || CKind == OMPC_dyn_groupprivate)
+ CKind == OMPC_dyn_groupprivate)
Clause = ParseOpenMPSingleExprWithArgClause(DKind, CKind, WrongDirective);
else
Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
@@ -3428,6 +3427,7 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
break;
case OMPC_num_teams:
case OMPC_thread_limit:
+ case OMPC_num_threads:
if (!FirstClause) {
Diag(Tok, diag::err_omp_more_one_clause)
<< getOpenMPDirectiveName(DKind, OMPVersion)
@@ -4331,33 +4331,6 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
Arg.push_back(OMPC_NUMTASKS_unknown);
KLoc.emplace_back();
}
- } else if (Kind == OMPC_num_threads) {
- // Parse optional <num_threads modifier> ':'
- OpenMPNumThreadsClauseModifier Modifier =
- static_cast<OpenMPNumThreadsClauseModifier>(getO...
[truncated]
|
|
@llvm/pr-subscribers-clang-modules Author: Kevin Sala Penades (kevinsala) ChangesAdd parsing for constexpr int N = 2;
#pragma omp parallel num_threads(dims(N),strict: a, b)
{ ... }Patch is 83.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/208353.diff 31 Files Affected:
diff --git a/clang/include/clang/AST/OpenMPClause.h b/clang/include/clang/AST/OpenMPClause.h
index 641c03bf8ff5c..a3048709ccd8c 100644
--- a/clang/include/clang/AST/OpenMPClause.h
+++ b/clang/include/clang/AST/OpenMPClause.h
@@ -804,67 +804,175 @@ class OMPFinalClause final
return const_cast<OMPFinalClause *>(this)->used_children();
}
};
+
/// This represents 'num_threads' clause in the '#pragma omp ...'
/// directive.
///
/// \code
-/// #pragma omp parallel num_threads(6)
+/// #pragma omp parallel num_threads(n)
+/// \endcode
+/// In this example directive '#pragma omp parallel' has 'num_threads' clause
+/// requesting 'n' threads.
+///
+/// \code
+/// #pragma omp parallel num_threads(strict: n)
/// \endcode
-/// In this example directive '#pragma omp parallel' has simple 'num_threads'
-/// clause with number of threads '6'.
+/// In this example directive '#pragma omp parallel' has 'num_threads' clause
+/// requesting 'n' threads strictly with the 'strict' modifier.
+///
+/// \code
+/// #pragma omp parallel num_threads(dims(2): x, y)
+/// \endcode
+/// In this example directive '#pragma omp parallel' has clause 'num_threads'
+/// with the 'dims' modifier specifying two dimensions. The list specifies the
+/// number of threads in each dimension.
class OMPNumThreadsClause final
- : public OMPOneStmtClause<llvm::omp::OMPC_num_threads, OMPClause>,
- public OMPClauseWithPreInit {
+ : public OMPVarListClause<OMPNumThreadsClause>,
+ public OMPClauseWithPreInit,
+ private llvm::TrailingObjects<OMPNumThreadsClause, Expr *> {
+ friend class OMPVarListClause<OMPNumThreadsClause>;
+ friend TrailingObjects;
friend class OMPClauseReader;
- /// Modifiers for 'num_threads' clause.
- OpenMPNumThreadsClauseModifier Modifier = OMPC_NUMTHREADS_unknown;
+private:
+ /// Categories of modifiers for the 'num_threads' clause.
+ enum { PRESCRIPTIVENESS, DIMS, NUM_MODIFIERS };
- /// Location of the modifier.
- SourceLocation ModifierLoc;
+ /// Modifiers for the clause, indexed by category.
+ OpenMPNumThreadsClauseModifier Modifiers[NUM_MODIFIERS] = {
+ OMPC_NUMTHREADS_unknown, OMPC_NUMTHREADS_unknown};
- /// Sets modifier.
- void setModifier(OpenMPNumThreadsClauseModifier M) { Modifier = M; }
+ /// Locations of the modifiers, indexed by category.
+ SourceLocation ModifiersLoc[NUM_MODIFIERS];
- /// Sets modifier location.
- void setModifierLoc(SourceLocation Loc) { ModifierLoc = Loc; }
+ /// Build clause with number of expressions \a N.
+ ///
+ /// \param C AST context.
+ /// \param StartLoc Starting location of the clause.
+ /// \param LParenLoc Location of '('.
+ /// \param EndLoc Ending location of the clause.
+ /// \param N Number of expressions.
+ OMPNumThreadsClause(const ASTContext &C, SourceLocation StartLoc,
+ SourceLocation LParenLoc, SourceLocation EndLoc,
+ unsigned N)
+ : OMPVarListClause<OMPNumThreadsClause>(llvm::omp::OMPC_num_threads,
+ StartLoc, LParenLoc, EndLoc, N),
+ OMPClauseWithPreInit(this) {}
- /// Set condition.
- void setNumThreads(Expr *NThreads) { setStmt(NThreads); }
+ /// Build an empty clause.
+ ///
+ /// \param N Number of expressions.
+ explicit OMPNumThreadsClause(unsigned N)
+ : OMPVarListClause<OMPNumThreadsClause>(
+ llvm::omp::OMPC_num_threads, SourceLocation(), SourceLocation(),
+ SourceLocation(), N),
+ OMPClauseWithPreInit(this) {}
+
+ /// Sets the dims modifier.
+ void setDimsModifier(OpenMPNumThreadsClauseModifier M) {
+ Modifiers[DIMS] = M;
+ }
+
+ /// Sets the prescriptiveness modifier.
+ void setPrescriptivenessModifier(OpenMPNumThreadsClauseModifier M) {
+ Modifiers[PRESCRIPTIVENESS] = M;
+ }
+
+ /// Sets the location of the prescriptiveness modifier.
+ void setPrescriptivenessModifierLoc(SourceLocation Loc) {
+ ModifiersLoc[PRESCRIPTIVENESS] = Loc;
+ }
+
+ /// Sets the location of the dims modifier.
+ void setDimsModifierLoc(SourceLocation Loc) { ModifiersLoc[DIMS] = Loc; }
+
+ /// Sets the dims modifier expression.
+ void setDimsModifierExpr(Expr *E) { *varlist_end() = E; }
public:
- /// Build 'num_threads' clause with condition \a NumThreads.
+ /// Creates clause with a list of variables/expressions.
///
- /// \param Modifier Clause modifier.
- /// \param NumThreads Number of threads for the construct.
- /// \param HelperNumThreads Helper Number of threads for the construct.
- /// \param CaptureRegion Innermost OpenMP region where expressions in this
- /// clause must be captured.
+ /// \param C AST context.
+ /// \param CaptureRegion The captured region for the pre-init statements.
/// \param StartLoc Starting location of the clause.
/// \param LParenLoc Location of '('.
- /// \param ModifierLoc Modifier location.
/// \param EndLoc Ending location of the clause.
- OMPNumThreadsClause(OpenMPNumThreadsClauseModifier Modifier, Expr *NumThreads,
- Stmt *HelperNumThreads, OpenMPDirectiveKind CaptureRegion,
- SourceLocation StartLoc, SourceLocation LParenLoc,
- SourceLocation ModifierLoc, SourceLocation EndLoc)
- : OMPOneStmtClause(NumThreads, StartLoc, LParenLoc, EndLoc),
- OMPClauseWithPreInit(this), Modifier(Modifier),
- ModifierLoc(ModifierLoc) {
- setPreInitStmt(HelperNumThreads, CaptureRegion);
+ /// \param VL List of references to the expressions.
+ /// \param PrescriptivenessModifier The prescriptiveness modifier.
+ /// \param DimsModifier The dims modifier.
+ /// \param PrescriptivenessModifierLoc Location of the prescriptiveness modifier.
+ /// \param DimsModifierLoc Location of the dims modifier.
+ /// \param DimsModifierExpr The expression of the number of dimensions.
+ /// \param PreInit
+ static OMPNumThreadsClause *
+ Create(const ASTContext &C, OpenMPDirectiveKind CaptureRegion,
+ SourceLocation StartLoc, SourceLocation LParenLoc,
+ SourceLocation EndLoc, ArrayRef<Expr *> VL,
+ OpenMPNumThreadsClauseModifier PrescriptivenessModifier,
+ OpenMPNumThreadsClauseModifier DimsModifier,
+ SourceLocation PrescriptivenessModifierLoc,
+ SourceLocation DimsModifierLoc, Expr *DimsModifierExpr, Stmt *PreInit);
+
+ /// Creates an empty clause with the place for \a N expressions.
+ ///
+ /// \param C AST context.
+ /// \param N The number of expressions.
+ static OMPNumThreadsClause *CreateEmpty(const ASTContext &C, unsigned N);
+
+ /// Return NumThreads expressions.
+ ArrayRef<Expr *> getNumThreads() { return getVarRefs(); }
+ ArrayRef<Expr *> getNumThreads() const {
+ return const_cast<OMPNumThreadsClause *>(this)->getNumThreads();
}
- /// Build an empty clause.
- OMPNumThreadsClause() : OMPOneStmtClause(), OMPClauseWithPreInit(this) {}
+ /// Returns the prescriptiveness modifier.
+ OpenMPNumThreadsClauseModifier getPrescriptivenessModifier() const {
+ return Modifiers[PRESCRIPTIVENESS];
+ }
+ /// Returns the location of the prescriptiveness modifier.
+ SourceLocation getPrescriptivenessModifierLoc() const {
+ return ModifiersLoc[PRESCRIPTIVENESS];
+ }
- /// Gets modifier.
- OpenMPNumThreadsClauseModifier getModifier() const { return Modifier; }
+ /// Returns the dims modifier.
+ OpenMPNumThreadsClauseModifier getDimsModifier() const {
+ return Modifiers[DIMS];
+ }
+ /// Returns the location of the dims modifier.
+ SourceLocation getDimsModifierLoc() const { return ModifiersLoc[DIMS]; }
- /// Gets modifier location.
- SourceLocation getModifierLoc() const { return ModifierLoc; }
+ /// Checks if the clause has the dims modifier.
+ bool hasDimsModifier() const {
+ return Modifiers[DIMS] == OMPC_NUMTHREADS_dims;
+ }
- /// Returns number of threads.
- Expr *getNumThreads() const { return getStmtAs<Expr>(); }
+ /// Returns the dims modifier expression if present.
+ Expr *getDimsModifierExpr() {
+ return hasDimsModifier() ? *varlist_end() : nullptr;
+ }
+ /// Returns the dims modifier expression if present.
+ const Expr *getDimsModifierExpr() const {
+ return const_cast<OMPNumThreadsClause *>(this)->getDimsModifierExpr();
+ }
+
+ child_range children() {
+ return child_range(reinterpret_cast<Stmt **>(varlist_begin()),
+ reinterpret_cast<Stmt **>(varlist_end()) + 1);
+ }
+ const_child_range children() const {
+ return const_cast<OMPNumThreadsClause *>(this)->children();
+ }
+
+ child_range used_children() {
+ return child_range(child_iterator(), child_iterator());
+ }
+ const_child_range used_children() const {
+ return const_child_range(const_child_iterator(), const_child_iterator());
+ }
+
+ static bool classof(const OMPClause *T) {
+ return T->getClauseKind() == llvm::omp::OMPC_num_threads;
+ }
};
/// This represents 'safelen' clause in the '#pragma omp ...'
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 84aaac71746ed..1c0c182241064 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -3501,8 +3501,10 @@ bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) {
template <typename Derived>
bool
RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
+ if (auto *E = C->getDimsModifierExpr())
+ TRY_TO(VisitStmt(E));
+ TRY_TO(VisitOMPClauseList(C));
TRY_TO(VisitOMPClauseWithPreInit(C));
- TRY_TO(TraverseStmt(C->getNumThreads()));
return true;
}
diff --git a/clang/include/clang/Basic/OpenMPKinds.def b/clang/include/clang/Basic/OpenMPKinds.def
index 5c396a5d2d4b3..5b5222a2ab446 100644
--- a/clang/include/clang/Basic/OpenMPKinds.def
+++ b/clang/include/clang/Basic/OpenMPKinds.def
@@ -278,8 +278,9 @@ OPENMP_NUMTASKS_MODIFIER(strict)
OPENMP_NUMTEAMS_MODIFIER(lower_bound)
OPENMP_NUMTEAMS_MODIFIER(dims)
-// Modifiers for the 'num_tasks' clause.
+// Modifiers for the 'num_threads' clause.
OPENMP_NUMTHREADS_MODIFIER(strict)
+OPENMP_NUMTHREADS_MODIFIER(dims)
// Modifiers for the 'thread_limit' clause.
OPENMP_THREADLIMIT_MODIFIER(dims)
diff --git a/clang/include/clang/Sema/SemaOpenMP.h b/clang/include/clang/Sema/SemaOpenMP.h
index 9ef388a744db8..93f8bd7f85958 100644
--- a/clang/include/clang/Sema/SemaOpenMP.h
+++ b/clang/include/clang/Sema/SemaOpenMP.h
@@ -898,9 +898,12 @@ class SemaOpenMP : public SemaBase {
SourceLocation EndLoc);
/// Called on well-formed 'num_threads' clause.
OMPClause *ActOnOpenMPNumThreadsClause(
- OpenMPNumThreadsClauseModifier Modifier, Expr *NumThreads,
- SourceLocation StartLoc, SourceLocation LParenLoc,
- SourceLocation ModifierLoc, SourceLocation EndLoc);
+ ArrayRef<Expr *> VarList,
+ OpenMPNumThreadsClauseModifier PrescriptivenessModifier,
+ SourceLocation PrescriptivenessModifierLoc,
+ OpenMPNumThreadsClauseModifier DimsModifier, Expr *DimsModifierExpr,
+ SourceLocation DimsModifierLoc, SourceLocation StartLoc,
+ SourceLocation LParenLoc, SourceLocation EndLoc);
/// Called on well-formed 'align' clause.
OMPClause *ActOnOpenMPAlignClause(Expr *Alignment, SourceLocation StartLoc,
SourceLocation LParenLoc,
diff --git a/clang/lib/AST/OpenMPClause.cpp b/clang/lib/AST/OpenMPClause.cpp
index d451255bf5845..2860a96d4e3e4 100644
--- a/clang/lib/AST/OpenMPClause.cpp
+++ b/clang/lib/AST/OpenMPClause.cpp
@@ -1999,6 +1999,35 @@ OMPThreadLimitClause *OMPThreadLimitClause::CreateEmpty(const ASTContext &C,
return new (Mem) OMPThreadLimitClause(N);
}
+OMPNumThreadsClause *OMPNumThreadsClause::Create(
+ const ASTContext &C, OpenMPDirectiveKind CaptureRegion,
+ SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc,
+ ArrayRef<Expr *> VL,
+ OpenMPNumThreadsClauseModifier PrescriptivenessModifier,
+ OpenMPNumThreadsClauseModifier DimsModifier,
+ SourceLocation PrescriptivenessModifierLoc, SourceLocation DimsModifierLoc,
+ Expr *DimsModifierExpr, Stmt *PreInit) {
+ // Reserve space for an extra modifier expression.
+ void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
+ OMPNumThreadsClause *Clause =
+ new (Mem) OMPNumThreadsClause(C, StartLoc, LParenLoc, EndLoc, VL.size());
+ Clause->setVarRefs(VL);
+ Clause->setPrescriptivenessModifier(PrescriptivenessModifier);
+ Clause->setPrescriptivenessModifierLoc(PrescriptivenessModifierLoc);
+ Clause->setDimsModifier(DimsModifier);
+ Clause->setDimsModifierExpr(DimsModifierExpr);
+ Clause->setDimsModifierLoc(DimsModifierLoc);
+ Clause->setPreInitStmt(PreInit, CaptureRegion);
+ return Clause;
+}
+
+OMPNumThreadsClause *OMPNumThreadsClause::CreateEmpty(const ASTContext &C,
+ unsigned N) {
+ // Reserve space for an extra modifier expression.
+ void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
+ return new (Mem) OMPNumThreadsClause(N);
+}
+
//===----------------------------------------------------------------------===//
// OpenMP clauses printing methods
//===----------------------------------------------------------------------===//
@@ -2018,14 +2047,30 @@ void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
}
void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
- OS << "num_threads(";
- OpenMPNumThreadsClauseModifier Modifier = Node->getModifier();
- if (Modifier != OMPC_NUMTHREADS_unknown) {
- OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
- << ": ";
+ if (!Node->varlist_empty()) {
+ OS << "num_threads";
+ bool HasPrescriptiveness =
+ Node->getPrescriptivenessModifier() != OMPC_NUMTHREADS_unknown;
+ bool HasDims = Node->getDimsModifier() != OMPC_NUMTHREADS_unknown;
+ if (HasPrescriptiveness || HasDims) {
+ OS << "(";
+ if (HasPrescriptiveness)
+ OS << getOpenMPSimpleClauseTypeName(
+ Node->getClauseKind(), Node->getPrescriptivenessModifier());
+ if (HasPrescriptiveness && HasDims)
+ OS << ",";
+ if (HasDims) {
+ OS << "dims(";
+ Node->getDimsModifierExpr()->printPretty(OS, nullptr, Policy, 0);
+ OS << ")";
+ }
+ OS << ":";
+ VisitOMPClauseList(Node, ' ');
+ } else {
+ VisitOMPClauseList(Node, '(');
+ }
+ OS << ")";
}
- Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
- OS << ")";
}
void OMPClausePrinter::VisitOMPAlignClause(OMPAlignClause *Node) {
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index 291c72385518e..0751783483bf6 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -475,9 +475,12 @@ void OMPClauseProfiler::VisitOMPFinalClause(const OMPFinalClause *C) {
}
void OMPClauseProfiler::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
+ Profiler->VisitInteger(C->getPrescriptivenessModifier());
+ Profiler->VisitInteger(C->getDimsModifier());
+ if (const Expr *Modifier = C->getDimsModifierExpr())
+ Profiler->VisitStmt(Modifier);
+ VisitOMPClauseList(C);
VisitOMPClauseWithPreInit(C);
- if (C->getNumThreads())
- Profiler->VisitStmt(C->getNumThreads());
}
void OMPClauseProfiler::VisitOMPAlignClause(const OMPAlignClause *C) {
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 18ed6570730f4..f957eb4d859c9 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -6201,6 +6201,7 @@ Value *ScalarExprEmitter::VisitAtomicExpr(AtomicExpr *E) {
/// Emit the computation of the specified expression of scalar type, ignoring
/// the result.
Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
+ assert(E && "Invalid expression to emit");
assert(E && hasScalarEvaluationKind(E->getType()) &&
"Invalid scalar expression to emit");
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index eb2f92cdbf972..67d575d9d5e0d 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6701,7 +6701,7 @@ static void getNumThreads(CodeGenFunction &CGF, const CapturedStmt *CS,
CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
const auto *NumThreadsClause =
Dir->getSingleClause<OMPNumThreadsClause>();
- const Expr *NTExpr = NumThreadsClause->getNumThreads();
+ const Expr *NTExpr = NumThreadsClause->getNumThreads().front();
if (NTExpr->isIntegerConstantExpr(CGF.getContext()))
if (auto Constant = NTExpr->getIntegerConstantExpr(CGF.getContext()))
UpperBound =
@@ -6887,8 +6887,8 @@ const Expr *CGOpenMPRuntime::getNumThreadsExprForTargetDirective(
if (D.hasClausesOfKind<OMPNumThreadsClause>()) {
CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
const auto *NumThreadsClause = D.getSingleClause<OMPNumThreadsClause>();
- CheckForConstExpr(NumThreadsClause->getNumThreads(), nullptr);
- return NumThreadsClause->getNumThreads();
+ CheckForConstExpr(NumThreadsClause->getNumThreads().front(), nullptr);
+ return NumThreadsClause->getNumThreads().front();
}
return NT;
}
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 88f698f38cce0..9174f6c63a751 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -1913,9 +1913,9 @@ static void emitCommonOMPParallelDirective(
if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) {
CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
- NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
+ NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads().front(),
/*IgnoreResultAssign=*/true);
- Modifier = NumThreadsClause->getModifier();
+ Modifier = NumThreadsClause->getPrescriptivenessModifier();
if (const auto *MessageClause = S.getSingleClause<OMPMessageClause>()) {
Message = MessageClause->getMessageString();
MessageLoc = MessageClause->getBeginLoc();
@@ -2113,7 +2113,7 @@ void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
llvm::Value *NumThreads = nullptr;
if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>())
- NumThreads = EmitScalarExpr(NumThreadsClause->getNumThreads(),
+ NumThreads = EmitScalarExpr(NumThreadsClause->getNumThreads().front(),
/*IgnoreResultAssign=*/true);
ProcBindKind ProcBind = OMP_PROC_BIND_default;
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 7ef1e4211a1ef..4e19082f8f814 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -3231,7 +3231,6 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
switch (CKind) {
case OMPC_final:
- case OMPC_num_threads:
case OMPC_safelen:
case OMPC_simdlen:
case OMPC_collapse:
@@ -3297,7 +3296,7 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Clause = ParseOpenMPClause(CKind, WrongDirective);
else if (CKind == OMPC_grainsize || CKind == OMPC_num_tasks ||
- CKind == OMPC_num_threads || CKind == OMPC_dyn_groupprivate)
+ CKind == OMPC_dyn_groupprivate)
Clause = ParseOpenMPSingleExprWithArgClause(DKind, CKind, WrongDirective);
else
Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
@@ -3428,6 +3427,7 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
break;
case OMPC_num_teams:
case OMPC_thread_limit:
+ case OMPC_num_threads:
if (!FirstClause) {
Diag(Tok, diag::err_omp_more_one_clause)
<< getOpenMPDirectiveName(DKind, OMPVersion)
@@ -4331,33 +4331,6 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
Arg.push_back(OMPC_NUMTASKS_unknown);
KLoc.emplace_back();
}
- } else if (Kind == OMPC_num_threads) {
- // Parse optional <num_threads modifier> ':'
- OpenMPNumThreadsClauseModifier Modifier =
- static_cast<OpenMPNumThreadsClauseModifier>(getO...
[truncated]
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
d8ab549 to
62e4015
Compare
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
ebe2afb to
84f97c3
Compare
6a0194e to
dd80482
Compare
84f97c3 to
34b57bf
Compare
| if (ModifierLoc.isValid() && Modifier == OMPC_NUMTHREADS_unknown) { | ||
| std::string Values = getListOfPossibleValues(OMPC_num_threads, /*First=*/0, | ||
| OMPC_NUMTHREADS_unknown); | ||
| Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) | ||
| << Values << getOpenMPClauseNameForDiag(OMPC_num_threads); | ||
| return nullptr; | ||
| ArrayRef<Expr *> VarList, | ||
| OpenMPNumThreadsClauseModifier PrescriptivenessModifier, | ||
| SourceLocation PrescriptivenessModifierLoc, | ||
| OpenMPNumThreadsClauseModifier DimsModifier, Expr *DimsModifierExpr, | ||
| SourceLocation DimsModifierLoc, SourceLocation StartLoc, | ||
| SourceLocation LParenLoc, SourceLocation EndLoc) { | ||
| SmallVector<Expr *, 3> Vars(VarList.begin(), VarList.end()); | ||
| for (Expr *&ValExpr : Vars) { | ||
| // OpenMP [2.5, Restrictions] | ||
| // The num_threads expression must evaluate to a positive integer value. | ||
| if (!isNonNegativeIntegerValue(ValExpr, SemaRef, OMPC_num_threads, | ||
| /*StrictlyPositive=*/true)) | ||
| return nullptr; | ||
| } |
There was a problem hiding this comment.
Restore a sema check when a token was parsed as modifier prefix but is not strict/dims, or reject unknown identifiers in the modifier loop before the varlist.
There was a problem hiding this comment.
Unknown identifiers in the modifiers are already handled by the parsing side. We only store modifiers and their locations if they are recognized. Unknown identifiers will fail when being parsed as part of the varlist in ParseOpenMPVarList.
In the sema side, I can add the asserts below. If these conditions are not true, it's an internal error.
assert((PrescriptivenessModifier == (PrescriptivenessModifierLoc.isValid() ? OMPC_NUMTHREADS_strict : OMPC_NUMTHREADS_unknown)) &&
"Unexpected num_threads prescriptiveness modifier.");
assert((DimsModifier == (DimsModifierLoc.isValid() ? OMPC_NUMTHREADS_dims : OMPC_NUMTHREADS_unknown)) &&
"Unexpected num_threads dims modifier.");Are you OK with adding the asserts?
There was a problem hiding this comment.
This is usually done in sema, not in parsing
There was a problem hiding this comment.
@alexey-bataev Sorry for the late response.
I've updated the PR with the latest commit, moving more checks from parsing to sema. Unknown modifiers are now checked in sema. The dims modifier still needs to be identified in parsing, as it is a complex modifier that requires a parameter within the parenthesis.
There was a problem hiding this comment.
I think it still must be analyzed in Sema. You just need to parse all expressions and then in sema compare the number of parsed expressions and the dims modifier expression value
9799de7 to
b921292
Compare
9cfb4b6 to
3d25d7d
Compare
0bad201 to
45f81bc
Compare
…vm#208353) Add parsing for `dims` modifier (OpenMP 6.1) in `num_threads` clause. Examples: ```cpp constexpr int N = 2; #pragma omp parallel num_threads(dims(N),strict: a, b) { ... } ``` Assisted by Gemini 3.1 Pro.
Depends on #206412.
Add parsing for
dimsmodifier (OpenMP 6.1) innum_threadsclause. Examples:Assisted by Gemini 3.1 Pro.