Skip to content

Commit 2244d2e

Browse files
committed
[OpenACC] Implement 'if_present' clause sema
The 'if_present' clause controls the replacement of addresses in the var-list in current device memory. This clause can only go on 'host_device'. From a Sema perspective, there isn't anything to do beyond add this to AST and pass it on.
1 parent 0f776f1 commit 2244d2e

20 files changed

+169
-24
lines changed

clang/include/clang/AST/OpenACCClause.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ class OpenACCFinalizeClause : public OpenACCClause {
9898
}
9999
};
100100

101+
// Represents the 'if_present' clause.
102+
class OpenACCIfPresentClause : public OpenACCClause {
103+
protected:
104+
OpenACCIfPresentClause(SourceLocation BeginLoc, SourceLocation EndLoc)
105+
: OpenACCClause(OpenACCClauseKind::IfPresent, BeginLoc, EndLoc) {}
106+
107+
public:
108+
static bool classof(const OpenACCClause *C) {
109+
return C->getClauseKind() == OpenACCClauseKind::IfPresent;
110+
}
111+
112+
static OpenACCIfPresentClause *
113+
Create(const ASTContext &Ctx, SourceLocation BeginLoc, SourceLocation EndLoc);
114+
115+
child_range children() {
116+
return child_range(child_iterator(), child_iterator());
117+
}
118+
const_child_range children() const {
119+
return const_child_range(const_child_iterator(), const_child_iterator());
120+
}
121+
};
122+
101123
// Represents the 'independent' clause.
102124
class OpenACCIndependentClause : public OpenACCClause {
103125
protected:

clang/include/clang/Basic/OpenACCClauses.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ VISIT_CLAUSE(Finalize)
4545
VISIT_CLAUSE(FirstPrivate)
4646
VISIT_CLAUSE(Gang)
4747
VISIT_CLAUSE(If)
48+
VISIT_CLAUSE(IfPresent)
4849
VISIT_CLAUSE(Independent)
4950
VISIT_CLAUSE(NoCreate)
5051
VISIT_CLAUSE(NumGangs)

clang/lib/AST/OpenACCClause.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,14 @@ OpenACCFinalizeClause *OpenACCFinalizeClause::Create(const ASTContext &C,
452452
return new (Mem) OpenACCFinalizeClause(BeginLoc, EndLoc);
453453
}
454454

455+
OpenACCIfPresentClause *OpenACCIfPresentClause::Create(const ASTContext &C,
456+
SourceLocation BeginLoc,
457+
SourceLocation EndLoc) {
458+
void *Mem = C.Allocate(sizeof(OpenACCIfPresentClause),
459+
alignof(OpenACCIfPresentClause));
460+
return new (Mem) OpenACCIfPresentClause(BeginLoc, EndLoc);
461+
}
462+
455463
//===----------------------------------------------------------------------===//
456464
// OpenACC clauses printing methods
457465
//===----------------------------------------------------------------------===//
@@ -697,3 +705,8 @@ void OpenACCClausePrinter::VisitVectorClause(const OpenACCVectorClause &C) {
697705
void OpenACCClausePrinter::VisitFinalizeClause(const OpenACCFinalizeClause &C) {
698706
OS << "finalize";
699707
}
708+
709+
void OpenACCClausePrinter::VisitIfPresentClause(
710+
const OpenACCIfPresentClause &C) {
711+
OS << "if_present";
712+
}

clang/lib/AST/StmtProfile.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,6 +2561,9 @@ void OpenACCClauseProfiler::VisitSelfClause(const OpenACCSelfClause &Clause) {
25612561
void OpenACCClauseProfiler::VisitFinalizeClause(
25622562
const OpenACCFinalizeClause &Clause) {}
25632563

2564+
void OpenACCClauseProfiler::VisitIfPresentClause(
2565+
const OpenACCIfPresentClause &Clause) {}
2566+
25642567
void OpenACCClauseProfiler::VisitNumGangsClause(
25652568
const OpenACCNumGangsClause &Clause) {
25662569
for (auto *E : Clause.getIntExprs())

clang/lib/AST/TextNodeDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ void TextNodeDumper::Visit(const OpenACCClause *C) {
409409
case OpenACCClauseKind::PCopy:
410410
case OpenACCClauseKind::PresentOrCopy:
411411
case OpenACCClauseKind::If:
412+
case OpenACCClauseKind::IfPresent:
412413
case OpenACCClauseKind::Independent:
413414
case OpenACCClauseKind::DevicePtr:
414415
case OpenACCClauseKind::Finalize:

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,15 @@ bool doesClauseApplyToDirective(OpenACCDirectiveKind DirectiveKind,
416416
return false;
417417
}
418418
}
419+
case OpenACCClauseKind::IfPresent: {
420+
switch (DirectiveKind) {
421+
case OpenACCDirectiveKind::HostData:
422+
case OpenACCDirectiveKind::Update:
423+
return true;
424+
default:
425+
return false;
426+
}
427+
}
419428
}
420429

421430
default:
@@ -1620,6 +1629,16 @@ OpenACCClause *SemaOpenACCClauseVisitor::VisitFinalizeClause(
16201629
Clause.getEndLoc());
16211630
}
16221631

1632+
OpenACCClause *SemaOpenACCClauseVisitor::VisitIfPresentClause(
1633+
SemaOpenACC::OpenACCParsedClause &Clause) {
1634+
if (Clause.getDirectiveKind() != OpenACCDirectiveKind::HostData)
1635+
return isNotImplemented();
1636+
// There isn't anything to do here, this is only valid on one construct, and
1637+
// has no associated rules.
1638+
return OpenACCIfPresentClause::Create(Ctx, Clause.getBeginLoc(),
1639+
Clause.getEndLoc());
1640+
}
1641+
16231642
OpenACCClause *SemaOpenACCClauseVisitor::VisitSeqClause(
16241643
SemaOpenACC::OpenACCParsedClause &Clause) {
16251644
// Restrictions only properly implemented on 'loop' constructs and combined ,

clang/lib/Sema/TreeTransform.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11986,6 +11986,14 @@ void OpenACCClauseTransform<Derived>::VisitFinalizeClause(
1198611986
ParsedClause.getEndLoc());
1198711987
}
1198811988

11989+
template <typename Derived>
11990+
void OpenACCClauseTransform<Derived>::VisitIfPresentClause(
11991+
const OpenACCIfPresentClause &C) {
11992+
NewClause = OpenACCIfPresentClause::Create(Self.getSema().getASTContext(),
11993+
ParsedClause.getBeginLoc(),
11994+
ParsedClause.getEndLoc());
11995+
}
11996+
1198911997
template <typename Derived>
1199011998
void OpenACCClauseTransform<Derived>::VisitReductionClause(
1199111999
const OpenACCReductionClause &C) {

clang/lib/Serialization/ASTReader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12536,6 +12536,8 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1253612536
return OpenACCSeqClause::Create(getContext(), BeginLoc, EndLoc);
1253712537
case OpenACCClauseKind::Finalize:
1253812538
return OpenACCFinalizeClause::Create(getContext(), BeginLoc, EndLoc);
12539+
case OpenACCClauseKind::IfPresent:
12540+
return OpenACCIfPresentClause::Create(getContext(), BeginLoc, EndLoc);
1253912541
case OpenACCClauseKind::Independent:
1254012542
return OpenACCIndependentClause::Create(getContext(), BeginLoc, EndLoc);
1254112543
case OpenACCClauseKind::Auto:
@@ -12581,7 +12583,6 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1258112583
VectorExpr, EndLoc);
1258212584
}
1258312585

12584-
case OpenACCClauseKind::IfPresent:
1258512586
case OpenACCClauseKind::NoHost:
1258612587
case OpenACCClauseKind::UseDevice:
1258712588
case OpenACCClauseKind::Delete:

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8452,6 +8452,7 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
84528452
case OpenACCClauseKind::Independent:
84538453
case OpenACCClauseKind::Auto:
84548454
case OpenACCClauseKind::Finalize:
8455+
case OpenACCClauseKind::IfPresent:
84558456
// Nothing to do here, there is no additional information beyond the
84568457
// begin/end loc and clause kind.
84578458
return;
@@ -8497,7 +8498,6 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
84978498
return;
84988499
}
84998500

8500-
case OpenACCClauseKind::IfPresent:
85018501
case OpenACCClauseKind::NoHost:
85028502
case OpenACCClauseKind::UseDevice:
85038503
case OpenACCClauseKind::Delete:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ void foo() {
113113

114114
// CHECK: #pragma acc exit data copyout(i) finalize
115115
#pragma acc exit data copyout(i) finalize
116+
117+
// CHECK: #pragma acc host_data if_present
118+
#pragma acc host_data use_device(i) if_present
119+
;
116120
}

0 commit comments

Comments
 (0)