Skip to content

Commit 4a11cce

Browse files
SunilKuravinakopSunil Kuravinakop
andauthored
[Clang][OpenMP]Default clause variable category (#157063)
Support for Variable Category in Default Clause. --------- Co-authored-by: Sunil Kuravinakop <[email protected]>
1 parent 6a4f664 commit 4a11cce

16 files changed

+295
-33
lines changed

clang/docs/OpenMPSupport.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ implementation.
576576
| | | | Flang parser: https://github.com/llvm/llvm-project/pull/153807 |
577577
| | | | Flang sema: https://github.com/llvm/llvm-project/pull/154779 |
578578
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
579-
| variable-category on default clause | :part:`In Progress` | :none:`unclaimed` | |
579+
| variable-category on default clause | :good:`done` | :none:`unclaimed` | |
580580
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
581581
| Changes to omp_target_is_accessible | :part:`In Progress` | :part:`In Progress` | |
582582
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ OpenMP Support
521521
- Allow array length to be omitted in array section subscript expression.
522522
- Fixed non-contiguous strided update in the ``omp target update`` directive with the ``from`` clause.
523523
- Properly handle array section/assumed-size array privatization in C/C++.
524+
- Added support for ``variable-category`` modifier in ``default clause``.
524525

525526
Improvements
526527
^^^^^^^^^^^^

clang/include/clang/AST/OpenMPClause.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,12 @@ class OMPDefaultClause : public OMPClause {
12691269
/// Start location of the kind in source code.
12701270
SourceLocation KindKwLoc;
12711271

1272+
/// Variable-Category to indicate where Kind is applied
1273+
OpenMPDefaultClauseVariableCategory VC = OMPC_DEFAULT_VC_all;
1274+
1275+
/// Start location of Variable-Category
1276+
SourceLocation VCLoc;
1277+
12721278
/// Set kind of the clauses.
12731279
///
12741280
/// \param K Argument of clause.
@@ -1279,6 +1285,15 @@ class OMPDefaultClause : public OMPClause {
12791285
/// \param KLoc Argument location.
12801286
void setDefaultKindKwLoc(SourceLocation KLoc) { KindKwLoc = KLoc; }
12811287

1288+
/// Set Variable Category used with the Kind Clause (Default Modifier)
1289+
void setDefaultVariableCategory(OpenMPDefaultClauseVariableCategory VC) {
1290+
this->VC = VC;
1291+
}
1292+
1293+
void setDefaultVariableCategoryLocation(SourceLocation VCLoc) {
1294+
this->VCLoc = VCLoc;
1295+
}
1296+
12821297
public:
12831298
/// Build 'default' clause with argument \a A ('none' or 'shared').
12841299
///
@@ -1288,10 +1303,11 @@ class OMPDefaultClause : public OMPClause {
12881303
/// \param LParenLoc Location of '('.
12891304
/// \param EndLoc Ending location of the clause.
12901305
OMPDefaultClause(llvm::omp::DefaultKind A, SourceLocation ALoc,
1306+
OpenMPDefaultClauseVariableCategory VC, SourceLocation VCLoc,
12911307
SourceLocation StartLoc, SourceLocation LParenLoc,
12921308
SourceLocation EndLoc)
12931309
: OMPClause(llvm::omp::OMPC_default, StartLoc, EndLoc),
1294-
LParenLoc(LParenLoc), Kind(A), KindKwLoc(ALoc) {}
1310+
LParenLoc(LParenLoc), Kind(A), KindKwLoc(ALoc), VC(VC), VCLoc(VCLoc) {}
12951311

12961312
/// Build an empty clause.
12971313
OMPDefaultClause()
@@ -1310,6 +1326,10 @@ class OMPDefaultClause : public OMPClause {
13101326
/// Returns location of clause kind.
13111327
SourceLocation getDefaultKindKwLoc() const { return KindKwLoc; }
13121328

1329+
OpenMPDefaultClauseVariableCategory getDefaultVC() const { return VC; }
1330+
1331+
SourceLocation getDefaultVCLoc() const { return VCLoc; }
1332+
13131333
child_range children() {
13141334
return child_range(child_iterator(), child_iterator());
13151335
}

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11714,6 +11714,8 @@ def note_omp_default_dsa_none : Note<
1171411714
"explicit data sharing attribute requested here">;
1171511715
def note_omp_defaultmap_attr_none : Note<
1171611716
"explicit data sharing attribute, data mapping attribute, or is_device_ptr clause requested here">;
11717+
def err_omp_default_vc : Error<
11718+
"wrong variable category specified with modifier %0 in the default clause">;
1171711719
def err_omp_wrong_dsa : Error<
1171811720
"%0 variable cannot be %1">;
1171911721
def err_omp_variably_modified_type_not_supported : Error<

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#ifndef OPENMP_DIST_SCHEDULE_KIND
3636
#define OPENMP_DIST_SCHEDULE_KIND(Name)
3737
#endif
38+
#ifndef OPENMP_DEFAULT_VARIABLE_CATEGORY
39+
#define OPENMP_DEFAULT_VARIABLE_CATEGORY(Name)
40+
#endif
3841
#ifndef OPENMP_DEFAULTMAP_KIND
3942
#define OPENMP_DEFAULTMAP_KIND(Name)
4043
#endif
@@ -112,6 +115,13 @@ OPENMP_SCHEDULE_MODIFIER(simd)
112115
OPENMP_DEVICE_MODIFIER(ancestor)
113116
OPENMP_DEVICE_MODIFIER(device_num)
114117

118+
// Variable-category attributes for 'default' clause.
119+
OPENMP_DEFAULT_VARIABLE_CATEGORY(aggregate)
120+
OPENMP_DEFAULT_VARIABLE_CATEGORY(all)
121+
OPENMP_DEFAULT_VARIABLE_CATEGORY(allocatable)
122+
OPENMP_DEFAULT_VARIABLE_CATEGORY(pointer)
123+
OPENMP_DEFAULT_VARIABLE_CATEGORY(scalar)
124+
115125
// Static attributes for 'defaultmap' clause.
116126
OPENMP_DEFAULTMAP_KIND(scalar)
117127
OPENMP_DEFAULTMAP_KIND(aggregate)
@@ -267,6 +277,7 @@ OPENMP_DOACROSS_MODIFIER(source_omp_cur_iteration)
267277
#undef OPENMP_MAP_MODIFIER_KIND
268278
#undef OPENMP_MOTION_MODIFIER_KIND
269279
#undef OPENMP_DIST_SCHEDULE_KIND
280+
#undef OPENMP_DEFAULT_VARIABLE_CATEGORY
270281
#undef OPENMP_DEFAULTMAP_KIND
271282
#undef OPENMP_DEFAULTMAP_MODIFIER
272283
#undef OPENMP_DOACROSS_MODIFIER

clang/include/clang/Basic/OpenMPKinds.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ enum OpenMPDistScheduleClauseKind {
107107
OMPC_DIST_SCHEDULE_unknown
108108
};
109109

110+
/// OpenMP variable-category for 'default' clause.
111+
enum OpenMPDefaultClauseVariableCategory {
112+
#define OPENMP_DEFAULT_VARIABLE_CATEGORY(Name) OMPC_DEFAULT_VC_##Name,
113+
#include "clang/Basic/OpenMPKinds.def"
114+
OMPC_DEFAULT_VC_unknown
115+
};
116+
110117
/// OpenMP attributes for 'defaultmap' clause.
111118
enum OpenMPDefaultmapClauseKind {
112119
#define OPENMP_DEFAULTMAP_KIND(Name) \
@@ -257,6 +264,10 @@ struct OMPInteropInfo final {
257264
llvm::SmallVector<Expr *, 4> PreferTypes;
258265
};
259266

267+
OpenMPDefaultClauseVariableCategory
268+
getOpenMPDefaultVariableCategory(StringRef Str, const LangOptions &LangOpts);
269+
const char *getOpenMPDefaultVariableCategoryName(unsigned VC);
270+
260271
unsigned getOpenMPSimpleClauseType(OpenMPClauseKind Kind, llvm::StringRef Str,
261272
const LangOptions &LangOpts);
262273
const char *getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind, unsigned Type);

clang/include/clang/Sema/SemaOpenMP.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,11 @@ class SemaOpenMP : public SemaBase {
951951
SourceLocation LParenLoc,
952952
SourceLocation EndLoc);
953953
/// Called on well-formed 'default' clause.
954-
OMPClause *ActOnOpenMPDefaultClause(llvm::omp::DefaultKind Kind,
955-
SourceLocation KindLoc,
956-
SourceLocation StartLoc,
957-
SourceLocation LParenLoc,
958-
SourceLocation EndLoc);
954+
OMPClause *
955+
ActOnOpenMPDefaultClause(llvm::omp::DefaultKind M, SourceLocation MLoc,
956+
OpenMPDefaultClauseVariableCategory VCKind,
957+
SourceLocation VCKindLoc, SourceLocation StartLoc,
958+
SourceLocation LParenLoc, SourceLocation EndLoc);
959959
/// Called on well-formed 'proc_bind' clause.
960960
OMPClause *ActOnOpenMPProcBindClause(llvm::omp::ProcBindKind Kind,
961961
SourceLocation KindLoc,

clang/lib/AST/OpenMPClause.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,8 +1911,13 @@ void OMPClausePrinter::VisitOMPDetachClause(OMPDetachClause *Node) {
19111911
void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
19121912
OS << "default("
19131913
<< getOpenMPSimpleClauseTypeName(OMPC_default,
1914-
unsigned(Node->getDefaultKind()))
1915-
<< ")";
1914+
unsigned(Node->getDefaultKind()));
1915+
if (Version >= 60 && Node->getDefaultVC() != OMPC_DEFAULT_VC_all) {
1916+
OS << ":"
1917+
<< getOpenMPDefaultVariableCategoryName(unsigned(Node->getDefaultVC()));
1918+
}
1919+
1920+
OS << ")";
19161921
}
19171922

19181923
void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {

clang/lib/Basic/OpenMPKinds.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@
2020
using namespace clang;
2121
using namespace llvm::omp;
2222

23+
OpenMPDefaultClauseVariableCategory
24+
clang::getOpenMPDefaultVariableCategory(StringRef Str,
25+
const LangOptions &LangOpts) {
26+
return llvm::StringSwitch<OpenMPDefaultClauseVariableCategory>(Str)
27+
#define OPENMP_DEFAULT_VARIABLE_CATEGORY(Name) \
28+
.Case(#Name, OMPC_DEFAULT_VC_##Name)
29+
#include "clang/Basic/OpenMPKinds.def"
30+
.Default(OMPC_DEFAULT_VC_unknown);
31+
}
32+
33+
const char *clang::getOpenMPDefaultVariableCategoryName(unsigned VC) {
34+
switch (VC) {
35+
#define OPENMP_DEFAULT_VARIABLE_CATEGORY(Name) \
36+
case OMPC_DEFAULT_VC_##Name: \
37+
return #Name;
38+
#include "clang/Basic/OpenMPKinds.def"
39+
}
40+
llvm_unreachable("Invalid Variable Category in the default clause");
41+
}
42+
2343
unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
2444
const LangOptions &LangOpts) {
2545
switch (Kind) {
@@ -902,4 +922,3 @@ bool clang::checkFailClauseParameter(OpenMPClauseKind FailClauseParameter) {
902922
FailClauseParameter == llvm::omp::OMPC_relaxed ||
903923
FailClauseParameter == llvm::omp::OMPC_seq_cst;
904924
}
905-

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3083,7 +3083,6 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
30833083
Clause = ParseOpenMPSingleExprClause(CKind, WrongDirective);
30843084
break;
30853085
case OMPC_fail:
3086-
case OMPC_default:
30873086
case OMPC_proc_bind:
30883087
case OMPC_atomic_default_mem_order:
30893088
case OMPC_at:
@@ -3115,6 +3114,7 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
31153114
case OMPC_schedule:
31163115
case OMPC_dist_schedule:
31173116
case OMPC_defaultmap:
3117+
case OMPC_default:
31183118
case OMPC_order:
31193119
// OpenMP [2.7.1, Restrictions, p. 3]
31203120
// Only one schedule clause can appear on a loop directive.
@@ -3734,6 +3734,32 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
37343734
ConsumeAnyToken();
37353735
if (Arg.back() == OMPC_DIST_SCHEDULE_static && Tok.is(tok::comma))
37363736
DelimLoc = ConsumeAnyToken();
3737+
} else if (Kind == OMPC_default) {
3738+
// Get a default modifier
3739+
unsigned Modifier = getOpenMPSimpleClauseType(
3740+
Kind, Tok.isAnnotation() ? "" : PP.getSpelling(Tok), getLangOpts());
3741+
3742+
Arg.push_back(Modifier);
3743+
KLoc.push_back(Tok.getLocation());
3744+
if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
3745+
Tok.isNot(tok::annot_pragma_openmp_end))
3746+
ConsumeAnyToken();
3747+
// Parse ':'
3748+
if (Tok.is(tok::colon) && getLangOpts().OpenMP >= 60) {
3749+
ConsumeAnyToken();
3750+
// Get a variable-category attribute for default clause modifier
3751+
OpenMPDefaultClauseVariableCategory VariableCategory =
3752+
getOpenMPDefaultVariableCategory(
3753+
Tok.isAnnotation() ? "" : PP.getSpelling(Tok), getLangOpts());
3754+
Arg.push_back(VariableCategory);
3755+
KLoc.push_back(Tok.getLocation());
3756+
if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
3757+
Tok.isNot(tok::annot_pragma_openmp_end))
3758+
ConsumeAnyToken();
3759+
} else {
3760+
Arg.push_back(OMPC_DEFAULT_VC_all);
3761+
KLoc.push_back(SourceLocation());
3762+
}
37373763
} else if (Kind == OMPC_defaultmap) {
37383764
// Get a defaultmap modifier
37393765
unsigned Modifier = getOpenMPSimpleClauseType(
@@ -3932,6 +3958,18 @@ OMPClause *Parser::ParseOpenMPSingleExprWithArgClause(OpenMPDirectiveKind DKind,
39323958
if (NeedAnExpression && Val.isInvalid())
39333959
return nullptr;
39343960

3961+
if (Kind == OMPC_default && getLangOpts().OpenMP < 51 && Arg[0] &&
3962+
(static_cast<DefaultKind>(Arg[0]) == OMP_DEFAULT_private ||
3963+
static_cast<DefaultKind>(Arg[0]) == OMP_DEFAULT_firstprivate)) {
3964+
Diag(KLoc[0], diag::err_omp_invalid_dsa)
3965+
<< getOpenMPClauseName(static_cast<DefaultKind>(Arg[0]) ==
3966+
OMP_DEFAULT_private
3967+
? OMPC_private
3968+
: OMPC_firstprivate)
3969+
<< getOpenMPClauseName(OMPC_default) << "5.1";
3970+
return nullptr;
3971+
}
3972+
39353973
if (ParseOnly)
39363974
return nullptr;
39373975
return Actions.OpenMP().ActOnOpenMPSingleExprWithArgClause(

0 commit comments

Comments
 (0)