Skip to content

Commit 054f7c0

Browse files
committed
[OpenACC] Implement copy clause for compute constructs.
Like present, no_create, and first_private, copy is a clause that takes just a var-list, and follows the same rules as the others. The one unique part of this clause is that it ALSO supports two deprecated/backwards-compatibility spellings, so this patch adds them and implements them.
1 parent 99ca408 commit 054f7c0

18 files changed

+409
-24
lines changed

clang/include/clang/AST/OpenACCClause.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,30 @@ class OpenACCPresentClause final
351351
ArrayRef<Expr *> VarList, SourceLocation EndLoc);
352352
};
353353

354+
class OpenACCCopyClause final
355+
: public OpenACCClauseWithVarList,
356+
public llvm::TrailingObjects<OpenACCCopyClause, Expr *> {
357+
358+
OpenACCCopyClause(OpenACCClauseKind Spelling, SourceLocation BeginLoc,
359+
SourceLocation LParenLoc, ArrayRef<Expr *> VarList,
360+
SourceLocation EndLoc)
361+
: OpenACCClauseWithVarList(Spelling, BeginLoc, LParenLoc, EndLoc) {
362+
assert((Spelling == OpenACCClauseKind::Copy ||
363+
Spelling == OpenACCClauseKind::PCopy ||
364+
Spelling == OpenACCClauseKind::PresentOrCopy) &&
365+
"Invalid clause kind for copy-clause");
366+
std::uninitialized_copy(VarList.begin(), VarList.end(),
367+
getTrailingObjects<Expr *>());
368+
setExprs(MutableArrayRef(getTrailingObjects<Expr *>(), VarList.size()));
369+
}
370+
371+
public:
372+
static OpenACCCopyClause *
373+
Create(const ASTContext &C, OpenACCClauseKind Spelling,
374+
SourceLocation BeginLoc, SourceLocation LParenLoc,
375+
ArrayRef<Expr *> VarList, SourceLocation EndLoc);
376+
};
377+
354378
template <class Impl> class OpenACCClauseVisitor {
355379
Impl &getDerived() { return static_cast<Impl &>(*this); }
356380

@@ -369,6 +393,10 @@ template <class Impl> class OpenACCClauseVisitor {
369393
case OpenACCClauseKind::CLAUSE_NAME: \
370394
Visit##CLAUSE_NAME##Clause(*cast<OpenACC##CLAUSE_NAME##Clause>(C)); \
371395
return;
396+
#define CLAUSE_ALIAS(ALIAS_NAME, CLAUSE_NAME) \
397+
case OpenACCClauseKind::ALIAS_NAME: \
398+
Visit##CLAUSE_NAME##Clause(*cast<OpenACC##CLAUSE_NAME##Clause>(C)); \
399+
return;
372400
#include "clang/Basic/OpenACCClauses.def"
373401

374402
default:

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12333,4 +12333,8 @@ def err_acc_subarray_out_of_range
1233312333
def err_acc_subarray_base_plus_length_out_of_range
1233412334
: Error<"OpenACC sub-array specified range [%0:%1] would be out of the "
1233512335
"range of the subscripted array size of %2">;
12336+
def warn_acc_deprecated_alias_name
12337+
: Warning<"OpenACC clause name '%0' is a deprecated clause name and is "
12338+
"now an alias for '%1'">,
12339+
InGroup<DiagGroup<"openacc-deprecated-clause-alias">>;
1233612340
} // end of sema component.

clang/include/clang/Basic/OpenACCClauses.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@
1414
// as used in Clang source (so `Default` instead of `default`).
1515
//
1616
// VISIT_CLAUSE(CLAUSE_NAME)
17+
//
18+
// CLAUSE_ALIAS(ALIAS_NAME, CLAUSE_NAME)
19+
20+
#ifndef CLAUSE_ALIAS
21+
#define CLAUSE_ALIAS(ALIAS_NAME, CLAUSE_NAME)
22+
#endif
1723

24+
VISIT_CLAUSE(Copy)
25+
CLAUSE_ALIAS(PCopy, Copy)
26+
CLAUSE_ALIAS(PresentOrCopy, Copy)
1827
VISIT_CLAUSE(Default)
1928
VISIT_CLAUSE(FirstPrivate)
2029
VISIT_CLAUSE(If)
@@ -27,3 +36,4 @@ VISIT_CLAUSE(Self)
2736
VISIT_CLAUSE(VectorLength)
2837

2938
#undef VISIT_CLAUSE
39+
#undef CLAUSE_ALIAS

clang/include/clang/Basic/OpenACCKinds.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ enum class OpenACCClauseKind {
189189
/// 'copy' clause, allowed on Compute and Combined Constructs, plus 'data' and
190190
/// 'declare'.
191191
Copy,
192+
/// 'copy' clause alias 'pcopy'. Preserved for diagnostic purposes.
193+
PCopy,
194+
/// 'copy' clause alias 'present_or_copy'. Preserved for diagnostic purposes.
195+
PresentOrCopy,
192196
/// 'use_device' clause, allowed on 'host_data' construct.
193197
UseDevice,
194198
/// 'attach' clause, allowed on Compute and Combined constructs, plus 'data'
@@ -310,6 +314,12 @@ inline StreamTy &printOpenACCClauseKind(StreamTy &Out, OpenACCClauseKind K) {
310314
case OpenACCClauseKind::Copy:
311315
return Out << "copy";
312316

317+
case OpenACCClauseKind::PCopy:
318+
return Out << "pcopy";
319+
320+
case OpenACCClauseKind::PresentOrCopy:
321+
return Out << "present_or_copy";
322+
313323
case OpenACCClauseKind::UseDevice:
314324
return Out << "use_device";
315325

clang/include/clang/Sema/SemaOpenACC.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ class SemaOpenACC : public SemaBase {
120120
assert((ClauseKind == OpenACCClauseKind::Private ||
121121
ClauseKind == OpenACCClauseKind::NoCreate ||
122122
ClauseKind == OpenACCClauseKind::Present ||
123+
ClauseKind == OpenACCClauseKind::Copy ||
124+
ClauseKind == OpenACCClauseKind::PCopy ||
125+
ClauseKind == OpenACCClauseKind::PresentOrCopy ||
123126
ClauseKind == OpenACCClauseKind::FirstPrivate) &&
124127
"Parsed clause kind does not have a var-list");
125128
return std::get<VarListDetails>(Details).VarList;
@@ -171,6 +174,9 @@ class SemaOpenACC : public SemaBase {
171174
assert((ClauseKind == OpenACCClauseKind::Private ||
172175
ClauseKind == OpenACCClauseKind::NoCreate ||
173176
ClauseKind == OpenACCClauseKind::Present ||
177+
ClauseKind == OpenACCClauseKind::Copy ||
178+
ClauseKind == OpenACCClauseKind::PCopy ||
179+
ClauseKind == OpenACCClauseKind::PresentOrCopy ||
174180
ClauseKind == OpenACCClauseKind::FirstPrivate) &&
175181
"Parsed clause kind does not have a var-list");
176182
Details = VarListDetails{{VarList.begin(), VarList.end()}};
@@ -180,6 +186,9 @@ class SemaOpenACC : public SemaBase {
180186
assert((ClauseKind == OpenACCClauseKind::Private ||
181187
ClauseKind == OpenACCClauseKind::NoCreate ||
182188
ClauseKind == OpenACCClauseKind::Present ||
189+
ClauseKind == OpenACCClauseKind::Copy ||
190+
ClauseKind == OpenACCClauseKind::PCopy ||
191+
ClauseKind == OpenACCClauseKind::PresentOrCopy ||
183192
ClauseKind == OpenACCClauseKind::FirstPrivate) &&
184193
"Parsed clause kind does not have a var-list");
185194
Details = VarListDetails{std::move(VarList)};

clang/lib/AST/OpenACCClause.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ OpenACCClause::child_range OpenACCClause::children() {
7676
#define VISIT_CLAUSE(CLAUSE_NAME) \
7777
case OpenACCClauseKind::CLAUSE_NAME: \
7878
return cast<OpenACC##CLAUSE_NAME##Clause>(this)->children();
79+
#define CLAUSE_ALIAS(ALIAS_NAME, CLAUSE_NAME) \
80+
case OpenACCClauseKind::ALIAS_NAME: \
81+
return cast<OpenACC##CLAUSE_NAME##Clause>(this)->children();
7982

8083
#include "clang/Basic/OpenACCClauses.def"
8184
}
@@ -173,6 +176,16 @@ OpenACCPresentClause *OpenACCPresentClause::Create(const ASTContext &C,
173176
return new (Mem) OpenACCPresentClause(BeginLoc, LParenLoc, VarList, EndLoc);
174177
}
175178

179+
OpenACCCopyClause *
180+
OpenACCCopyClause::Create(const ASTContext &C, OpenACCClauseKind Spelling,
181+
SourceLocation BeginLoc, SourceLocation LParenLoc,
182+
ArrayRef<Expr *> VarList, SourceLocation EndLoc) {
183+
void *Mem =
184+
C.Allocate(OpenACCCopyClause::totalSizeToAlloc<Expr *>(VarList.size()));
185+
return new (Mem)
186+
OpenACCCopyClause(Spelling, BeginLoc, LParenLoc, VarList, EndLoc);
187+
}
188+
176189
//===----------------------------------------------------------------------===//
177190
// OpenACC clauses printing methods
178191
//===----------------------------------------------------------------------===//
@@ -249,3 +262,10 @@ void OpenACCClausePrinter::VisitPresentClause(const OpenACCPresentClause &C) {
249262
[&](const Expr *E) { printExpr(E); });
250263
OS << ")";
251264
}
265+
266+
void OpenACCClausePrinter::VisitCopyClause(const OpenACCCopyClause &C) {
267+
OS << C.getClauseKind() << '(';
268+
llvm::interleaveComma(C.getVarList(), OS,
269+
[&](const Expr *E) { printExpr(E); });
270+
OS << ")";
271+
}

clang/lib/AST/StmtProfile.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,11 @@ void OpenACCClauseProfiler::VisitIfClause(const OpenACCIfClause &Clause) {
24922492
Profiler.VisitStmt(Clause.getConditionExpr());
24932493
}
24942494

2495+
void OpenACCClauseProfiler::VisitCopyClause(const OpenACCCopyClause &Clause) {
2496+
for (auto *E : Clause.getVarList())
2497+
Profiler.VisitStmt(E);
2498+
}
2499+
24952500
void OpenACCClauseProfiler::VisitSelfClause(const OpenACCSelfClause &Clause) {
24962501
if (Clause.hasConditionExpr())
24972502
Profiler.VisitStmt(Clause.getConditionExpr());

clang/lib/AST/TextNodeDumper.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ void TextNodeDumper::Visit(const OpenACCClause *C) {
397397
case OpenACCClauseKind::Default:
398398
OS << '(' << cast<OpenACCDefaultClause>(C)->getDefaultClauseKind() << ')';
399399
break;
400+
case OpenACCClauseKind::Copy:
401+
case OpenACCClauseKind::PCopy:
402+
case OpenACCClauseKind::PresentOrCopy:
400403
case OpenACCClauseKind::If:
401404
case OpenACCClauseKind::FirstPrivate:
402405
case OpenACCClauseKind::NoCreate:

clang/lib/Parse/ParseOpenACC.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ OpenACCClauseKind getOpenACCClauseKind(Token Tok) {
102102
.Case("create", OpenACCClauseKind::Create)
103103
.Case("collapse", OpenACCClauseKind::Collapse)
104104
.Case("copy", OpenACCClauseKind::Copy)
105+
.Case("pcopy", OpenACCClauseKind::PCopy)
106+
.Case("present_or_copy", OpenACCClauseKind::PresentOrCopy)
105107
.Case("copyin", OpenACCClauseKind::CopyIn)
106108
.Case("copyout", OpenACCClauseKind::CopyOut)
107109
.Case("default", OpenACCClauseKind::Default)
@@ -489,6 +491,8 @@ ClauseParensKind getClauseParensKind(OpenACCDirectiveKind DirKind,
489491
case OpenACCClauseKind::If:
490492
case OpenACCClauseKind::Create:
491493
case OpenACCClauseKind::Copy:
494+
case OpenACCClauseKind::PCopy:
495+
case OpenACCClauseKind::PresentOrCopy:
492496
case OpenACCClauseKind::CopyIn:
493497
case OpenACCClauseKind::CopyOut:
494498
case OpenACCClauseKind::UseDevice:
@@ -920,7 +924,6 @@ Parser::OpenACCClauseParseResult Parser::ParseOpenACCClauseParams(
920924
assert(DirKind == OpenACCDirectiveKind::Update);
921925
[[fallthrough]];
922926
case OpenACCClauseKind::Attach:
923-
case OpenACCClauseKind::Copy:
924927
case OpenACCClauseKind::Delete:
925928
case OpenACCClauseKind::Detach:
926929
case OpenACCClauseKind::Device:
@@ -931,6 +934,9 @@ Parser::OpenACCClauseParseResult Parser::ParseOpenACCClauseParams(
931934
case OpenACCClauseKind::UseDevice:
932935
ParseOpenACCVarList();
933936
break;
937+
case OpenACCClauseKind::Copy:
938+
case OpenACCClauseKind::PCopy:
939+
case OpenACCClauseKind::PresentOrCopy:
934940
case OpenACCClauseKind::FirstPrivate:
935941
case OpenACCClauseKind::NoCreate:
936942
case OpenACCClauseKind::Present:

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,23 @@ bool doesClauseApplyToDirective(OpenACCDirectiveKind DirectiveKind,
153153
default:
154154
return false;
155155
}
156+
157+
case OpenACCClauseKind::Copy:
158+
case OpenACCClauseKind::PCopy:
159+
case OpenACCClauseKind::PresentOrCopy:
160+
switch (DirectiveKind) {
161+
case OpenACCDirectiveKind::Parallel:
162+
case OpenACCDirectiveKind::Serial:
163+
case OpenACCDirectiveKind::Kernels:
164+
case OpenACCDirectiveKind::Data:
165+
case OpenACCDirectiveKind::Declare:
166+
case OpenACCDirectiveKind::ParallelLoop:
167+
case OpenACCDirectiveKind::SerialLoop:
168+
case OpenACCDirectiveKind::KernelsLoop:
169+
return true;
170+
default:
171+
return false;
172+
}
156173
default:
157174
// Do nothing so we can go to the 'unimplemented' diagnostic instead.
158175
return true;
@@ -413,6 +430,26 @@ SemaOpenACC::ActOnClause(ArrayRef<const OpenACCClause *> ExistingClauses,
413430
getASTContext(), Clause.getBeginLoc(), Clause.getLParenLoc(),
414431
Clause.getVarList(), Clause.getEndLoc());
415432
}
433+
case OpenACCClauseKind::PresentOrCopy:
434+
case OpenACCClauseKind::PCopy:
435+
Diag(Clause.getBeginLoc(), diag::warn_acc_deprecated_alias_name)
436+
<< Clause.getClauseKind() << OpenACCClauseKind::Copy;
437+
LLVM_FALLTHROUGH;
438+
case OpenACCClauseKind::Copy: {
439+
// Restrictions only properly implemented on 'compute' constructs, and
440+
// 'compute' constructs are the only construct that can do anything with
441+
// this yet, so skip/treat as unimplemented in this case.
442+
if (!isOpenACCComputeDirectiveKind(Clause.getDirectiveKind()))
443+
break;
444+
445+
// ActOnVar ensured that everything is a valid variable reference, so there
446+
// really isn't anything to do here. GCC does some duplicate-finding, though
447+
// it isn't apparent in the standard where this is justified.
448+
449+
return OpenACCCopyClause::Create(
450+
getASTContext(), Clause.getClauseKind(), Clause.getBeginLoc(),
451+
Clause.getLParenLoc(), Clause.getVarList(), Clause.getEndLoc());
452+
}
416453
default:
417454
break;
418455
}

clang/lib/Sema/TreeTransform.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11264,6 +11264,17 @@ void OpenACCClauseTransform<Derived>::VisitPresentClause(
1126411264
ParsedClause.getEndLoc());
1126511265
}
1126611266

11267+
template <typename Derived>
11268+
void OpenACCClauseTransform<Derived>::VisitCopyClause(
11269+
const OpenACCCopyClause &C) {
11270+
ParsedClause.setVarListDetails(VisitVarList(C.getVarList()));
11271+
11272+
NewClause = OpenACCCopyClause::Create(
11273+
Self.getSema().getASTContext(), ParsedClause.getClauseKind(),
11274+
ParsedClause.getBeginLoc(), ParsedClause.getLParenLoc(),
11275+
ParsedClause.getVarList(), ParsedClause.getEndLoc());
11276+
}
11277+
1126711278
template <typename Derived>
1126811279
void OpenACCClauseTransform<Derived>::VisitNumWorkersClause(
1126911280
const OpenACCNumWorkersClause &C) {

clang/lib/Serialization/ASTReader.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11853,6 +11853,14 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1185311853
return OpenACCPresentClause::Create(getContext(), BeginLoc, LParenLoc,
1185411854
VarList, EndLoc);
1185511855
}
11856+
case OpenACCClauseKind::PCopy:
11857+
case OpenACCClauseKind::PresentOrCopy:
11858+
case OpenACCClauseKind::Copy: {
11859+
SourceLocation LParenLoc = readSourceLocation();
11860+
llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
11861+
return OpenACCCopyClause::Create(getContext(), ClauseKind, BeginLoc,
11862+
LParenLoc, VarList, EndLoc);
11863+
}
1185611864
case OpenACCClauseKind::Finalize:
1185711865
case OpenACCClauseKind::IfPresent:
1185811866
case OpenACCClauseKind::Seq:
@@ -11861,7 +11869,6 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1186111869
case OpenACCClauseKind::Worker:
1186211870
case OpenACCClauseKind::Vector:
1186311871
case OpenACCClauseKind::NoHost:
11864-
case OpenACCClauseKind::Copy:
1186511872
case OpenACCClauseKind::UseDevice:
1186611873
case OpenACCClauseKind::Attach:
1186711874
case OpenACCClauseKind::Delete:

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7805,6 +7805,15 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
78057805
writeOpenACCVarList(PC);
78067806
return;
78077807
}
7808+
case OpenACCClauseKind::Copy:
7809+
case OpenACCClauseKind::PCopy:
7810+
case OpenACCClauseKind::PresentOrCopy: {
7811+
const auto *CC = cast<OpenACCCopyClause>(C);
7812+
writeSourceLocation(CC->getLParenLoc());
7813+
writeOpenACCVarList(CC);
7814+
return;
7815+
}
7816+
78087817
case OpenACCClauseKind::Finalize:
78097818
case OpenACCClauseKind::IfPresent:
78107819
case OpenACCClauseKind::Seq:
@@ -7813,7 +7822,6 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
78137822
case OpenACCClauseKind::Worker:
78147823
case OpenACCClauseKind::Vector:
78157824
case OpenACCClauseKind::NoHost:
7816-
case OpenACCClauseKind::Copy:
78177825
case OpenACCClauseKind::UseDevice:
78187826
case OpenACCClauseKind::Attach:
78197827
case OpenACCClauseKind::Delete:

0 commit comments

Comments
 (0)