Skip to content

Commit

Permalink
[OpenACC] Create AST nodes for 'data' constructs
Browse files Browse the repository at this point in the history
These constructs are all very similar and closely related, so this patch
creates the AST nodes for them, serialization, printing/etc.
Additionally the restrictions are all added as tests/todos in the tests,
as those will have to be implemented once we get those clauses implemented.
  • Loading branch information
erichkeane committed Dec 12, 2024
1 parent 6f8a363 commit 010d011
Show file tree
Hide file tree
Showing 34 changed files with 1,116 additions and 59 deletions.
20 changes: 19 additions & 1 deletion clang/include/clang-c/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -2166,9 +2166,27 @@ enum CXCursorKind {
*/
CXCursor_OpenACCLoopConstruct = 321,

/** OpenACC Combined Constructs.
*/
CXCursor_OpenACCCombinedConstruct = 322,

CXCursor_LastStmt = CXCursor_OpenACCCombinedConstruct,
/** OpenACC data Construct.
*/
CXCursor_OpenACCDataConstruct = 323,

/** OpenACC enter data Construct.
*/
CXCursor_OpenACCEnterDataConstruct = 324,

/** OpenACC exit data Construct.
*/
CXCursor_OpenACCExitDataConstruct = 325,

/** OpenACC host_data Construct.
*/
CXCursor_OpenACCHostDataConstruct = 326,

CXCursor_LastStmt = CXCursor_OpenACCHostDataConstruct,

/**
* Cursor that represents the translation unit itself.
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4058,6 +4058,12 @@ DEF_TRAVERSE_STMT(OpenACCLoopConstruct,
{ TRY_TO(TraverseOpenACCAssociatedStmtConstruct(S)); })
DEF_TRAVERSE_STMT(OpenACCCombinedConstruct,
{ TRY_TO(TraverseOpenACCAssociatedStmtConstruct(S)); })
DEF_TRAVERSE_STMT(OpenACCDataConstruct,
{ TRY_TO(TraverseOpenACCAssociatedStmtConstruct(S)); })
DEF_TRAVERSE_STMT(OpenACCEnterDataConstruct, {})
DEF_TRAVERSE_STMT(OpenACCExitDataConstruct, {})
DEF_TRAVERSE_STMT(OpenACCHostDataConstruct,
{ TRY_TO(TraverseOpenACCAssociatedStmtConstruct(S)); })

// Traverse HLSL: Out argument expression
DEF_TRAVERSE_STMT(HLSLOutArgExpr, {})
Expand Down
170 changes: 170 additions & 0 deletions clang/include/clang/AST/StmtOpenACC.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,175 @@ class OpenACCCombinedConstruct final
return const_cast<OpenACCCombinedConstruct *>(this)->getLoop();
}
};

// This class represents a 'data' construct, which has an associated statement
// and clauses, but is otherwise pretty simple.
class OpenACCDataConstruct final
: public OpenACCAssociatedStmtConstruct,
public llvm::TrailingObjects<OpenACCCombinedConstruct,
const OpenACCClause *> {
OpenACCDataConstruct(unsigned NumClauses)
: OpenACCAssociatedStmtConstruct(
OpenACCDataConstructClass, OpenACCDirectiveKind::Data,
SourceLocation{}, SourceLocation{}, SourceLocation{},
/*AssociatedStmt=*/nullptr) {
std::uninitialized_value_construct(
getTrailingObjects<const OpenACCClause *>(),
getTrailingObjects<const OpenACCClause *>() + NumClauses);
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
NumClauses));
}

OpenACCDataConstruct(SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End,
ArrayRef<const OpenACCClause *> Clauses,
Stmt *StructuredBlock)
: OpenACCAssociatedStmtConstruct(OpenACCDataConstructClass,
OpenACCDirectiveKind::Data, Start,
DirectiveLoc, End, StructuredBlock) {
std::uninitialized_copy(Clauses.begin(), Clauses.end(),
getTrailingObjects<const OpenACCClause *>());
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
Clauses.size()));
}
void setStructuredBlock(Stmt *S) { setAssociatedStmt(S); }

public:
static bool classof(const Stmt *T) {
return T->getStmtClass() == OpenACCDataConstructClass;
}

static OpenACCDataConstruct *CreateEmpty(const ASTContext &C,
unsigned NumClauses);
static OpenACCDataConstruct *Create(const ASTContext &C, SourceLocation Start,
SourceLocation DirectiveLoc,
SourceLocation End,
ArrayRef<const OpenACCClause *> Clauses,
Stmt *StructuredBlock);
Stmt *getStructuredBlock() { return getAssociatedStmt(); }
const Stmt *getStructuredBlock() const {
return const_cast<OpenACCDataConstruct *>(this)->getStructuredBlock();
}
};
// This class represents a 'enter data' construct, which JUST has clauses.
class OpenACCEnterDataConstruct final
: public OpenACCConstructStmt,
public llvm::TrailingObjects<OpenACCCombinedConstruct,
const OpenACCClause *> {
OpenACCEnterDataConstruct(unsigned NumClauses)
: OpenACCConstructStmt(OpenACCEnterDataConstructClass,
OpenACCDirectiveKind::EnterData, SourceLocation{},
SourceLocation{}, SourceLocation{}) {
std::uninitialized_value_construct(
getTrailingObjects<const OpenACCClause *>(),
getTrailingObjects<const OpenACCClause *>() + NumClauses);
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
NumClauses));
}
OpenACCEnterDataConstruct(SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End,
ArrayRef<const OpenACCClause *> Clauses)
: OpenACCConstructStmt(OpenACCEnterDataConstructClass,
OpenACCDirectiveKind::EnterData, Start,
DirectiveLoc, End) {
std::uninitialized_copy(Clauses.begin(), Clauses.end(),
getTrailingObjects<const OpenACCClause *>());
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
Clauses.size()));
}

public:
static bool classof(const Stmt *T) {
return T->getStmtClass() == OpenACCEnterDataConstructClass;
}
static OpenACCEnterDataConstruct *CreateEmpty(const ASTContext &C,
unsigned NumClauses);
static OpenACCEnterDataConstruct *
Create(const ASTContext &C, SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End, ArrayRef<const OpenACCClause *> Clauses);
};
// This class represents a 'exit data' construct, which JUST has clauses.
class OpenACCExitDataConstruct final
: public OpenACCConstructStmt,
public llvm::TrailingObjects<OpenACCCombinedConstruct,
const OpenACCClause *> {
OpenACCExitDataConstruct(unsigned NumClauses)
: OpenACCConstructStmt(OpenACCExitDataConstructClass,
OpenACCDirectiveKind::ExitData, SourceLocation{},
SourceLocation{}, SourceLocation{}) {
std::uninitialized_value_construct(
getTrailingObjects<const OpenACCClause *>(),
getTrailingObjects<const OpenACCClause *>() + NumClauses);
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
NumClauses));
}
OpenACCExitDataConstruct(SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End,
ArrayRef<const OpenACCClause *> Clauses)
: OpenACCConstructStmt(OpenACCExitDataConstructClass,
OpenACCDirectiveKind::ExitData, Start,
DirectiveLoc, End) {
std::uninitialized_copy(Clauses.begin(), Clauses.end(),
getTrailingObjects<const OpenACCClause *>());
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
Clauses.size()));
}

public:
static bool classof(const Stmt *T) {
return T->getStmtClass() == OpenACCExitDataConstructClass;
}
static OpenACCExitDataConstruct *CreateEmpty(const ASTContext &C,
unsigned NumClauses);
static OpenACCExitDataConstruct *
Create(const ASTContext &C, SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End, ArrayRef<const OpenACCClause *> Clauses);
};
// This class represents a 'host_data' construct, which has an associated
// statement and clauses, but is otherwise pretty simple.
class OpenACCHostDataConstruct final
: public OpenACCAssociatedStmtConstruct,
public llvm::TrailingObjects<OpenACCCombinedConstruct,
const OpenACCClause *> {
OpenACCHostDataConstruct(unsigned NumClauses)
: OpenACCAssociatedStmtConstruct(
OpenACCHostDataConstructClass, OpenACCDirectiveKind::HostData,
SourceLocation{}, SourceLocation{}, SourceLocation{},
/*AssociatedStmt=*/nullptr) {
std::uninitialized_value_construct(
getTrailingObjects<const OpenACCClause *>(),
getTrailingObjects<const OpenACCClause *>() + NumClauses);
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
NumClauses));
}
OpenACCHostDataConstruct(SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End,
ArrayRef<const OpenACCClause *> Clauses,
Stmt *StructuredBlock)
: OpenACCAssociatedStmtConstruct(OpenACCHostDataConstructClass,
OpenACCDirectiveKind::HostData, Start,
DirectiveLoc, End, StructuredBlock) {
std::uninitialized_copy(Clauses.begin(), Clauses.end(),
getTrailingObjects<const OpenACCClause *>());
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
Clauses.size()));
}
void setStructuredBlock(Stmt *S) { setAssociatedStmt(S); }

public:
static bool classof(const Stmt *T) {
return T->getStmtClass() == OpenACCHostDataConstructClass;
}
static OpenACCHostDataConstruct *CreateEmpty(const ASTContext &C,
unsigned NumClauses);
static OpenACCHostDataConstruct *
Create(const ASTContext &C, SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End, ArrayRef<const OpenACCClause *> Clauses,
Stmt *StructuredBlock);
Stmt *getStructuredBlock() { return getAssociatedStmt(); }
const Stmt *getStructuredBlock() const {
return const_cast<OpenACCHostDataConstruct *>(this)->getStructuredBlock();
}
};
} // namespace clang
#endif // LLVM_CLANG_AST_STMTOPENACC_H
4 changes: 4 additions & 0 deletions clang/include/clang/AST/TextNodeDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ class TextNodeDumper
void VisitOpenACCConstructStmt(const OpenACCConstructStmt *S);
void VisitOpenACCLoopConstruct(const OpenACCLoopConstruct *S);
void VisitOpenACCCombinedConstruct(const OpenACCCombinedConstruct *S);
void VisitOpenACCDataConstruct(const OpenACCDataConstruct *S);
void VisitOpenACCEnterDataConstruct(const OpenACCEnterDataConstruct *S);
void VisitOpenACCExitDataConstruct(const OpenACCExitDataConstruct *S);
void VisitOpenACCHostDataConstruct(const OpenACCHostDataConstruct *S);
void VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *S);
void VisitEmbedExpr(const EmbedExpr *S);
void VisitAtomicExpr(const AtomicExpr *AE);
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/StmtNodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ def OpenACCAssociatedStmtConstruct
def OpenACCComputeConstruct : StmtNode<OpenACCAssociatedStmtConstruct>;
def OpenACCLoopConstruct : StmtNode<OpenACCAssociatedStmtConstruct>;
def OpenACCCombinedConstruct : StmtNode<OpenACCAssociatedStmtConstruct>;
def OpenACCDataConstruct : StmtNode<OpenACCAssociatedStmtConstruct>;
def OpenACCEnterDataConstruct : StmtNode<OpenACCConstructStmt>;
def OpenACCExitDataConstruct : StmtNode<OpenACCConstructStmt>;
def OpenACCHostDataConstruct : StmtNode<OpenACCAssociatedStmtConstruct>;

// OpenACC Additional Expressions.
def OpenACCAsteriskSizeExpr : StmtNode<Expr>;
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Serialization/ASTBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,10 @@ enum StmtCode {
STMT_OPENACC_LOOP_CONSTRUCT,
STMT_OPENACC_COMBINED_CONSTRUCT,
EXPR_OPENACC_ASTERISK_SIZE,
STMT_OPENACC_DATA_CONSTRUCT,
STMT_OPENACC_ENTER_DATA_CONSTRUCT,
STMT_OPENACC_EXIT_DATA_CONSTRUCT,
STMT_OPENACC_HOST_DATA_CONSTRUCT,

// HLSL Constructs
EXPR_HLSL_OUT_ARG,
Expand Down
86 changes: 86 additions & 0 deletions clang/lib/AST/StmtOpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,89 @@ OpenACCCombinedConstruct *OpenACCCombinedConstruct::Create(
OpenACCCombinedConstruct(DK, BeginLoc, DirLoc, EndLoc, Clauses, Loop);
return Inst;
}

OpenACCDataConstruct *OpenACCDataConstruct::CreateEmpty(const ASTContext &C,
unsigned NumClauses) {
void *Mem =
C.Allocate(OpenACCDataConstruct::totalSizeToAlloc<const OpenACCClause *>(
NumClauses));
auto *Inst = new (Mem) OpenACCDataConstruct(NumClauses);
return Inst;
}

OpenACCDataConstruct *
OpenACCDataConstruct::Create(const ASTContext &C, SourceLocation Start,
SourceLocation DirectiveLoc, SourceLocation End,
ArrayRef<const OpenACCClause *> Clauses,
Stmt *StructuredBlock) {
void *Mem =
C.Allocate(OpenACCDataConstruct::totalSizeToAlloc<const OpenACCClause *>(
Clauses.size()));
auto *Inst = new (Mem)
OpenACCDataConstruct(Start, DirectiveLoc, End, Clauses, StructuredBlock);
return Inst;
}

OpenACCEnterDataConstruct *
OpenACCEnterDataConstruct::CreateEmpty(const ASTContext &C,
unsigned NumClauses) {
void *Mem = C.Allocate(
OpenACCEnterDataConstruct::totalSizeToAlloc<const OpenACCClause *>(
NumClauses));
auto *Inst = new (Mem) OpenACCEnterDataConstruct(NumClauses);
return Inst;
}

OpenACCEnterDataConstruct *OpenACCEnterDataConstruct::Create(
const ASTContext &C, SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End, ArrayRef<const OpenACCClause *> Clauses) {
void *Mem = C.Allocate(
OpenACCEnterDataConstruct::totalSizeToAlloc<const OpenACCClause *>(
Clauses.size()));
auto *Inst =
new (Mem) OpenACCEnterDataConstruct(Start, DirectiveLoc, End, Clauses);
return Inst;
}

OpenACCExitDataConstruct *
OpenACCExitDataConstruct::CreateEmpty(const ASTContext &C,
unsigned NumClauses) {
void *Mem = C.Allocate(
OpenACCExitDataConstruct::totalSizeToAlloc<const OpenACCClause *>(
NumClauses));
auto *Inst = new (Mem) OpenACCExitDataConstruct(NumClauses);
return Inst;
}

OpenACCExitDataConstruct *OpenACCExitDataConstruct::Create(
const ASTContext &C, SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End, ArrayRef<const OpenACCClause *> Clauses) {
void *Mem = C.Allocate(
OpenACCExitDataConstruct::totalSizeToAlloc<const OpenACCClause *>(
Clauses.size()));
auto *Inst =
new (Mem) OpenACCExitDataConstruct(Start, DirectiveLoc, End, Clauses);
return Inst;
}

OpenACCHostDataConstruct *
OpenACCHostDataConstruct::CreateEmpty(const ASTContext &C,
unsigned NumClauses) {
void *Mem = C.Allocate(
OpenACCHostDataConstruct::totalSizeToAlloc<const OpenACCClause *>(
NumClauses));
auto *Inst = new (Mem) OpenACCHostDataConstruct(NumClauses);
return Inst;
}

OpenACCHostDataConstruct *OpenACCHostDataConstruct::Create(
const ASTContext &C, SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End, ArrayRef<const OpenACCClause *> Clauses,
Stmt *StructuredBlock) {
void *Mem = C.Allocate(
OpenACCHostDataConstruct::totalSizeToAlloc<const OpenACCClause *>(
Clauses.size()));
auto *Inst = new (Mem) OpenACCHostDataConstruct(Start, DirectiveLoc, End,
Clauses, StructuredBlock);
return Inst;
}
45 changes: 45 additions & 0 deletions clang/lib/AST/StmtPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,51 @@ void StmtPrinter::VisitOpenACCCombinedConstruct(OpenACCCombinedConstruct *S) {
PrintStmt(S->getLoop());
}

void StmtPrinter::VisitOpenACCDataConstruct(OpenACCDataConstruct *S) {
Indent() << "#pragma acc data";

if (!S->clauses().empty()) {
OS << ' ';
OpenACCClausePrinter Printer(OS, Policy);
Printer.VisitClauseList(S->clauses());
}
OS << '\n';

PrintStmt(S->getStructuredBlock());
}
void StmtPrinter::VisitOpenACCEnterDataConstruct(OpenACCEnterDataConstruct *S) {
Indent() << "#pragma acc enter data";

if (!S->clauses().empty()) {
OS << ' ';
OpenACCClausePrinter Printer(OS, Policy);
Printer.VisitClauseList(S->clauses());
}
OS << '\n';
}
void StmtPrinter::VisitOpenACCExitDataConstruct(OpenACCExitDataConstruct *S) {
Indent() << "#pragma acc exit data";

if (!S->clauses().empty()) {
OS << ' ';
OpenACCClausePrinter Printer(OS, Policy);
Printer.VisitClauseList(S->clauses());
}
OS << '\n';
}
void StmtPrinter::VisitOpenACCHostDataConstruct(OpenACCHostDataConstruct *S) {
Indent() << "#pragma acc host_data";

if (!S->clauses().empty()) {
OS << ' ';
OpenACCClausePrinter Printer(OS, Policy);
Printer.VisitClauseList(S->clauses());
}
OS << '\n';

PrintStmt(S->getStructuredBlock());
}

//===----------------------------------------------------------------------===//
// Expr printing methods.
//===----------------------------------------------------------------------===//
Expand Down
Loading

0 comments on commit 010d011

Please sign in to comment.