Skip to content

Commit be32621

Browse files
committed
[OpenACC] Implement 'device' and 'host' clauses for 'update'
These two clauses just take a 'var-list' and specify where the variables should be copied from/to. This patch implements the AST nodes for them and ensures they properly take a var-list.
1 parent f8f8598 commit be32621

21 files changed

+345
-67
lines changed

clang/include/clang/AST/OpenACCClause.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,52 @@ class OpenACCPresentClause final
965965
Create(const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
966966
ArrayRef<Expr *> VarList, SourceLocation EndLoc);
967967
};
968+
class OpenACCHostClause final
969+
: public OpenACCClauseWithVarList,
970+
private llvm::TrailingObjects<OpenACCHostClause, Expr *> {
971+
friend TrailingObjects;
972+
973+
OpenACCHostClause(SourceLocation BeginLoc, SourceLocation LParenLoc,
974+
ArrayRef<Expr *> VarList, SourceLocation EndLoc)
975+
: OpenACCClauseWithVarList(OpenACCClauseKind::Host, BeginLoc, LParenLoc,
976+
EndLoc) {
977+
std::uninitialized_copy(VarList.begin(), VarList.end(),
978+
getTrailingObjects<Expr *>());
979+
setExprs(MutableArrayRef(getTrailingObjects<Expr *>(), VarList.size()));
980+
}
981+
982+
public:
983+
static bool classof(const OpenACCClause *C) {
984+
return C->getClauseKind() == OpenACCClauseKind::Host;
985+
}
986+
static OpenACCHostClause *Create(const ASTContext &C, SourceLocation BeginLoc,
987+
SourceLocation LParenLoc,
988+
ArrayRef<Expr *> VarList,
989+
SourceLocation EndLoc);
990+
};
991+
992+
class OpenACCDeviceClause final
993+
: public OpenACCClauseWithVarList,
994+
private llvm::TrailingObjects<OpenACCDeviceClause, Expr *> {
995+
friend TrailingObjects;
996+
997+
OpenACCDeviceClause(SourceLocation BeginLoc, SourceLocation LParenLoc,
998+
ArrayRef<Expr *> VarList, SourceLocation EndLoc)
999+
: OpenACCClauseWithVarList(OpenACCClauseKind::Device, BeginLoc, LParenLoc,
1000+
EndLoc) {
1001+
std::uninitialized_copy(VarList.begin(), VarList.end(),
1002+
getTrailingObjects<Expr *>());
1003+
setExprs(MutableArrayRef(getTrailingObjects<Expr *>(), VarList.size()));
1004+
}
1005+
1006+
public:
1007+
static bool classof(const OpenACCClause *C) {
1008+
return C->getClauseKind() == OpenACCClauseKind::Device;
1009+
}
1010+
static OpenACCDeviceClause *
1011+
Create(const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
1012+
ArrayRef<Expr *> VarList, SourceLocation EndLoc);
1013+
};
9681014

9691015
class OpenACCCopyClause final
9701016
: public OpenACCClauseWithVarList,

clang/include/clang/Basic/OpenACCClauses.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ VISIT_CLAUSE(Default)
4141
VISIT_CLAUSE(DefaultAsync)
4242
VISIT_CLAUSE(Delete)
4343
VISIT_CLAUSE(Detach)
44+
VISIT_CLAUSE(Device)
4445
VISIT_CLAUSE(DeviceNum)
4546
VISIT_CLAUSE(DevicePtr)
4647
VISIT_CLAUSE(DeviceType)
4748
CLAUSE_ALIAS(DType, DeviceType, false)
4849
VISIT_CLAUSE(Finalize)
4950
VISIT_CLAUSE(FirstPrivate)
5051
VISIT_CLAUSE(Gang)
52+
VISIT_CLAUSE(Host)
5153
VISIT_CLAUSE(If)
5254
VISIT_CLAUSE(IfPresent)
5355
VISIT_CLAUSE(Independent)

clang/include/clang/Sema/SemaOpenACC.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ class SemaOpenACC : public SemaBase {
409409
ClauseKind == OpenACCClauseKind::Detach ||
410410
ClauseKind == OpenACCClauseKind::DevicePtr ||
411411
ClauseKind == OpenACCClauseKind::Reduction ||
412+
ClauseKind == OpenACCClauseKind::Host ||
413+
ClauseKind == OpenACCClauseKind::Device ||
412414
(ClauseKind == OpenACCClauseKind::Self &&
413415
DirKind == OpenACCDirectiveKind::Update) ||
414416
ClauseKind == OpenACCClauseKind::FirstPrivate) &&
@@ -553,6 +555,8 @@ class SemaOpenACC : public SemaBase {
553555
ClauseKind == OpenACCClauseKind::UseDevice ||
554556
ClauseKind == OpenACCClauseKind::Detach ||
555557
ClauseKind == OpenACCClauseKind::DevicePtr ||
558+
ClauseKind == OpenACCClauseKind::Host ||
559+
ClauseKind == OpenACCClauseKind::Device ||
556560
(ClauseKind == OpenACCClauseKind::Self &&
557561
DirKind == OpenACCDirectiveKind::Update) ||
558562
ClauseKind == OpenACCClauseKind::FirstPrivate) &&
@@ -594,6 +598,8 @@ class SemaOpenACC : public SemaBase {
594598
ClauseKind == OpenACCClauseKind::UseDevice ||
595599
ClauseKind == OpenACCClauseKind::Detach ||
596600
ClauseKind == OpenACCClauseKind::DevicePtr ||
601+
ClauseKind == OpenACCClauseKind::Host ||
602+
ClauseKind == OpenACCClauseKind::Device ||
597603
(ClauseKind == OpenACCClauseKind::Self &&
598604
DirKind == OpenACCDirectiveKind::Update) ||
599605
ClauseKind == OpenACCClauseKind::FirstPrivate) &&

clang/lib/AST/OpenACCClause.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ bool OpenACCClauseWithVarList::classof(const OpenACCClause *C) {
3838
OpenACCNoCreateClause::classof(C) ||
3939
OpenACCPresentClause::classof(C) || OpenACCCopyClause::classof(C) ||
4040
OpenACCCopyInClause::classof(C) || OpenACCCopyOutClause::classof(C) ||
41-
OpenACCReductionClause::classof(C) || OpenACCCreateClause::classof(C);
41+
OpenACCReductionClause::classof(C) ||
42+
OpenACCCreateClause::classof(C) || OpenACCDeviceClause::classof(C) ||
43+
OpenACCHostClause::classof(C);
4244
}
4345
bool OpenACCClauseWithCondition::classof(const OpenACCClause *C) {
4446
return OpenACCIfClause::classof(C);
@@ -406,6 +408,26 @@ OpenACCPresentClause *OpenACCPresentClause::Create(const ASTContext &C,
406408
return new (Mem) OpenACCPresentClause(BeginLoc, LParenLoc, VarList, EndLoc);
407409
}
408410

411+
OpenACCHostClause *OpenACCHostClause::Create(const ASTContext &C,
412+
SourceLocation BeginLoc,
413+
SourceLocation LParenLoc,
414+
ArrayRef<Expr *> VarList,
415+
SourceLocation EndLoc) {
416+
void *Mem =
417+
C.Allocate(OpenACCHostClause::totalSizeToAlloc<Expr *>(VarList.size()));
418+
return new (Mem) OpenACCHostClause(BeginLoc, LParenLoc, VarList, EndLoc);
419+
}
420+
421+
OpenACCDeviceClause *OpenACCDeviceClause::Create(const ASTContext &C,
422+
SourceLocation BeginLoc,
423+
SourceLocation LParenLoc,
424+
ArrayRef<Expr *> VarList,
425+
SourceLocation EndLoc) {
426+
void *Mem =
427+
C.Allocate(OpenACCDeviceClause::totalSizeToAlloc<Expr *>(VarList.size()));
428+
return new (Mem) OpenACCDeviceClause(BeginLoc, LParenLoc, VarList, EndLoc);
429+
}
430+
409431
OpenACCCopyClause *
410432
OpenACCCopyClause::Create(const ASTContext &C, OpenACCClauseKind Spelling,
411433
SourceLocation BeginLoc, SourceLocation LParenLoc,
@@ -711,6 +733,20 @@ void OpenACCClausePrinter::VisitPresentClause(const OpenACCPresentClause &C) {
711733
OS << ")";
712734
}
713735

736+
void OpenACCClausePrinter::VisitHostClause(const OpenACCHostClause &C) {
737+
OS << "host(";
738+
llvm::interleaveComma(C.getVarList(), OS,
739+
[&](const Expr *E) { printExpr(E); });
740+
OS << ")";
741+
}
742+
743+
void OpenACCClausePrinter::VisitDeviceClause(const OpenACCDeviceClause &C) {
744+
OS << "device(";
745+
llvm::interleaveComma(C.getVarList(), OS,
746+
[&](const Expr *E) { printExpr(E); });
747+
OS << ")";
748+
}
749+
714750
void OpenACCClausePrinter::VisitCopyClause(const OpenACCCopyClause &C) {
715751
OS << C.getClauseKind() << '(';
716752
llvm::interleaveComma(C.getVarList(), OS,

clang/lib/AST/StmtProfile.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,15 @@ void OpenACCClauseProfiler::VisitCreateClause(
25542554
VisitClauseWithVarList(Clause);
25552555
}
25562556

2557+
void OpenACCClauseProfiler::VisitHostClause(const OpenACCHostClause &Clause) {
2558+
VisitClauseWithVarList(Clause);
2559+
}
2560+
2561+
void OpenACCClauseProfiler::VisitDeviceClause(
2562+
const OpenACCDeviceClause &Clause) {
2563+
VisitClauseWithVarList(Clause);
2564+
}
2565+
25572566
void OpenACCClauseProfiler::VisitSelfClause(const OpenACCSelfClause &Clause) {
25582567
if (Clause.isConditionExprClause()) {
25592568
if (Clause.hasConditionExpr())

clang/lib/AST/TextNodeDumper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,13 @@ void TextNodeDumper::Visit(const OpenACCClause *C) {
408408
case OpenACCClauseKind::Copy:
409409
case OpenACCClauseKind::PCopy:
410410
case OpenACCClauseKind::PresentOrCopy:
411+
case OpenACCClauseKind::Host:
411412
case OpenACCClauseKind::If:
412413
case OpenACCClauseKind::IfPresent:
413414
case OpenACCClauseKind::Independent:
414415
case OpenACCClauseKind::Detach:
415416
case OpenACCClauseKind::Delete:
417+
case OpenACCClauseKind::Device:
416418
case OpenACCClauseKind::DeviceNum:
417419
case OpenACCClauseKind::DefaultAsync:
418420
case OpenACCClauseKind::DevicePtr:

clang/lib/Parse/ParseOpenACC.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,15 +1000,16 @@ Parser::OpenACCClauseParseResult Parser::ParseOpenACCClauseParams(
10001000
}
10011001
case OpenACCClauseKind::Self:
10021002
// The 'self' clause is a var-list instead of a 'condition' in the case of
1003-
// the 'update' clause, so we have to handle it here. U se an assert to
1003+
// the 'update' clause, so we have to handle it here. Use an assert to
10041004
// make sure we get the right differentiator.
10051005
assert(DirKind == OpenACCDirectiveKind::Update);
1006+
[[fallthrough]];
1007+
case OpenACCClauseKind::Device:
1008+
case OpenACCClauseKind::Host:
10061009
ParsedClause.setVarListDetails(ParseOpenACCVarList(ClauseKind),
10071010
/*IsReadOnly=*/false, /*IsZero=*/false);
10081011
break;
1009-
case OpenACCClauseKind::Device:
10101012
case OpenACCClauseKind::DeviceResident:
1011-
case OpenACCClauseKind::Host:
10121013
case OpenACCClauseKind::Link:
10131014
ParseOpenACCVarList(ClauseKind);
10141015
break;

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,22 @@ bool doesClauseApplyToDirective(OpenACCDirectiveKind DirectiveKind,
471471
return false;
472472
}
473473
}
474+
case OpenACCClauseKind::Device: {
475+
switch (DirectiveKind) {
476+
case OpenACCDirectiveKind::Update:
477+
return true;
478+
default:
479+
return false;
480+
}
481+
}
482+
case OpenACCClauseKind::Host: {
483+
switch (DirectiveKind) {
484+
case OpenACCDirectiveKind::Update:
485+
return true;
486+
default:
487+
return false;
488+
}
489+
}
474490
}
475491

476492
default:
@@ -1040,6 +1056,28 @@ OpenACCClause *SemaOpenACCClauseVisitor::VisitPresentClause(
10401056
Clause.getVarList(), Clause.getEndLoc());
10411057
}
10421058

1059+
OpenACCClause *SemaOpenACCClauseVisitor::VisitHostClause(
1060+
SemaOpenACC::OpenACCParsedClause &Clause) {
1061+
// ActOnVar ensured that everything is a valid variable reference, so there
1062+
// really isn't anything to do here. GCC does some duplicate-finding, though
1063+
// it isn't apparent in the standard where this is justified.
1064+
1065+
return OpenACCHostClause::Create(Ctx, Clause.getBeginLoc(),
1066+
Clause.getLParenLoc(), Clause.getVarList(),
1067+
Clause.getEndLoc());
1068+
}
1069+
1070+
OpenACCClause *SemaOpenACCClauseVisitor::VisitDeviceClause(
1071+
SemaOpenACC::OpenACCParsedClause &Clause) {
1072+
// ActOnVar ensured that everything is a valid variable reference, so there
1073+
// really isn't anything to do here. GCC does some duplicate-finding, though
1074+
// it isn't apparent in the standard where this is justified.
1075+
1076+
return OpenACCDeviceClause::Create(Ctx, Clause.getBeginLoc(),
1077+
Clause.getLParenLoc(), Clause.getVarList(),
1078+
Clause.getEndLoc());
1079+
}
1080+
10431081
OpenACCClause *SemaOpenACCClauseVisitor::VisitCopyClause(
10441082
SemaOpenACC::OpenACCParsedClause &Clause) {
10451083
// Restrictions only properly implemented on 'compute'/'combined'/'data'

clang/lib/Sema/TreeTransform.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11730,6 +11730,30 @@ void OpenACCClauseTransform<Derived>::VisitPrivateClause(
1173011730
ParsedClause.getEndLoc());
1173111731
}
1173211732

11733+
template <typename Derived>
11734+
void OpenACCClauseTransform<Derived>::VisitHostClause(
11735+
const OpenACCHostClause &C) {
11736+
ParsedClause.setVarListDetails(VisitVarList(C.getVarList()),
11737+
/*IsReadOnly=*/false, /*IsZero=*/false);
11738+
11739+
NewClause = OpenACCHostClause::Create(
11740+
Self.getSema().getASTContext(), ParsedClause.getBeginLoc(),
11741+
ParsedClause.getLParenLoc(), ParsedClause.getVarList(),
11742+
ParsedClause.getEndLoc());
11743+
}
11744+
11745+
template <typename Derived>
11746+
void OpenACCClauseTransform<Derived>::VisitDeviceClause(
11747+
const OpenACCDeviceClause &C) {
11748+
ParsedClause.setVarListDetails(VisitVarList(C.getVarList()),
11749+
/*IsReadOnly=*/false, /*IsZero=*/false);
11750+
11751+
NewClause = OpenACCDeviceClause::Create(
11752+
Self.getSema().getASTContext(), ParsedClause.getBeginLoc(),
11753+
ParsedClause.getLParenLoc(), ParsedClause.getVarList(),
11754+
ParsedClause.getEndLoc());
11755+
}
11756+
1173311757
template <typename Derived>
1173411758
void OpenACCClauseTransform<Derived>::VisitFirstPrivateClause(
1173511759
const OpenACCFirstPrivateClause &C) {

clang/lib/Serialization/ASTReader.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12439,6 +12439,18 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1243912439
return OpenACCPrivateClause::Create(getContext(), BeginLoc, LParenLoc,
1244012440
VarList, EndLoc);
1244112441
}
12442+
case OpenACCClauseKind::Host: {
12443+
SourceLocation LParenLoc = readSourceLocation();
12444+
llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12445+
return OpenACCHostClause::Create(getContext(), BeginLoc, LParenLoc, VarList,
12446+
EndLoc);
12447+
}
12448+
case OpenACCClauseKind::Device: {
12449+
SourceLocation LParenLoc = readSourceLocation();
12450+
llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12451+
return OpenACCDeviceClause::Create(getContext(), BeginLoc, LParenLoc,
12452+
VarList, EndLoc);
12453+
}
1244212454
case OpenACCClauseKind::FirstPrivate: {
1244312455
SourceLocation LParenLoc = readSourceLocation();
1244412456
llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
@@ -12611,9 +12623,7 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1261112623
}
1261212624

1261312625
case OpenACCClauseKind::NoHost:
12614-
case OpenACCClauseKind::Device:
1261512626
case OpenACCClauseKind::DeviceResident:
12616-
case OpenACCClauseKind::Host:
1261712627
case OpenACCClauseKind::Link:
1261812628
case OpenACCClauseKind::Bind:
1261912629
case OpenACCClauseKind::Invalid:

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8371,6 +8371,18 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
83718371
writeOpenACCVarList(PC);
83728372
return;
83738373
}
8374+
case OpenACCClauseKind::Host: {
8375+
const auto *HC = cast<OpenACCHostClause>(C);
8376+
writeSourceLocation(HC->getLParenLoc());
8377+
writeOpenACCVarList(HC);
8378+
return;
8379+
}
8380+
case OpenACCClauseKind::Device: {
8381+
const auto *DC = cast<OpenACCDeviceClause>(C);
8382+
writeSourceLocation(DC->getLParenLoc());
8383+
writeOpenACCVarList(DC);
8384+
return;
8385+
}
83748386
case OpenACCClauseKind::FirstPrivate: {
83758387
const auto *FPC = cast<OpenACCFirstPrivateClause>(C);
83768388
writeSourceLocation(FPC->getLParenLoc());
@@ -8544,9 +8556,7 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
85448556
}
85458557

85468558
case OpenACCClauseKind::NoHost:
8547-
case OpenACCClauseKind::Device:
85488559
case OpenACCClauseKind::DeviceResident:
8549-
case OpenACCClauseKind::Host:
85508560
case OpenACCClauseKind::Link:
85518561
case OpenACCClauseKind::Bind:
85528562
case OpenACCClauseKind::Invalid:

clang/test/AST/ast-print-openacc-update-construct.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ void uses(bool cond) {
3838

3939
// CHECK: #pragma acc update self(I, iPtr, array, array[1], array[1:2])
4040
#pragma acc update self(I, iPtr, array, array[1], array[1:2])
41+
42+
// CHECK: #pragma acc update host(I, iPtr, array, array[1], array[1:2])
43+
#pragma acc update host (I, iPtr, array, array[1], array[1:2])
44+
45+
// CHECK: #pragma acc update device(I, iPtr, array, array[1], array[1:2])
46+
#pragma acc update device(I, iPtr, array, array[1], array[1:2])
4147
}

clang/test/ParserOpenACC/parse-clauses.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -538,22 +538,18 @@ void VarListClauses() {
538538
#pragma acc serial link(s.array[s.value : 5], s.value), self
539539
for(int i = 0; i < 5;++i) {}
540540

541-
// expected-error@+2{{expected ','}}
542-
// expected-warning@+1{{OpenACC clause 'host' not yet implemented, clause ignored}}
543-
#pragma acc serial host(s.array[s.value] s.array[s.value :5] ), self
541+
// expected-error@+1{{expected ','}}
542+
#pragma acc update host(s.array[s.value] s.array[s.value :5] )
544543
for(int i = 0; i < 5;++i) {}
545544

546-
// expected-warning@+1{{OpenACC clause 'host' not yet implemented, clause ignored}}
547-
#pragma acc serial host(s.array[s.value : 5], s.value), self
545+
#pragma acc update host(s.array[s.value : 5], s.value)
548546
for(int i = 0; i < 5;++i) {}
549547

550-
// expected-error@+2{{expected ','}}
551-
// expected-warning@+1{{OpenACC clause 'device' not yet implemented, clause ignored}}
552-
#pragma acc serial device(s.array[s.value] s.array[s.value :5] ), self
548+
// expected-error@+1{{expected ','}}
549+
#pragma acc update device(s.array[s.value] s.array[s.value :5] )
553550
for(int i = 0; i < 5;++i) {}
554551

555-
// expected-warning@+1{{OpenACC clause 'device' not yet implemented, clause ignored}}
556-
#pragma acc serial device(s.array[s.value : 5], s.value), self
552+
#pragma acc update device(s.array[s.value : 5], s.value)
557553
for(int i = 0; i < 5;++i) {}
558554

559555
// expected-error@+1{{expected ','}}

0 commit comments

Comments
 (0)