Skip to content

Commit

Permalink
[OpenMP 5.0] Parsing/sema support for to clause with mapper modifier.
Browse files Browse the repository at this point in the history
This patch implements the parsing and sema support for OpenMP to clause
with potential user-defined mappers attached. User defined mapper is a
new feature in OpenMP 5.0. A to/from clause can have an explicit or
implicit associated mapper, which instructs the compiler to generate and
use customized mapping functions. An example is shown below:

    struct S { int len; int *d; };
    #pragma omp declare mapper(id: struct S s) map(s, s.d[0:s.len])
    struct S ss;
    #pragma omp target update to(mapper(id): ss) // use the mapper with name 'id' to map ss to device

Contributed-by: <lildmh@gmail.com>

Differential Revision: https://reviews.llvm.org/D58523

llvm-svn: 354698
  • Loading branch information
Meinersbur committed Feb 22, 2019
1 parent 8fffa1d commit 01f670d
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 118 deletions.
32 changes: 28 additions & 4 deletions clang/include/clang/AST/OpenMPClause.h
Expand Up @@ -158,8 +158,11 @@ class OMPClauseWithPostUpdate : public OMPClauseWithPreInit {

/// This structure contains most locations needed for by an OMPVarListClause.
struct OMPVarListLocTy {
/// Starting location of the clause (the clause keyword).
SourceLocation StartLoc;
/// Location of '('.
SourceLocation LParenLoc;
/// Ending location of the clause.
SourceLocation EndLoc;
OMPVarListLocTy() = default;
OMPVarListLocTy(SourceLocation StartLoc, SourceLocation LParenLoc,
Expand Down Expand Up @@ -3609,9 +3612,13 @@ class OMPClauseMappableExprCommon {
/// This structure contains all sizes needed for by an
/// OMPMappableExprListClause.
struct OMPMappableExprListSizeTy {
/// Number of expressions listed.
unsigned NumVars;
/// Number of unique base declarations.
unsigned NumUniqueDeclarations;
/// Number of component lists.
unsigned NumComponentLists;
/// Total number of expression components.
unsigned NumComponents;
OMPMappableExprListSizeTy() = default;
OMPMappableExprListSizeTy(unsigned NumVars, unsigned NumUniqueDeclarations,
Expand Down Expand Up @@ -4969,6 +4976,10 @@ class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,

/// Build clause with number of variables \a NumVars.
///
/// \param MapperQualifierLoc C++ nested name specifier for the associated
/// user-defined mapper.
/// \param MapperIdInfo The identifier of associated user-defined mapper.
/// \param MapType Map type.
/// \param Locs Locations needed to build a mappable clause. It includes 1)
/// StartLoc: starting location of the clause (the clause keyword); 2)
/// LParenLoc: location of '('; 3) EndLoc: ending location of the clause.
Expand All @@ -4977,9 +4988,12 @@ class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
/// NumUniqueDeclarations: number of unique base declarations in this clause;
/// 3) NumComponentLists: number of component lists in this clause; and 4)
/// NumComponents: total number of expression components in the clause.
explicit OMPToClause(const OMPVarListLocTy &Locs,
explicit OMPToClause(NestedNameSpecifierLoc MapperQualifierLoc,
DeclarationNameInfo MapperIdInfo,
const OMPVarListLocTy &Locs,
const OMPMappableExprListSizeTy &Sizes)
: OMPMappableExprListClause(OMPC_to, Locs, Sizes) {}
: OMPMappableExprListClause(OMPC_to, Locs, Sizes, &MapperQualifierLoc,
&MapperIdInfo) {}

/// Build an empty clause.
///
Expand All @@ -4994,7 +5008,9 @@ class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
/// Define the sizes of each trailing object array except the last one. This
/// is required for TrailingObjects to work properly.
size_t numTrailingObjects(OverloadToken<Expr *>) const {
return varlist_size();
// There are varlist_size() of expressions, and varlist_size() of
// user-defined mappers.
return 2 * varlist_size();
}
size_t numTrailingObjects(OverloadToken<ValueDecl *>) const {
return getUniqueDeclarationsNum();
Expand All @@ -5013,10 +5029,18 @@ class OMPToClause final : public OMPMappableExprListClause<OMPToClause>,
/// \param Vars The original expression used in the clause.
/// \param Declarations Declarations used in the clause.
/// \param ComponentLists Component lists used in the clause.
/// \param UDMapperRefs References to user-defined mappers associated with
/// expressions used in the clause.
/// \param UDMQualifierLoc C++ nested name specifier for the associated
/// user-defined mapper.
/// \param MapperId The identifier of associated user-defined mapper.
static OMPToClause *Create(const ASTContext &C, const OMPVarListLocTy &Locs,
ArrayRef<Expr *> Vars,
ArrayRef<ValueDecl *> Declarations,
MappableExprComponentListsRef ComponentLists);
MappableExprComponentListsRef ComponentLists,
ArrayRef<Expr *> UDMapperRefs,
NestedNameSpecifierLoc UDMQualifierLoc,
DeclarationNameInfo MapperId);

/// Creates an empty clause with the place for \a NumVars variables.
///
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticParseKinds.td
Expand Up @@ -1175,7 +1175,7 @@ def err_omp_declare_target_unexpected_clause: Error<
def err_omp_expected_clause: Error<
"expected at least one clause on '#pragma omp %0' directive">;
def err_omp_mapper_illegal_identifier : Error<
"illegal identifier on 'omp declare mapper' directive">;
"illegal OpenMP user-defined mapper identifier">;
def err_omp_mapper_expected_declarator : Error<
"expected declarator on 'omp declare mapper' directive">;

Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/OpenMPKinds.def
Expand Up @@ -122,6 +122,9 @@
#ifndef OPENMP_MAP_MODIFIER_KIND
#define OPENMP_MAP_MODIFIER_KIND(Name)
#endif
#ifndef OPENMP_TO_MODIFIER_KIND
#define OPENMP_TO_MODIFIER_KIND(Name)
#endif
#ifndef OPENMP_DIST_SCHEDULE_KIND
#define OPENMP_DIST_SCHEDULE_KIND(Name)
#endif
Expand Down Expand Up @@ -578,6 +581,9 @@ OPENMP_MAP_MODIFIER_KIND(always)
OPENMP_MAP_MODIFIER_KIND(close)
OPENMP_MAP_MODIFIER_KIND(mapper)

// Modifiers for 'to' clause.
OPENMP_TO_MODIFIER_KIND(mapper)

// Clauses allowed for OpenMP directive 'taskloop'.
OPENMP_TASKLOOP_CLAUSE(if)
OPENMP_TASKLOOP_CLAUSE(shared)
Expand Down Expand Up @@ -934,6 +940,7 @@ OPENMP_DECLARE_MAPPER_CLAUSE(map)
#undef OPENMP_FOR_SIMD_CLAUSE
#undef OPENMP_MAP_KIND
#undef OPENMP_MAP_MODIFIER_KIND
#undef OPENMP_TO_MODIFIER_KIND
#undef OPENMP_DISTRIBUTE_CLAUSE
#undef OPENMP_DIST_SCHEDULE_KIND
#undef OPENMP_DEFAULTMAP_KIND
Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/Basic/OpenMPKinds.h
Expand Up @@ -105,6 +105,14 @@ enum OpenMPMapModifierKind {
OMPC_MAP_MODIFIER_last
};

/// OpenMP modifier kind for 'to' clause.
enum OpenMPToModifierKind {
#define OPENMP_TO_MODIFIER_KIND(Name) \
OMPC_TO_MODIFIER_##Name,
#include "clang/Basic/OpenMPKinds.def"
OMPC_TO_MODIFIER_unknown
};

/// OpenMP attributes for 'dist_schedule' clause.
enum OpenMPDistScheduleClauseKind {
#define OPENMP_DIST_SCHEDULE_KIND(Name) OMPC_DIST_SCHEDULE_##Name,
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Parse/Parser.h
Expand Up @@ -2925,6 +2925,8 @@ class Parser : public CodeCompletionHandler {
ParsedType ObjectType,
SourceLocation *TemplateKWLoc,
UnqualifiedId &Result);
/// Parses the mapper modifier in map, to, and from clauses.
bool parseMapperModifier(OpenMPVarListDataTy &Data);
/// Parses map-type-modifiers in map clause.
/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
Expand Down
7 changes: 5 additions & 2 deletions clang/include/clang/Sema/Sema.h
Expand Up @@ -9471,8 +9471,11 @@ class Sema {
SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
SourceLocation KindLoc, SourceLocation EndLoc);
/// Called on well-formed 'to' clause.
OMPClause *ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
const OMPVarListLocTy &Locs);
OMPClause *
ActOnOpenMPToClause(ArrayRef<Expr *> VarList, CXXScopeSpec &MapperIdScopeSpec,
DeclarationNameInfo &MapperId,
const OMPVarListLocTy &Locs,
ArrayRef<Expr *> UnresolvedMappers = llvm::None);
/// Called on well-formed 'from' clause.
OMPClause *ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
const OMPVarListLocTy &Locs);
Expand Down
35 changes: 24 additions & 11 deletions clang/lib/AST/OpenMPClause.cpp
Expand Up @@ -845,20 +845,20 @@ OMPMapClause::CreateEmpty(const ASTContext &C,
return new (Mem) OMPMapClause(Sizes);
}

OMPToClause *OMPToClause::Create(const ASTContext &C,
const OMPVarListLocTy &Locs,
ArrayRef<Expr *> Vars,
ArrayRef<ValueDecl *> Declarations,
MappableExprComponentListsRef ComponentLists) {
OMPToClause *OMPToClause::Create(
const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
ArrayRef<ValueDecl *> Declarations,
MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
OMPMappableExprListSizeTy Sizes;
Sizes.NumVars = Vars.size();
Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
Sizes.NumComponentLists = ComponentLists.size();
Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);

// We need to allocate:
// NumVars x Expr* - we have an original list expression for each clause list
// entry.
// 2 x NumVars x Expr* - we have an original list expression and an associated
// user-defined mapper for each clause list entry.
// NumUniqueDeclarations x ValueDecl* - unique base declarations associated
// with each component list.
// (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
Expand All @@ -869,13 +869,14 @@ OMPToClause *OMPToClause::Create(const ASTContext &C,
void *Mem = C.Allocate(
totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
OMPClauseMappableExprCommon::MappableComponent>(
Sizes.NumVars, Sizes.NumUniqueDeclarations,
2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
Sizes.NumComponents));

OMPToClause *Clause = new (Mem) OMPToClause(Locs, Sizes);
auto *Clause = new (Mem) OMPToClause(UDMQualifierLoc, MapperId, Locs, Sizes);

Clause->setVarRefs(Vars);
Clause->setUDMapperRefs(UDMapperRefs);
Clause->setClauseInfo(Declarations, ComponentLists);
return Clause;
}
Expand All @@ -885,7 +886,7 @@ OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C,
void *Mem = C.Allocate(
totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
OMPClauseMappableExprCommon::MappableComponent>(
Sizes.NumVars, Sizes.NumUniqueDeclarations,
2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
Sizes.NumComponents));
return new (Mem) OMPToClause(Sizes);
Expand Down Expand Up @@ -1444,7 +1445,19 @@ void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
if (!Node->varlist_empty()) {
OS << "to";
VisitOMPClauseList(Node, '(');
DeclarationNameInfo MapperId = Node->getMapperIdInfo();
if (MapperId.getName() && !MapperId.getName().isEmpty()) {
OS << '(';
OS << "mapper(";
NestedNameSpecifier *MapperNNS =
Node->getMapperQualifierLoc().getNestedNameSpecifier();
if (MapperNNS)
MapperNNS->print(OS, Policy);
OS << MapperId << "):";
VisitOMPClauseList(Node, ' ');
} else {
VisitOMPClauseList(Node, '(');
}
OS << ")";
}
}
Expand Down
20 changes: 18 additions & 2 deletions clang/lib/Basic/OpenMPKinds.cpp
Expand Up @@ -116,6 +116,12 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind,
.Case(#Name, static_cast<unsigned>(OMPC_MAP_MODIFIER_##Name))
#include "clang/Basic/OpenMPKinds.def"
.Default(OMPC_MAP_unknown);
case OMPC_to:
return llvm::StringSwitch<unsigned>(Str)
#define OPENMP_TO_MODIFIER_KIND(Name) \
.Case(#Name, static_cast<unsigned>(OMPC_TO_MODIFIER_##Name))
#include "clang/Basic/OpenMPKinds.def"
.Default(OMPC_TO_MODIFIER_unknown);
case OMPC_dist_schedule:
return llvm::StringSwitch<OpenMPDistScheduleClauseKind>(Str)
#define OPENMP_DIST_SCHEDULE_KIND(Name) .Case(#Name, OMPC_DIST_SCHEDULE_##Name)
Expand Down Expand Up @@ -174,7 +180,6 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind,
case OMPC_num_tasks:
case OMPC_hint:
case OMPC_uniform:
case OMPC_to:
case OMPC_from:
case OMPC_use_device_ptr:
case OMPC_is_device_ptr:
Expand Down Expand Up @@ -260,6 +265,18 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
break;
}
llvm_unreachable("Invalid OpenMP 'map' clause type");
case OMPC_to:
switch (Type) {
case OMPC_TO_MODIFIER_unknown:
return "unknown";
#define OPENMP_TO_MODIFIER_KIND(Name) \
case OMPC_TO_MODIFIER_##Name: \
return #Name;
#include "clang/Basic/OpenMPKinds.def"
default:
break;
}
llvm_unreachable("Invalid OpenMP 'to' clause type");
case OMPC_dist_schedule:
switch (Type) {
case OMPC_DIST_SCHEDULE_unknown:
Expand Down Expand Up @@ -333,7 +350,6 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
case OMPC_num_tasks:
case OMPC_hint:
case OMPC_uniform:
case OMPC_to:
case OMPC_from:
case OMPC_use_device_ptr:
case OMPC_is_device_ptr:
Expand Down

0 comments on commit 01f670d

Please sign in to comment.