Skip to content

Commit 2d78b14

Browse files
authored
[OpenMP][Clang] Parsing/Sema support for need_device_ptr(fb_nullify/fb_preserve). (#168905)
This patch adds parsing, semantic handling, and diagnostics for the `OpenMP 6.1 fb_nullify` and` fb_preserve` fallback modifiers used with the `need_device_ptr` map modifier.
1 parent a8e0afe commit 2d78b14

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-0
lines changed

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,10 @@ def warn_omp_invalid_attribute_for_ompx_attributes : Warning<"'ompx_attribute' c
17241724
"%0 is ignored">, InGroup<OpenMPExtensions>;
17251725
def err_omp_duplicate_modifier : Error<"duplicate modifier '%0' in '%1' clause">;
17261726
def err_omp_expected_modifier : Error<"expected modifier in '%0' clause">;
1727+
def err_omp_unknown_need_device_ptr_kind
1728+
: Error<
1729+
"invalid argument for 'need_device_ptr' kind in 'adjust_args' clause; "
1730+
"expected 'fp_nullify' or 'fb_preserve'">;
17271731

17281732
// Pragma loop support.
17291733
def err_pragma_loop_missing_argument : Error<

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
#ifndef OPENMP_THREADSET_KIND
108108
#define OPENMP_THREADSET_KIND(Name)
109109
#endif
110+
#ifndef OPENMP_NEED_DEVICE_PTR_KIND
111+
#define OPENMP_NEED_DEVICE_PTR_KIND(Name)
112+
#endif
110113

111114
// Static attributes for 'schedule' clause.
112115
OPENMP_SCHEDULE_KIND(static)
@@ -274,6 +277,10 @@ OPENMP_DOACROSS_MODIFIER(source_omp_cur_iteration)
274277
OPENMP_THREADSET_KIND(omp_pool)
275278
OPENMP_THREADSET_KIND(omp_team)
276279

280+
// OpenMP 6.1 modifiers for 'adjust_args' clause.
281+
OPENMP_NEED_DEVICE_PTR_KIND(fb_nullify)
282+
OPENMP_NEED_DEVICE_PTR_KIND(fb_preserve)
283+
277284
#undef OPENMP_NUMTASKS_MODIFIER
278285
#undef OPENMP_NUMTHREADS_MODIFIER
279286
#undef OPENMP_DYN_GROUPPRIVATE_MODIFIER
@@ -306,3 +313,4 @@ OPENMP_THREADSET_KIND(omp_team)
306313
#undef OPENMP_DOACROSS_MODIFIER
307314
#undef OPENMP_ALLOCATE_MODIFIER
308315
#undef OPENMP_THREADSET_KIND
316+
#undef OPENMP_NEED_DEVICE_PTR_KIND

clang/include/clang/Basic/OpenMPKinds.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ enum OpenMPAdjustArgsOpKind {
211211
OMPC_ADJUST_ARGS_unknown,
212212
};
213213

214+
/// OpenMP 6.1 need_device modifier
215+
enum OpenMPNeedDevicePtrModifier {
216+
#define OPENMP_NEED_DEVICE_PTR_KIND(Name) OMPC_NEED_DEVICE_PTR_##Name,
217+
#include "clang/Basic/OpenMPKinds.def"
218+
OMPC_NEED_DEVICE_PTR_unknown,
219+
};
220+
214221
/// OpenMP bindings for the 'bind' clause.
215222
enum OpenMPBindClauseKind {
216223
#define OPENMP_BIND_KIND(Name) OMPC_BIND_##Name,

clang/include/clang/Sema/SemaOpenMP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,8 @@ class SemaOpenMP : public SemaBase {
11741174
int ExtraModifier = -1; ///< Additional modifier for linear, map, depend or
11751175
///< lastprivate clause.
11761176
int OriginalSharingModifier = 0; // Default is shared
1177+
int NeedDevicePtrModifier = 0;
1178+
SourceLocation NeedDevicePtrModifierLoc;
11771179
SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
11781180
MapTypeModifiers;
11791181
SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5010,6 +5010,37 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
50105010
ConsumeToken();
50115011
if (Tok.is(tok::colon))
50125012
Data.ColonLoc = Tok.getLocation();
5013+
if (getLangOpts().OpenMP >= 61) {
5014+
// Handle the optional fallback argument for the need_device_ptr
5015+
// modifier.
5016+
if (Tok.is(tok::l_paren)) {
5017+
BalancedDelimiterTracker T(*this, tok::l_paren);
5018+
T.consumeOpen();
5019+
if (Tok.is(tok::identifier)) {
5020+
std::string Modifier = PP.getSpelling(Tok);
5021+
if (Modifier == "fb_nullify" || Modifier == "fb_preserve") {
5022+
Data.NeedDevicePtrModifier =
5023+
Modifier == "fb_nullify" ? OMPC_NEED_DEVICE_PTR_fb_nullify
5024+
: OMPC_NEED_DEVICE_PTR_fb_preserve;
5025+
} else {
5026+
Diag(Tok, diag::err_omp_unknown_need_device_ptr_kind);
5027+
SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end,
5028+
StopBeforeMatch);
5029+
return false;
5030+
}
5031+
ConsumeToken();
5032+
if (Tok.is(tok::r_paren)) {
5033+
Data.NeedDevicePtrModifierLoc = Tok.getLocation();
5034+
ConsumeAnyToken();
5035+
} else {
5036+
Diag(Tok, diag::err_expected) << tok::r_paren;
5037+
SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end,
5038+
StopBeforeMatch);
5039+
return false;
5040+
}
5041+
}
5042+
}
5043+
}
50135044
ExpectAndConsume(tok::colon, diag::warn_pragma_expected_colon,
50145045
"adjust-op");
50155046
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=61 -ast-print %s \
2+
// RUN: | FileCheck %s
3+
4+
// expected-no-diagnostics
5+
6+
void __attribute__((noinline)) device_impl(int *xp, int *&xpref, int n) {}
7+
8+
#pragma omp declare variant(device_impl) \
9+
adjust_args(need_device_ptr(fb_nullify) : xp, xpref)
10+
void __attribute__((noinline)) host_entry_a(int *xp, int *&xpref, int n) {}
11+
12+
#pragma omp declare variant(device_impl) \
13+
adjust_args(need_device_ptr(fb_preserve) : xp, xpref)
14+
void __attribute__((noinline)) host_entry_b(int *xp, int *&xpref, int n) {}
15+
16+
// CHECK-LABEL: int main()
17+
int main() {
18+
int x;
19+
int *xp = &x;
20+
21+
host_entry_a(xp, xp, 1);
22+
host_entry_b(xp, xp, 1);
23+
return 0;
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_cc1 -verify=omp61 -fopenmp -fopenmp-version=61 %s
2+
3+
void __attribute__((noinline)) device_impl(int *xp, int *&xpref, int n) {}
4+
5+
#pragma omp declare variant(device_impl) \
6+
adjust_args(need_device_ptr(foo) : xp, xpref) // omp61-error{{invalid argument for 'need_device_ptr' kind in 'adjust_args' clause; expected 'fp_nullify' or 'fb_preserve'}} // omp61-error{{expected 'match', 'adjust_args', or 'append_args' clause on 'omp declare variant' directive}}
7+
void __attribute__((noinline)) host_entry_a(int *xp, int *&xpref, int n) {}
8+
9+
#pragma omp declare variant(device_impl) \
10+
adjust_args(need_device_ptr(fb_nullify) : xp, xpref)
11+
void __attribute__((noinline)) host_entry_b(int *xp, int *&xpref, int n) {}
12+
13+
#pragma omp declare variant(device_impl) \
14+
adjust_args(need_device_ptr(fb_preserve) : xp, xpref)
15+
void __attribute__((noinline)) host_entry_c(int *xp, int *&xpref, int n) {}
16+
17+
18+
int main() {
19+
int x;
20+
int *xp = &x;
21+
22+
host_entry_a(xp, xp, 1);
23+
host_entry_b(xp, xp, 1);
24+
host_entry_c(xp, xp, 1);
25+
return 0;
26+
}

0 commit comments

Comments
 (0)