Skip to content

Commit

Permalink
[OpenMP] Initial parsing/sema for 'strict' modifier with 'grainsize' …
Browse files Browse the repository at this point in the history
…clause

This patch gives basic parsing and semantic analysis support for 'strict'
modifier with 'grainsize' clause of 'taskloop' construct introduced in
OpenMP 5.1 (section 2.12.2)

Differential Revision: https://reviews.llvm.org/D138217
  • Loading branch information
mdfazlay authored and mikerice1969 committed Nov 18, 2022
1 parent 47e953e commit ab9eac7
Show file tree
Hide file tree
Showing 24 changed files with 423 additions and 26 deletions.
31 changes: 27 additions & 4 deletions clang/include/clang/AST/OpenMPClause.h
Expand Up @@ -6354,26 +6354,43 @@ class OMPGrainsizeClause : public OMPClause, public OMPClauseWithPreInit {
/// Location of '('.
SourceLocation LParenLoc;

/// Modifiers for 'grainsize' clause.
OpenMPGrainsizeClauseModifier Modifier = OMPC_GRAINSIZE_unknown;

/// Location of the modifier.
SourceLocation ModifierLoc;

/// Safe iteration space distance.
Stmt *Grainsize = nullptr;

/// Set safelen.
void setGrainsize(Expr *Size) { Grainsize = Size; }

/// Sets modifier.
void setModifier(OpenMPGrainsizeClauseModifier M) { Modifier = M; }

/// Sets modifier location.
void setModifierLoc(SourceLocation Loc) { ModifierLoc = Loc; }

public:
/// Build 'grainsize' clause.
///
/// \param Modifier Clause modifier.
/// \param Size Expression associated with this clause.
/// \param HelperSize Helper grainsize for the construct.
/// \param CaptureRegion Innermost OpenMP region where expressions in this
/// clause must be captured.
/// \param StartLoc Starting location of the clause.
/// \param ModifierLoc Modifier location.
/// \param LParenLoc Location of '('.
/// \param EndLoc Ending location of the clause.
OMPGrainsizeClause(Expr *Size, Stmt *HelperSize,
OpenMPDirectiveKind CaptureRegion, SourceLocation StartLoc,
SourceLocation LParenLoc, SourceLocation EndLoc)
OMPGrainsizeClause(OpenMPGrainsizeClauseModifier Modifier, Expr *Size,
Stmt *HelperSize, OpenMPDirectiveKind CaptureRegion,
SourceLocation StartLoc, SourceLocation LParenLoc,
SourceLocation ModifierLoc, SourceLocation EndLoc)
: OMPClause(llvm::omp::OMPC_grainsize, StartLoc, EndLoc),
OMPClauseWithPreInit(this), LParenLoc(LParenLoc), Grainsize(Size) {
OMPClauseWithPreInit(this), LParenLoc(LParenLoc), Modifier(Modifier),
ModifierLoc(ModifierLoc), Grainsize(Size) {
setPreInitStmt(HelperSize, CaptureRegion);
}

Expand All @@ -6392,6 +6409,12 @@ class OMPGrainsizeClause : public OMPClause, public OMPClauseWithPreInit {
/// Return safe iteration space distance.
Expr *getGrainsize() const { return cast_or_null<Expr>(Grainsize); }

/// Gets modifier.
OpenMPGrainsizeClauseModifier getModifier() const { return Modifier; }

/// Gets modifier location.
SourceLocation getModifierLoc() const { return ModifierLoc; }

child_range children() { return child_range(&Grainsize, &Grainsize + 1); }

const_child_range children() const {
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/DiagnosticParseKinds.td
Expand Up @@ -1316,6 +1316,7 @@ def warn_pragma_unsupported_extension : Warning<
def warn_pragma_extension_is_core : Warning<
"OpenCL extension %0 is core feature or supported optional core feature - ignoring">,
InGroup<OpenCLCoreFeaturesDiagGroup>, DefaultIgnore;
def err_modifier_expected_colon : Error<"missing ':' after %0 modifier">;

// OpenCL errors.
def err_opencl_taking_function_address_parser : Error<
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/OpenMPKinds.def
Expand Up @@ -71,6 +71,9 @@
#ifndef OPENMP_BIND_KIND
#define OPENMP_BIND_KIND(Name)
#endif
#ifndef OPENMP_GRAINSIZE_MODIFIER
#define OPENMP_GRAINSIZE_MODIFIER(Name)
#endif

// Static attributes for 'schedule' clause.
OPENMP_SCHEDULE_KIND(static)
Expand Down Expand Up @@ -181,6 +184,10 @@ OPENMP_BIND_KIND(teams)
OPENMP_BIND_KIND(parallel)
OPENMP_BIND_KIND(thread)

// Modifiers for the 'grainsize' clause.
OPENMP_GRAINSIZE_MODIFIER(strict)

#undef OPENMP_GRAINSIZE_MODIFIER
#undef OPENMP_BIND_KIND
#undef OPENMP_ADJUST_ARGS_KIND
#undef OPENMP_REDUCTION_MODIFIER
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Basic/OpenMPKinds.h
Expand Up @@ -195,6 +195,12 @@ enum OpenMPBindClauseKind {
OMPC_BIND_unknown
};

enum OpenMPGrainsizeClauseModifier {
#define OPENMP_GRAINSIZE_MODIFIER(Name) OMPC_GRAINSIZE_##Name,
#include "clang/Basic/OpenMPKinds.def"
OMPC_GRAINSIZE_unknown
};

/// Contains 'interop' data for 'append_args' and 'init' clauses.
class Expr;
struct OMPInteropInfo final {
Expand Down
4 changes: 3 additions & 1 deletion clang/include/clang/Sema/Sema.h
Expand Up @@ -11679,8 +11679,10 @@ class Sema final {
SourceLocation LParenLoc = SourceLocation(),
Expr *NumForLoops = nullptr);
/// Called on well-formed 'grainsize' clause.
OMPClause *ActOnOpenMPGrainsizeClause(Expr *Size, SourceLocation StartLoc,
OMPClause *ActOnOpenMPGrainsizeClause(OpenMPGrainsizeClauseModifier Modifier,
Expr *Size, SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation ModifierLoc,
SourceLocation EndLoc);
/// Called on well-formed 'num_tasks' clause.
OMPClause *ActOnOpenMPNumTasksClause(Expr *NumTasks, SourceLocation StartLoc,
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/OpenMPClause.cpp
Expand Up @@ -1920,6 +1920,11 @@ void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {

void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
OS << "grainsize(";
OpenMPGrainsizeClauseModifier Modifier = Node->getModifier();
if (Modifier != OMPC_GRAINSIZE_unknown) {
OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
<< ": ";
}
Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
OS << ")";
}
Expand Down
21 changes: 19 additions & 2 deletions clang/lib/Basic/OpenMPKinds.cpp
Expand Up @@ -149,6 +149,15 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
#define OPENMP_BIND_KIND(Name) .Case(#Name, OMPC_BIND_##Name)
#include "clang/Basic/OpenMPKinds.def"
.Default(OMPC_BIND_unknown);
case OMPC_grainsize: {
unsigned Type = llvm::StringSwitch<unsigned>(Str)
#define OPENMP_GRAINSIZE_MODIFIER(Name) .Case(#Name, OMPC_GRAINSIZE_##Name)
#include "clang/Basic/OpenMPKinds.def"
.Default(OMPC_GRAINSIZE_unknown);
if (LangOpts.OpenMP < 51)
return OMPC_GRAINSIZE_unknown;
return Type;
}
case OMPC_unknown:
case OMPC_threadprivate:
case OMPC_if:
Expand Down Expand Up @@ -188,7 +197,6 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
case OMPC_num_teams:
case OMPC_thread_limit:
case OMPC_priority:
case OMPC_grainsize:
case OMPC_nogroup:
case OMPC_num_tasks:
case OMPC_hint:
Expand Down Expand Up @@ -436,6 +444,16 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
#include "clang/Basic/OpenMPKinds.def"
}
llvm_unreachable("Invalid OpenMP 'bind' clause type");
case OMPC_grainsize:
switch (Type) {
case OMPC_GRAINSIZE_unknown:
return "unknown";
#define OPENMP_GRAINSIZE_MODIFIER(Name) \
case OMPC_GRAINSIZE_##Name: \
return #Name;
#include "clang/Basic/OpenMPKinds.def"
}
llvm_unreachable("Invalid OpenMP 'grainsize' clause modifier");
case OMPC_unknown:
case OMPC_threadprivate:
case OMPC_if:
Expand Down Expand Up @@ -475,7 +493,6 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
case OMPC_num_teams:
case OMPC_thread_limit:
case OMPC_priority:
case OMPC_grainsize:
case OMPC_nogroup:
case OMPC_num_tasks:
case OMPC_hint:
Expand Down
32 changes: 31 additions & 1 deletion clang/lib/Parse/ParseOpenMP.cpp
Expand Up @@ -3226,6 +3226,8 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
if ((CKind == OMPC_ordered || CKind == OMPC_partial) &&
PP.LookAhead(/*N=*/0).isNot(tok::l_paren))
Clause = ParseOpenMPClause(CKind, WrongDirective);
else if (CKind == OMPC_grainsize)
Clause = ParseOpenMPSingleExprWithArgClause(DKind, CKind, WrongDirective);
else
Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
break;
Expand Down Expand Up @@ -3871,6 +3873,33 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
Arg.push_back(OMPC_DEVICE_unknown);
KLoc.emplace_back();
}
} else if (Kind == OMPC_grainsize) {
// Parse optional <grainsize modifier> ':'
OpenMPGrainsizeClauseModifier Modifier =
static_cast<OpenMPGrainsizeClauseModifier>(getOpenMPSimpleClauseType(
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok),
getLangOpts()));
if (getLangOpts().OpenMP >= 51) {
if (NextToken().is(tok::colon)) {
Arg.push_back(Modifier);
KLoc.push_back(Tok.getLocation());
// Parse modifier
ConsumeAnyToken();
// Parse ':'
ConsumeAnyToken();
} else {
if (Modifier == OMPC_GRAINSIZE_strict) {
Diag(Tok, diag::err_modifier_expected_colon) << "strict";
// Parse modifier
ConsumeAnyToken();
}
Arg.push_back(OMPC_GRAINSIZE_unknown);
KLoc.emplace_back();
}
} else {
Arg.push_back(OMPC_GRAINSIZE_unknown);
KLoc.emplace_back();
}
} else {
assert(Kind == OMPC_if);
KLoc.push_back(Tok.getLocation());
Expand All @@ -3893,7 +3922,8 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,

bool NeedAnExpression = (Kind == OMPC_schedule && DelimLoc.isValid()) ||
(Kind == OMPC_dist_schedule && DelimLoc.isValid()) ||
Kind == OMPC_if || Kind == OMPC_device;
Kind == OMPC_if || Kind == OMPC_device ||
Kind == OMPC_grainsize;
if (NeedAnExpression) {
SourceLocation ELoc = Tok.getLocation();
ExprResult LHS(ParseCastExpression(AnyCastExpr, false, NotTypeCast));
Expand Down
45 changes: 31 additions & 14 deletions clang/lib/Sema/SemaOpenMP.cpp
Expand Up @@ -15110,9 +15110,6 @@ OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
case OMPC_priority:
Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
break;
case OMPC_grainsize:
Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
break;
case OMPC_num_tasks:
Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
break;
Expand Down Expand Up @@ -15140,6 +15137,7 @@ OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
case OMPC_align:
Res = ActOnOpenMPAlignClause(Expr, StartLoc, LParenLoc, EndLoc);
break;
case OMPC_grainsize:
case OMPC_device:
case OMPC_if:
case OMPC_default:
Expand Down Expand Up @@ -16879,6 +16877,13 @@ OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr,
StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc);
break;
case OMPC_grainsize:
assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
"Modifier for grainsize clause and its location are expected.");
Res = ActOnOpenMPGrainsizeClause(
static_cast<OpenMPGrainsizeClauseModifier>(Argument.back()), Expr,
StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc);
break;
case OMPC_final:
case OMPC_num_threads:
case OMPC_safelen:
Expand Down Expand Up @@ -16924,7 +16929,6 @@ OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
case OMPC_num_teams:
case OMPC_thread_limit:
case OMPC_priority:
case OMPC_grainsize:
case OMPC_nogroup:
case OMPC_num_tasks:
case OMPC_hint:
Expand Down Expand Up @@ -22352,25 +22356,38 @@ OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
StartLoc, LParenLoc, EndLoc);
}

OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation EndLoc) {
OMPClause *Sema::ActOnOpenMPGrainsizeClause(
OpenMPGrainsizeClauseModifier Modifier, Expr *Grainsize,
SourceLocation StartLoc, SourceLocation LParenLoc,
SourceLocation ModifierLoc, SourceLocation EndLoc) {
assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 51) &&
"Unexpected grainsize modifier in OpenMP < 51.");

if (ModifierLoc.isValid() && Modifier == OMPC_GRAINSIZE_unknown) {
std::string Values = getListOfPossibleValues(OMPC_grainsize, /*First=*/0,
OMPC_GRAINSIZE_unknown);
Diag(ModifierLoc, diag::err_omp_unexpected_clause_value)
<< Values << getOpenMPClauseName(OMPC_grainsize);
return nullptr;
}

Expr *ValExpr = Grainsize;
Stmt *HelperValStmt = nullptr;
OpenMPDirectiveKind CaptureRegion = OMPD_unknown;

// OpenMP [2.9.2, taskloop Constrcut]
// The parameter of the grainsize clause must be a positive integer
// expression.
if (!isNonNegativeIntegerValue(
ValExpr, *this, OMPC_grainsize,
/*StrictlyPositive=*/true, /*BuildCapture=*/true,
DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
/*StrictlyPositive=*/true,
/*BuildCapture=*/true,
DSAStack->getCurrentDirective(),
&CaptureRegion, &HelperValStmt))
return nullptr;

return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion,
StartLoc, LParenLoc, EndLoc);
return new (Context)
OMPGrainsizeClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
StartLoc, LParenLoc, ModifierLoc, EndLoc);
}

OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
Expand Down
11 changes: 7 additions & 4 deletions clang/lib/Sema/TreeTransform.h
Expand Up @@ -2051,11 +2051,13 @@ class TreeTransform {
///
/// By default, performs semantic analysis to build the new statement.
/// Subclasses may override this routine to provide different behavior.
OMPClause *RebuildOMPGrainsizeClause(Expr *Grainsize, SourceLocation StartLoc,
OMPClause *RebuildOMPGrainsizeClause(OpenMPGrainsizeClauseModifier Modifier,
Expr *Device, SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation ModifierLoc,
SourceLocation EndLoc) {
return getSema().ActOnOpenMPGrainsizeClause(Grainsize, StartLoc, LParenLoc,
EndLoc);
return getSema().ActOnOpenMPGrainsizeClause(Modifier, Device, StartLoc,
LParenLoc, ModifierLoc, EndLoc);
}

/// Build a new OpenMP 'num_tasks' clause.
Expand Down Expand Up @@ -10350,7 +10352,8 @@ TreeTransform<Derived>::TransformOMPGrainsizeClause(OMPGrainsizeClause *C) {
if (E.isInvalid())
return nullptr;
return getDerived().RebuildOMPGrainsizeClause(
E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc());
C->getModifier(), E.get(), C->getBeginLoc(), C->getLParenLoc(),
C->getModifierLoc(), C->getEndLoc());
}

template <typename Derived>
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Serialization/ASTReader.cpp
Expand Up @@ -10784,7 +10784,9 @@ void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {

void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
VisitOMPClauseWithPreInit(C);
C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
C->setGrainsize(Record.readSubExpr());
C->setModifierLoc(Record.readSourceLocation());
C->setLParenLoc(Record.readSourceLocation());
}

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Serialization/ASTWriter.cpp
Expand Up @@ -6790,7 +6790,9 @@ void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {

void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
VisitOMPClauseWithPreInit(C);
Record.writeEnum(C->getModifier());
Record.AddStmt(C->getGrainsize());
Record.AddSourceLocation(C->getModifierLoc());
Record.AddSourceLocation(C->getLParenLoc());
}

Expand Down
2 changes: 2 additions & 0 deletions clang/test/OpenMP/masked_taskloop_grainsize_messages.cpp
@@ -1,6 +1,8 @@
// RUN: %clang_cc1 -verify -fopenmp -ferror-limit 100 %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=51 -ferror-limit 100 %s -Wuninitialized

// RUN: %clang_cc1 -verify -fopenmp-simd -ferror-limit 100 %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp-version=51 -fopenmp-simd -ferror-limit 100 %s -Wuninitialized

void foo() {
}
Expand Down
2 changes: 2 additions & 0 deletions clang/test/OpenMP/masked_taskloop_simd_grainsize_messages.cpp
@@ -1,6 +1,8 @@
// RUN: %clang_cc1 -verify -fopenmp -ferror-limit 100 %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=51 -ferror-limit 100 %s -Wuninitialized

// RUN: %clang_cc1 -verify -fopenmp-simd -ferror-limit 100 %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp-version=51 -fopenmp-simd -ferror-limit 100 %s -Wuninitialized

void foo() {
}
Expand Down
2 changes: 2 additions & 0 deletions clang/test/OpenMP/master_taskloop_grainsize_messages.cpp
@@ -1,6 +1,8 @@
// RUN: %clang_cc1 -verify -fopenmp -ferror-limit 100 %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=51 -ferror-limit 100 %s -Wuninitialized

// RUN: %clang_cc1 -verify -fopenmp-simd -ferror-limit 100 %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp-version=51 -fopenmp-simd -ferror-limit 100 %s -Wuninitialized

void foo() {
}
Expand Down

0 comments on commit ab9eac7

Please sign in to comment.