Skip to content

Commit aa82c40

Browse files
committed
[OpenMP] Implement TR8 present map type modifier in Clang (1/2)
This patch implements Clang front end support for the OpenMP TR8 `present` map type modifier. The next patch in this series implements OpenMP runtime support. This patch does not attempt to implement TR8 sec. 2.22.7.1 "map Clause", p. 319, L14-16: > If a map clause with a present map-type-modifier is present in a map > clause, then the effect of the clause is ordered before all other > map clauses that do not have the present modifier. Compare to L10-11, which Clang does not appear to implement yet: > For a given construct, the effect of a map clause with the to, from, > or tofrom map-type is ordered before the effect of a map clause with > the alloc, release, or delete map-type. This patch also does not implement the `present` implicit-behavior for `defaultmap` or the `present` motion-modifier for `target update`. Reviewed By: ABataev Differential Revision: https://reviews.llvm.org/D83061
1 parent a60251d commit aa82c40

22 files changed

+1049
-169
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5393,7 +5393,7 @@ class OMPMapClause final : public OMPMappableExprListClause<OMPMapClause>,
53935393
/// Map-type-modifiers for the 'map' clause.
53945394
OpenMPMapModifierKind MapTypeModifiers[NumberOfOMPMapClauseModifiers] = {
53955395
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
5396-
OMPC_MAP_MODIFIER_unknown};
5396+
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown};
53975397

53985398
/// Location of map-type-modifiers for the 'map' clause.
53995399
SourceLocation MapTypeModifiersLoc[NumberOfOMPMapClauseModifiers];

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,8 @@ def err_omp_decl_in_declare_simd_variant : Error<
12501250
def err_omp_unknown_map_type : Error<
12511251
"incorrect map type, expected one of 'to', 'from', 'tofrom', 'alloc', 'release', or 'delete'">;
12521252
def err_omp_unknown_map_type_modifier : Error<
1253-
"incorrect map type modifier, expected 'always', 'close', or 'mapper'">;
1253+
"incorrect map type modifier, expected 'always', 'close', "
1254+
"%select{or 'mapper'|'mapper', or 'present'}0">;
12541255
def err_omp_map_type_missing : Error<
12551256
"missing map type">;
12561257
def err_omp_map_type_modifier_missing : Error<

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ OPENMP_MAP_KIND(release)
124124
OPENMP_MAP_MODIFIER_KIND(always)
125125
OPENMP_MAP_MODIFIER_KIND(close)
126126
OPENMP_MAP_MODIFIER_KIND(mapper)
127+
OPENMP_MAP_MODIFIER_KIND(present)
127128

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

clang/include/clang/Basic/OpenMPKinds.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ enum OpenMPReductionClauseModifier {
170170
OMPC_REDUCTION_unknown,
171171
};
172172

173-
unsigned getOpenMPSimpleClauseType(OpenMPClauseKind Kind, llvm::StringRef Str);
173+
unsigned getOpenMPSimpleClauseType(OpenMPClauseKind Kind, llvm::StringRef Str,
174+
unsigned OpenMPVersion);
174175
const char *getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind, unsigned Type);
175176

176177
/// Checks if the specified directive is a directive with an associated

clang/lib/Basic/OpenMPKinds.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
using namespace clang;
2121
using namespace llvm::omp;
2222

23-
unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind,
24-
StringRef Str) {
23+
unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
24+
unsigned OpenMPVersion) {
2525
switch (Kind) {
2626
case OMPC_default:
2727
return llvm::StringSwitch<unsigned>(Str)
@@ -51,14 +51,18 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind,
5151
#define OPENMP_LINEAR_KIND(Name) .Case(#Name, OMPC_LINEAR_##Name)
5252
#include "clang/Basic/OpenMPKinds.def"
5353
.Default(OMPC_LINEAR_unknown);
54-
case OMPC_map:
55-
return llvm::StringSwitch<unsigned>(Str)
54+
case OMPC_map: {
55+
unsigned Type = llvm::StringSwitch<unsigned>(Str)
5656
#define OPENMP_MAP_KIND(Name) \
5757
.Case(#Name, static_cast<unsigned>(OMPC_MAP_##Name))
5858
#define OPENMP_MAP_MODIFIER_KIND(Name) \
5959
.Case(#Name, static_cast<unsigned>(OMPC_MAP_MODIFIER_##Name))
6060
#include "clang/Basic/OpenMPKinds.def"
6161
.Default(OMPC_MAP_unknown);
62+
if (OpenMPVersion < 51 && Type == OMPC_MAP_MODIFIER_present)
63+
return OMPC_MAP_MODIFIER_unknown;
64+
return Type;
65+
}
6266
case OMPC_to:
6367
return llvm::StringSwitch<unsigned>(Str)
6468
#define OPENMP_TO_MODIFIER_KIND(Name) \

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7043,6 +7043,9 @@ class MappableExprsHandler {
70437043
/// Close is a hint to the runtime to allocate memory close to
70447044
/// the target device.
70457045
OMP_MAP_CLOSE = 0x400,
7046+
/// 0x800 is reserved for compatibility with XLC.
7047+
/// Produce a runtime error if the data is not already allocated.
7048+
OMP_MAP_PRESENT = 0x1000,
70467049
/// The 16 MSBs of the flags indicate whether the entry is member of some
70477050
/// struct/class.
70487051
OMP_MAP_MEMBER_OF = 0xffff000000000000,
@@ -7287,6 +7290,9 @@ class MappableExprsHandler {
72877290
if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_close)
72887291
!= MapModifiers.end())
72897292
Bits |= OMP_MAP_CLOSE;
7293+
if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_present)
7294+
!= MapModifiers.end())
7295+
Bits |= OMP_MAP_PRESENT;
72907296
return Bits;
72917297
}
72927298

@@ -7970,6 +7976,13 @@ class MappableExprsHandler {
79707976
CombinedInfo.Sizes.push_back(Size);
79717977
// Map type is always TARGET_PARAM
79727978
CombinedInfo.Types.push_back(OMP_MAP_TARGET_PARAM);
7979+
// If any element has the present modifier, then make sure the runtime
7980+
// doesn't attempt to allocate the struct.
7981+
if (CurTypes.end() !=
7982+
llvm::find_if(CurTypes, [](OpenMPOffloadMappingFlags Type) {
7983+
return Type & OMP_MAP_PRESENT;
7984+
}))
7985+
CombinedInfo.Types.back() |= OMP_MAP_PRESENT;
79737986
// Remove TARGET_PARAM flag from the first element
79747987
(*CurTypes.begin()) &= ~OMP_MAP_TARGET_PARAM;
79757988

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,8 @@ parseOpenMPSimpleClause(Parser &P, OpenMPClauseKind Kind) {
14731473
return llvm::None;
14741474

14751475
unsigned Type = getOpenMPSimpleClauseType(
1476-
Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok));
1476+
Kind, Tok.isAnnotation() ? "" : P.getPreprocessor().getSpelling(Tok),
1477+
P.getLangOpts().OpenMP);
14771478
SourceLocation TypeLoc = Tok.getLocation();
14781479
if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
14791480
Tok.isNot(tok::annot_pragma_openmp_end))
@@ -2871,7 +2872,8 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
28712872
Arg[Modifier2] = OMPC_SCHEDULE_MODIFIER_unknown;
28722873
Arg[ScheduleKind] = OMPC_SCHEDULE_unknown;
28732874
unsigned KindModifier = getOpenMPSimpleClauseType(
2874-
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2875+
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok),
2876+
getLangOpts().OpenMP);
28752877
if (KindModifier > OMPC_SCHEDULE_unknown) {
28762878
// Parse 'modifier'
28772879
Arg[Modifier1] = KindModifier;
@@ -2883,7 +2885,8 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
28832885
// Parse ',' 'modifier'
28842886
ConsumeAnyToken();
28852887
KindModifier = getOpenMPSimpleClauseType(
2886-
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2888+
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok),
2889+
getLangOpts().OpenMP);
28872890
Arg[Modifier2] = KindModifier > OMPC_SCHEDULE_unknown
28882891
? KindModifier
28892892
: (unsigned)OMPC_SCHEDULE_unknown;
@@ -2898,7 +2901,8 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
28982901
else
28992902
Diag(Tok, diag::warn_pragma_expected_colon) << "schedule modifier";
29002903
KindModifier = getOpenMPSimpleClauseType(
2901-
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2904+
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok),
2905+
getLangOpts().OpenMP);
29022906
}
29032907
Arg[ScheduleKind] = KindModifier;
29042908
KLoc[ScheduleKind] = Tok.getLocation();
@@ -2912,7 +2916,8 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
29122916
DelimLoc = ConsumeAnyToken();
29132917
} else if (Kind == OMPC_dist_schedule) {
29142918
Arg.push_back(getOpenMPSimpleClauseType(
2915-
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2919+
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok),
2920+
getLangOpts().OpenMP));
29162921
KLoc.push_back(Tok.getLocation());
29172922
if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
29182923
Tok.isNot(tok::annot_pragma_openmp_end))
@@ -2922,7 +2927,8 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
29222927
} else if (Kind == OMPC_defaultmap) {
29232928
// Get a defaultmap modifier
29242929
unsigned Modifier = getOpenMPSimpleClauseType(
2925-
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok));
2930+
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok),
2931+
getLangOpts().OpenMP);
29262932
// Set defaultmap modifier to unknown if it is either scalar, aggregate, or
29272933
// pointer
29282934
if (Modifier < OMPC_DEFAULTMAP_MODIFIER_unknown)
@@ -2940,7 +2946,8 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
29402946
Diag(Tok, diag::warn_pragma_expected_colon) << "defaultmap modifier";
29412947
// Get a defaultmap kind
29422948
Arg.push_back(getOpenMPSimpleClauseType(
2943-
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2949+
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok),
2950+
getLangOpts().OpenMP));
29442951
KLoc.push_back(Tok.getLocation());
29452952
if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
29462953
Tok.isNot(tok::annot_pragma_openmp_end))
@@ -2955,7 +2962,8 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
29552962
NextToken().is(tok::colon)) {
29562963
// Parse optional <device modifier> ':'
29572964
Arg.push_back(getOpenMPSimpleClauseType(
2958-
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok)));
2965+
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok),
2966+
getLangOpts().OpenMP));
29592967
KLoc.push_back(Tok.getLocation());
29602968
ConsumeAnyToken();
29612969
// Parse ':'
@@ -3057,14 +3065,16 @@ static bool ParseReductionId(Parser &P, CXXScopeSpec &ReductionIdScopeSpec,
30573065
}
30583066

30593067
/// Checks if the token is a valid map-type-modifier.
3068+
/// FIXME: It will return an OpenMPMapClauseKind if that's what it parses.
30603069
static OpenMPMapModifierKind isMapModifier(Parser &P) {
30613070
Token Tok = P.getCurToken();
30623071
if (!Tok.is(tok::identifier))
30633072
return OMPC_MAP_MODIFIER_unknown;
30643073

30653074
Preprocessor &PP = P.getPreprocessor();
3066-
OpenMPMapModifierKind TypeModifier = static_cast<OpenMPMapModifierKind>(
3067-
getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
3075+
OpenMPMapModifierKind TypeModifier =
3076+
static_cast<OpenMPMapModifierKind>(getOpenMPSimpleClauseType(
3077+
OMPC_map, PP.getSpelling(Tok), P.getLangOpts().OpenMP));
30683078
return TypeModifier;
30693079
}
30703080

@@ -3099,12 +3109,14 @@ bool Parser::parseMapperModifier(OpenMPVarListDataTy &Data) {
30993109

31003110
/// Parse map-type-modifiers in map clause.
31013111
/// map([ [map-type-modifier[,] [map-type-modifier[,] ...] map-type : ] list)
3102-
/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
3112+
/// where, map-type-modifier ::= always | close | mapper(mapper-identifier) |
3113+
/// present
31033114
bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
31043115
while (getCurToken().isNot(tok::colon)) {
31053116
OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
31063117
if (TypeModifier == OMPC_MAP_MODIFIER_always ||
3107-
TypeModifier == OMPC_MAP_MODIFIER_close) {
3118+
TypeModifier == OMPC_MAP_MODIFIER_close ||
3119+
TypeModifier == OMPC_MAP_MODIFIER_present) {
31083120
Data.MapTypeModifiers.push_back(TypeModifier);
31093121
Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
31103122
ConsumeToken();
@@ -3126,7 +3138,8 @@ bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
31263138
// Potential map-type token as it is followed by a colon.
31273139
if (PP.LookAhead(0).is(tok::colon))
31283140
return false;
3129-
Diag(Tok, diag::err_omp_unknown_map_type_modifier);
3141+
Diag(Tok, diag::err_omp_unknown_map_type_modifier)
3142+
<< (getLangOpts().OpenMP >= 51 ? 1 : 0);
31303143
ConsumeToken();
31313144
}
31323145
if (getCurToken().is(tok::comma))
@@ -3136,14 +3149,16 @@ bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
31363149
}
31373150

31383151
/// Checks if the token is a valid map-type.
3152+
/// FIXME: It will return an OpenMPMapModifierKind if that's what it parses.
31393153
static OpenMPMapClauseKind isMapType(Parser &P) {
31403154
Token Tok = P.getCurToken();
31413155
// The map-type token can be either an identifier or the C++ delete keyword.
31423156
if (!Tok.isOneOf(tok::identifier, tok::kw_delete))
31433157
return OMPC_MAP_unknown;
31443158
Preprocessor &PP = P.getPreprocessor();
3145-
OpenMPMapClauseKind MapType = static_cast<OpenMPMapClauseKind>(
3146-
getOpenMPSimpleClauseType(OMPC_map, PP.getSpelling(Tok)));
3159+
OpenMPMapClauseKind MapType =
3160+
static_cast<OpenMPMapClauseKind>(getOpenMPSimpleClauseType(
3161+
OMPC_map, PP.getSpelling(Tok), P.getLangOpts().OpenMP));
31473162
return MapType;
31483163
}
31493164

@@ -3297,7 +3312,8 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
32973312
(Tok.is(tok::identifier) || Tok.is(tok::kw_default)) &&
32983313
NextToken().is(tok::comma)) {
32993314
// Parse optional reduction modifier.
3300-
Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
3315+
Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok),
3316+
getLangOpts().OpenMP);
33013317
Data.ExtraModifierLoc = Tok.getLocation();
33023318
ConsumeToken();
33033319
assert(Tok.is(tok::comma) && "Expected comma.");
@@ -3342,7 +3358,8 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
33423358
// Handle dependency type for depend clause.
33433359
ColonProtectionRAIIObject ColonRAII(*this);
33443360
Data.ExtraModifier = getOpenMPSimpleClauseType(
3345-
Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "");
3361+
Kind, Tok.is(tok::identifier) ? PP.getSpelling(Tok) : "",
3362+
getLangOpts().OpenMP);
33463363
Data.ExtraModifierLoc = Tok.getLocation();
33473364
if (Data.ExtraModifier == OMPC_DEPEND_unknown) {
33483365
SkipUntil(tok::colon, tok::r_paren, tok::annot_pragma_openmp_end,
@@ -3367,7 +3384,8 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
33673384
// Try to parse modifier if any.
33683385
Data.ExtraModifier = OMPC_LINEAR_val;
33693386
if (Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::l_paren)) {
3370-
Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
3387+
Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok),
3388+
getLangOpts().OpenMP);
33713389
Data.ExtraModifierLoc = ConsumeToken();
33723390
LinearT.consumeOpen();
33733391
NeedRParenForLinear = true;
@@ -3380,7 +3398,8 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
33803398
if ((getLangOpts().OpenMP >= 50 && !isOpenMPDistributeDirective(DKind) &&
33813399
!isOpenMPTaskLoopDirective(DKind)) &&
33823400
Tok.is(tok::identifier) && PP.LookAhead(0).is(tok::colon)) {
3383-
Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok));
3401+
Data.ExtraModifier = getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok),
3402+
getLangOpts().OpenMP);
33843403
Data.ExtraModifierLoc = Tok.getLocation();
33853404
ConsumeToken();
33863405
assert(Tok.is(tok::colon) && "Expected colon.");
@@ -3425,13 +3444,15 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
34253444
if (Tok.is(tok::identifier)) {
34263445
bool IsMapperModifier = false;
34273446
if (Kind == OMPC_to) {
3428-
auto Modifier = static_cast<OpenMPToModifierKind>(
3429-
getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
3447+
auto Modifier =
3448+
static_cast<OpenMPToModifierKind>(getOpenMPSimpleClauseType(
3449+
Kind, PP.getSpelling(Tok), getLangOpts().OpenMP));
34303450
if (Modifier == OMPC_TO_MODIFIER_mapper)
34313451
IsMapperModifier = true;
34323452
} else {
3433-
auto Modifier = static_cast<OpenMPFromModifierKind>(
3434-
getOpenMPSimpleClauseType(Kind, PP.getSpelling(Tok)));
3453+
auto Modifier =
3454+
static_cast<OpenMPFromModifierKind>(getOpenMPSimpleClauseType(
3455+
Kind, PP.getSpelling(Tok), getLangOpts().OpenMP));
34353456
if (Modifier == OMPC_FROM_MODIFIER_mapper)
34363457
IsMapperModifier = true;
34373458
}

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17640,9 +17640,9 @@ OMPClause *Sema::ActOnOpenMPMapClause(
1764017640
OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
1764117641
SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
1764217642
const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
17643-
OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
17644-
OMPC_MAP_MODIFIER_unknown,
17645-
OMPC_MAP_MODIFIER_unknown};
17643+
OpenMPMapModifierKind Modifiers[] = {
17644+
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
17645+
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown};
1764617646
SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
1764717647

1764817648
// Process map-type-modifiers, flag errors for duplicate modifiers.

0 commit comments

Comments
 (0)