Skip to content

Commit

Permalink
[OpenACC] Implement 'default_async' sema
Browse files Browse the repository at this point in the history
A fairly simple one, only valid on the 'set' construct, this clause
takes an int expression.  Most of the work was already done as a part of
parsing, so this patch ends up being a lot of infrastructure.
  • Loading branch information
erichkeane committed Jan 6, 2025
1 parent 21c785d commit ff24e9a
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 52 deletions.
13 changes: 13 additions & 0 deletions clang/include/clang/AST/OpenACCClause.h
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,19 @@ class OpenACCDeviceNumClause : public OpenACCClauseWithSingleIntExpr {
SourceLocation EndLoc);
};

class OpenACCDefaultAsyncClause : public OpenACCClauseWithSingleIntExpr {
OpenACCDefaultAsyncClause(SourceLocation BeginLoc, SourceLocation LParenLoc,
Expr *IntExpr, SourceLocation EndLoc);

public:
static bool classof(const OpenACCClause *C) {
return C->getClauseKind() == OpenACCClauseKind::DefaultAsync;
}
static OpenACCDefaultAsyncClause *
Create(const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
Expr *IntExpr, SourceLocation EndLoc);
};

/// Represents a 'collapse' clause on a 'loop' construct. This clause takes an
/// integer constant expression 'N' that represents how deep to collapse the
/// construct. It also takes an optional 'force' tag that permits intervening
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/OpenACCClauses.def
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ VISIT_CLAUSE(Create)
CLAUSE_ALIAS(PCreate, Create, true)
CLAUSE_ALIAS(PresentOrCreate, Create, true)
VISIT_CLAUSE(Default)
VISIT_CLAUSE(DefaultAsync)
VISIT_CLAUSE(Delete)
VISIT_CLAUSE(Detach)
VISIT_CLAUSE(DeviceNum)
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Sema/SemaOpenACC.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ class SemaOpenACC : public SemaBase {
ClauseKind == OpenACCClauseKind::NumWorkers ||
ClauseKind == OpenACCClauseKind::Async ||
ClauseKind == OpenACCClauseKind::DeviceNum ||
ClauseKind == OpenACCClauseKind::DefaultAsync ||
ClauseKind == OpenACCClauseKind::Tile ||
ClauseKind == OpenACCClauseKind::Worker ||
ClauseKind == OpenACCClauseKind::Vector ||
Expand Down Expand Up @@ -349,6 +350,7 @@ class SemaOpenACC : public SemaBase {
ClauseKind == OpenACCClauseKind::NumWorkers ||
ClauseKind == OpenACCClauseKind::Async ||
ClauseKind == OpenACCClauseKind::DeviceNum ||
ClauseKind == OpenACCClauseKind::DefaultAsync ||
ClauseKind == OpenACCClauseKind::Tile ||
ClauseKind == OpenACCClauseKind::Gang ||
ClauseKind == OpenACCClauseKind::Worker ||
Expand Down Expand Up @@ -486,6 +488,7 @@ class SemaOpenACC : public SemaBase {
ClauseKind == OpenACCClauseKind::NumWorkers ||
ClauseKind == OpenACCClauseKind::Async ||
ClauseKind == OpenACCClauseKind::DeviceNum ||
ClauseKind == OpenACCClauseKind::DefaultAsync ||
ClauseKind == OpenACCClauseKind::Tile ||
ClauseKind == OpenACCClauseKind::Worker ||
ClauseKind == OpenACCClauseKind::Vector ||
Expand All @@ -498,6 +501,7 @@ class SemaOpenACC : public SemaBase {
ClauseKind == OpenACCClauseKind::NumWorkers ||
ClauseKind == OpenACCClauseKind::Async ||
ClauseKind == OpenACCClauseKind::DeviceNum ||
ClauseKind == OpenACCClauseKind::DefaultAsync ||
ClauseKind == OpenACCClauseKind::Tile ||
ClauseKind == OpenACCClauseKind::Worker ||
ClauseKind == OpenACCClauseKind::Vector ||
Expand Down
29 changes: 29 additions & 0 deletions clang/lib/AST/OpenACCClause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ bool OpenACCClauseWithSingleIntExpr::classof(const OpenACCClause *C) {
return OpenACCNumWorkersClause::classof(C) ||
OpenACCVectorLengthClause::classof(C) ||
OpenACCDeviceNumClause::classof(C) ||
OpenACCDefaultAsyncClause::classof(C) ||
OpenACCVectorClause::classof(C) || OpenACCWorkerClause::classof(C) ||
OpenACCCollapseClause::classof(C) || OpenACCAsyncClause::classof(C);
}
Expand Down Expand Up @@ -239,6 +240,27 @@ OpenACCDeviceNumClause *OpenACCDeviceNumClause::Create(const ASTContext &C,
return new (Mem) OpenACCDeviceNumClause(BeginLoc, LParenLoc, IntExpr, EndLoc);
}

OpenACCDefaultAsyncClause::OpenACCDefaultAsyncClause(SourceLocation BeginLoc,
SourceLocation LParenLoc,
Expr *IntExpr,
SourceLocation EndLoc)
: OpenACCClauseWithSingleIntExpr(OpenACCClauseKind::DefaultAsync, BeginLoc,
LParenLoc, IntExpr, EndLoc) {
assert((IntExpr->isInstantiationDependent() ||
IntExpr->getType()->isIntegerType()) &&
"default_async expression type not scalar/dependent");
}

OpenACCDefaultAsyncClause *
OpenACCDefaultAsyncClause::Create(const ASTContext &C, SourceLocation BeginLoc,
SourceLocation LParenLoc, Expr *IntExpr,
SourceLocation EndLoc) {
void *Mem = C.Allocate(sizeof(OpenACCDefaultAsyncClause),
alignof(OpenACCDefaultAsyncClause));
return new (Mem)
OpenACCDefaultAsyncClause(BeginLoc, LParenLoc, IntExpr, EndLoc);
}

OpenACCWaitClause *OpenACCWaitClause::Create(
const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
Expr *DevNumExpr, SourceLocation QueuesLoc, ArrayRef<Expr *> QueueIdExprs,
Expand Down Expand Up @@ -575,6 +597,13 @@ void OpenACCClausePrinter::VisitDeviceNumClause(
OS << ")";
}

void OpenACCClausePrinter::VisitDefaultAsyncClause(
const OpenACCDefaultAsyncClause &C) {
OS << "default_async(";
printExpr(C.getIntExpr());
OS << ")";
}

void OpenACCClausePrinter::VisitAsyncClause(const OpenACCAsyncClause &C) {
OS << "async";
if (C.hasIntExpr()) {
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/StmtProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2650,6 +2650,11 @@ void OpenACCClauseProfiler::VisitDeviceNumClause(
Profiler.VisitStmt(Clause.getIntExpr());
}

void OpenACCClauseProfiler::VisitDefaultAsyncClause(
const OpenACCDefaultAsyncClause &Clause) {
Profiler.VisitStmt(Clause.getIntExpr());
}

void OpenACCClauseProfiler::VisitWorkerClause(
const OpenACCWorkerClause &Clause) {
if (Clause.hasIntExpr())
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/TextNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ void TextNodeDumper::Visit(const OpenACCClause *C) {
case OpenACCClauseKind::Detach:
case OpenACCClauseKind::Delete:
case OpenACCClauseKind::DeviceNum:
case OpenACCClauseKind::DefaultAsync:
case OpenACCClauseKind::DevicePtr:
case OpenACCClauseKind::Finalize:
case OpenACCClauseKind::FirstPrivate:
Expand Down
8 changes: 1 addition & 7 deletions clang/lib/Parse/ParseOpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,13 +1082,7 @@ Parser::OpenACCClauseParseResult Parser::ParseOpenACCClauseParams(
return OpenACCCanContinue();
}

// TODO OpenACC: as we implement the 'rest' of the above, this 'if' should
// be removed leaving just the 'setIntExprDetails'.
if (ClauseKind == OpenACCClauseKind::NumWorkers ||
ClauseKind == OpenACCClauseKind::DeviceNum ||
ClauseKind == OpenACCClauseKind::VectorLength)
ParsedClause.setIntExprDetails(IntExpr.get());

ParsedClause.setIntExprDetails(IntExpr.get());
break;
}
case OpenACCClauseKind::DType:
Expand Down
31 changes: 25 additions & 6 deletions clang/lib/Sema/SemaOpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ bool doesClauseApplyToDirective(OpenACCDirectiveKind DirectiveKind,
return false;
}
}
case OpenACCClauseKind::DefaultAsync: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::Set:
return true;
default:
return false;
}
}
}

default:
Expand Down Expand Up @@ -977,6 +985,20 @@ OpenACCClause *SemaOpenACCClauseVisitor::VisitDeviceNumClause(
Clause.getEndLoc());
}

OpenACCClause *SemaOpenACCClauseVisitor::VisitDefaultAsyncClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// OpenACC 3.3 2.14.3: Two instances of the same clause may not appear on the
// same directive.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, Clause))
return nullptr;

assert(Clause.getNumIntExprs() == 1 &&
"Invalid number of expressions for default_async");
return OpenACCDefaultAsyncClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getIntExprs()[0],
Clause.getEndLoc());
}

OpenACCClause *SemaOpenACCClauseVisitor::VisitPrivateClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
Expand Down Expand Up @@ -3681,12 +3703,9 @@ bool SemaOpenACC::ActOnStartStmtDirective(
if (K == OpenACCDirectiveKind::Set &&
llvm::find_if(
Clauses,
llvm::IsaPred</*OpenACCDefaultAsyncClause,*/ // TODO: ERICH Need to
// implement.
// DefaultAsyncClause
// then enable this.
OpenACCDeviceNumClause, OpenACCDeviceTypeClause,
OpenACCIfClause>) == Clauses.end())
llvm::IsaPred<OpenACCDefaultAsyncClause, OpenACCDeviceNumClause,
OpenACCDeviceTypeClause, OpenACCIfClause>) ==
Clauses.end())
return Diag(StartLoc, diag::err_acc_construct_one_clause_of)
<< K
<< GetListOfClauses({OpenACCClauseKind::DefaultAsync,
Expand Down
23 changes: 23 additions & 0 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -11909,6 +11909,29 @@ void OpenACCClauseTransform<Derived>::VisitDeviceNumClause (
ParsedClause.getEndLoc());
}

template <typename Derived>
void OpenACCClauseTransform<Derived>::VisitDefaultAsyncClause(
const OpenACCDefaultAsyncClause &C) {
Expr *IntExpr = const_cast<Expr *>(C.getIntExpr());
assert(IntExpr && "default_async clause constructed with invalid int expr");

ExprResult Res = Self.TransformExpr(IntExpr);
if (!Res.isUsable())
return;

Res = Self.getSema().OpenACC().ActOnIntExpr(OpenACCDirectiveKind::Invalid,
C.getClauseKind(),
C.getBeginLoc(), Res.get());
if (!Res.isUsable())
return;

ParsedClause.setIntExprDetails(Res.get());
NewClause = OpenACCDefaultAsyncClause::Create(
Self.getSema().getASTContext(), ParsedClause.getBeginLoc(),
ParsedClause.getLParenLoc(), ParsedClause.getIntExprs()[0],
ParsedClause.getEndLoc());
}

template <typename Derived>
void OpenACCClauseTransform<Derived>::VisitVectorLengthClause(
const OpenACCVectorLengthClause &C) {
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12412,6 +12412,12 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
return OpenACCDeviceNumClause::Create(getContext(), BeginLoc, LParenLoc,
IntExpr, EndLoc);
}
case OpenACCClauseKind::DefaultAsync: {
SourceLocation LParenLoc = readSourceLocation();
Expr *IntExpr = readSubExpr();
return OpenACCDefaultAsyncClause::Create(getContext(), BeginLoc, LParenLoc,
IntExpr, EndLoc);
}
case OpenACCClauseKind::VectorLength: {
SourceLocation LParenLoc = readSourceLocation();
Expr *IntExpr = readSubExpr();
Expand Down Expand Up @@ -12601,7 +12607,6 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
case OpenACCClauseKind::Host:
case OpenACCClauseKind::Link:
case OpenACCClauseKind::Bind:
case OpenACCClauseKind::DefaultAsync:
case OpenACCClauseKind::Invalid:
llvm_unreachable("Clause serialization not yet implemented");
}
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8340,6 +8340,12 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
AddStmt(const_cast<Expr*>(DNC->getIntExpr()));
return;
}
case OpenACCClauseKind::DefaultAsync: {
const auto *DAC = cast<OpenACCDefaultAsyncClause>(C);
writeSourceLocation(DAC->getLParenLoc());
AddStmt(const_cast<Expr *>(DAC->getIntExpr()));
return;
}
case OpenACCClauseKind::NumWorkers: {
const auto *NWC = cast<OpenACCNumWorkersClause>(C);
writeSourceLocation(NWC->getLParenLoc());
Expand Down Expand Up @@ -8536,7 +8542,6 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
case OpenACCClauseKind::Host:
case OpenACCClauseKind::Link:
case OpenACCClauseKind::Bind:
case OpenACCClauseKind::DefaultAsync:
case OpenACCClauseKind::Invalid:
llvm_unreachable("Clause serialization not yet implemented");
}
Expand Down
13 changes: 6 additions & 7 deletions clang/test/AST/ast-print-openacc-set-construct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
unsigned Int;

void uses() {
// CHECK: #pragma acc set if(Int == 5) device_type(I) device_num(Int)
// CHECK: #pragma acc set default_async(Int) if(Int == 5) device_type(I) device_num(Int)
#pragma acc set default_async(Int) if (Int == 5) device_type(I) device_num(Int)
// CHECK: #pragma acc set device_type(I) device_num(Int)
// CHECK: #pragma acc set default_async(Int) device_type(I) device_num(Int)
#pragma acc set default_async(Int) device_type(I) device_num(Int)
// CHECK: #pragma acc set if(Int == 5) device_num(Int)
// CHECK: #pragma acc set default_async(Int) if(Int == 5) device_num(Int)
#pragma acc set default_async(Int) if (Int == 5) device_num(Int)
// CHECK: #pragma acc set if(Int == 5) device_type(I)
// CHECK: #pragma acc set default_async(Int) if(Int == 5) device_type(I)
#pragma acc set default_async(Int) if (Int == 5) device_type(I)
// CHECK: #pragma acc set if(Int == 5) device_type(I) device_num(Int)
#pragma acc set if (Int == 5) device_type(I) device_num(Int)
// CHECK-NOT: default_async(Int)
// TODO: OpenACC: Enable this when we have default_async implemented.
// #pragma acc set default_async(Int)
// CHECK: #pragma acc set default_async(Int)
#pragma acc set default_async(Int)
// CHECK: #pragma acc set if(Int == 5)
#pragma acc set if (Int == 5)
// CHECK: #pragma acc set device_type(I)
Expand Down
4 changes: 0 additions & 4 deletions clang/test/ParserOpenACC/parse-clauses.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,8 @@ void IntExprParsing() {
// expected-error@+1{{OpenACC 'set' construct must have at least one 'default_async', 'device_num', 'device_type' or 'if' clause}}
#pragma acc set default_async(5, 4)

// expected-error@+2{{OpenACC 'set' construct must have at least one 'default_async', 'device_num', 'device_type' or 'if' clause}}
// expected-warning@+1{{OpenACC clause 'default_async' not yet implemented, clause ignored}}
#pragma acc set default_async(5)

// expected-error@+2{{OpenACC 'set' construct must have at least one 'default_async', 'device_num', 'device_type' or 'if' clause}}
// expected-warning@+1{{OpenACC clause 'default_async' not yet implemented, clause ignored}}
#pragma acc set default_async(returns_int())


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void uses() {
// expected-error@+1{{OpenACC 'device_num' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop auto device_num(1)
for(unsigned i = 0; i < 5; ++i);
// expected-warning@+1{{OpenACC clause 'default_async' not yet implemented}}
// expected-error@+1{{OpenACC 'default_async' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop auto default_async(1)
for(unsigned i = 0; i < 5; ++i);
#pragma acc parallel loop auto device_type(*)
Expand Down Expand Up @@ -254,7 +254,7 @@ void uses() {
// expected-error@+1{{OpenACC 'device_num' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop device_num(1) auto
for(unsigned i = 0; i < 5; ++i);
// expected-warning@+1{{OpenACC clause 'default_async' not yet implemented}}
// expected-error@+1{{OpenACC 'default_async' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop default_async(1) auto
for(unsigned i = 0; i < 5; ++i);
#pragma acc parallel loop device_type(*) auto
Expand Down Expand Up @@ -372,7 +372,7 @@ void uses() {
// expected-error@+1{{OpenACC 'device_num' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop independent device_num(1)
for(unsigned i = 0; i < 5; ++i);
// expected-warning@+1{{OpenACC clause 'default_async' not yet implemented}}
// expected-error@+1{{OpenACC 'default_async' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop independent default_async(1)
for(unsigned i = 0; i < 5; ++i);
#pragma acc parallel loop independent device_type(*)
Expand Down Expand Up @@ -489,7 +489,7 @@ void uses() {
// expected-error@+1{{OpenACC 'device_num' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop device_num(1) independent
for(unsigned i = 0; i < 5; ++i);
// expected-warning@+1{{OpenACC clause 'default_async' not yet implemented}}
// expected-error@+1{{OpenACC 'default_async' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop default_async(1) independent
for(unsigned i = 0; i < 5; ++i);
#pragma acc parallel loop device_type(*) independent
Expand Down Expand Up @@ -615,7 +615,7 @@ void uses() {
// expected-error@+1{{OpenACC 'device_num' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop seq device_num(1)
for(unsigned i = 0; i < 5; ++i);
// expected-warning@+1{{OpenACC clause 'default_async' not yet implemented}}
// expected-error@+1{{OpenACC 'default_async' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop seq default_async(1)
for(unsigned i = 0; i < 5; ++i);
#pragma acc parallel loop seq device_type(*)
Expand Down Expand Up @@ -738,7 +738,7 @@ void uses() {
// expected-error@+1{{OpenACC 'device_num' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop device_num(1) seq
for(unsigned i = 0; i < 5; ++i);
// expected-warning@+1{{OpenACC clause 'default_async' not yet implemented}}
// expected-error@+1{{OpenACC 'default_async' clause is not valid on 'parallel loop' directive}}
#pragma acc parallel loop default_async(1) seq
for(unsigned i = 0; i < 5; ++i);
#pragma acc parallel loop device_type(*) seq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ void uses() {
// expected-error@+1{{OpenACC 'device_num' clause is not valid on 'serial loop' directive}}
#pragma acc serial loop device_type(*) device_num(1)
for(int i = 0; i < 5; ++i);
// expected-error@+2{{OpenACC clause 'default_async' may not follow a 'device_type' clause in a 'serial loop' construct}}
// expected-note@+1{{previous clause is here}}
// expected-error@+1{{OpenACC 'default_async' clause is not valid on 'serial loop' directive}}
#pragma acc serial loop device_type(*) default_async(1)
for(int i = 0; i < 5; ++i);
#pragma acc parallel loop device_type(*) async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ void uses() {
// expected-error@+1{{OpenACC 'device_num' clause is not valid on 'kernels' directive}}
#pragma acc kernels device_type(*) device_num(1)
while(1);
// expected-error@+2{{OpenACC clause 'default_async' may not follow a 'device_type' clause in a 'kernels' construct}}
// expected-note@+1{{previous clause is here}}
// expected-error@+1{{OpenACC 'default_async' clause is not valid on 'kernels' directive}}
#pragma acc kernels device_type(*) default_async(1)
while(1);
#pragma acc kernels device_type(*) async
Expand Down
Loading

0 comments on commit ff24e9a

Please sign in to comment.